J2EE Architecture(14)
1、实体Bean的生命期
1)创建实体Bean
容器调用如下方法创建实体Bean:
newInstance()
setEntityContext(...)
newInstance()创建Bean实例,setEntityContext(...)将Entity Context属性对象作为参数。
2)实体Bean的活动和服务
池化的Bean不表示任何特定数据。对于这样的实体Bean而言,不能调用其它业务方法。
可利用池化Bean执行一些通用活动,另外,当容器不需要池时,可进行销毁。
此阶段,可以执行如下3个动作:
(1)调用ejbHome(),在Bean实例上执行一些通用操作;
(2)调用ejbFind(),以查找持久数据集合;
(3)调用unsetEntityContext()方法销毁Bean实例,并对实例进行无用单元回收处理。
如需要新建数据,容器将从Bean池选一个实例,并按顺序调用以下方法:
ejbCreate()
ejbPostCreate()
在企业Bean中,ejbCreate()方法将接收大量参数,符合主键概念。故可能使用大量重载的ejbCreate()方法,以满足企业持久数据定义的需要。
在供应业务数据时,ejbCreate()方法用供应值刷新Bean实例的实例变量。
可以在Bean上运行ejbPostCreate()方法,以完成Bean的创建。此时,Bean进入“就绪”状态,可以接收业务方法的调用。
在实体Bean实例就绪后,它实际上将与数据库表的特殊记录等例子对应。现在,可在特定Bean实例上运行业务方法。
在Bean上运行业务方法比较复杂,因为多个客户端可能同时访问此类持久数据。为此,容器需要将Bean上的方法调用与数据库保持同步。容器必须确保客户端总能访问到最新的持久数据。
通过以下两种方法实现:
ejbStore()
ejbLoad()
调用ejbLoad()方法,它用来自数据库的最新值更新Bean的属性;调用ejbStore()方法,它将Bean实例的最新值保存在数据库表的记录中。
实体Bean如长期处于不活动状态,容器可能觉得将其换出,并将资源分配给活动Bean。相反,容器也可能决定将Bean换入,将其激活。
3)钝化
容器如决定换出Bean时,将顺序调用如下方法:
ejbStore()
ejbPassivate()
这将使Bean进入池化状态,并将Bean的数据保存在辅助存储器中;
4)激活
当容器决定换入Bean时,将顺序调用如下方法:
ejbActivate()
ejbLoad()
5)销毁实体Bean。
容器调用Bean上的ejbRemove()方法来销毁实体Bean。
这个操作使Bean实例重“就绪”状态进入“池化”状态。
2、消息驱动Bean的生命期
MDB的生命期最简单。
1)MDB的创建
容器调用3个方法创建MDB实例:
newInstance()
setMessageDrivenContext()
ejbCreate()
2)MDB的活动和服务
当业务方法onMessage()接收到JMS消息时,池化MDB将转入操作状态。消息必须是JMS支持的类型。
MDB的onMessage()可能接收以下类型的消息对象:
TextMessage
ByteMessage
ObjectMessage
StreamMessage
MapMessage
需正确识别消息类型,进行适当操作。发送电子邮件消息、更新数据库是一些JMS操作实例。
3)销毁MDB
调用Bean上的ejbRemove()方法,以释放Bean所占的所有资源。
3、EJB高级特性
CMP Bean中的高级特性:
1)Home方法
2)抽象持久模式(Abstract Persistence Schema)
3)EJB查询语言
4)使用CMP Bean管理关系
5)本地接口
分享到:
相关推荐
J2EE Architecture 介绍web,mvc,架构,EJB规则,bean
J2EE Architecture and Design Patterns J2EE Architecture and Design Patterns
Lightweight J2EE Architecture 轻量级J2EE架构
The Spring Framework Introduction To Lightweight j2Ee Architecture.pdf
The J2EE Connector architecture defines a standard architecture for connecting the J2EE platform to heterogeneous EISs. Examples of EISs include Enterprise Resource Planning (ERP), mainframe ...
应用开发与运作平台J2EE Architecture Overview
J2EE Architecture An Overview的源码及flash课件 (共3部分,请下载完毕后解压)
J2EE Architecture An Overview(part3)源码及flash课件 (共三部分,请下载完毕后解压)
J2EE Architecture An Overview(part2)源码及flash课件 (共三部分,请下载完毕后解压)
Practical J2EE Application Architecture 经典书籍,供大家参考。
J2EE Connector Architecture and Enterprise Application Integration一本很不错的书
介绍j2EE connector Architecture
McGrawHill-Practical J2EE Application Architecture
J2EE 连接器架构(J2EE Connector Architecture, JCA)简介
Addison-Wesley - J2EE Connector Architecture and Enterprise Application Integration.pdf
good description about how to build up one best J2EE architecture application
The gain a good understanding of J2EE Architecture and technologies
Adapter 适配器开发 Adapter 适配器开发
a slides introduce J2EE architecture