Tutorial for building J2EE Applications using JBOSS and ECLIPSE Chapter 4

一路跌跌撞撞的把statefule seesion bean 和stateless session beand 的例子都做完了,感觉还是有一点模糊,于是重新翻阅《Mastering EJB 2nd》和sun的培训教程SL - 351终于对EJB有了新的认识。

先看下图:

 EJB的整个请求过程

上图完整的说明了请求EJB方法的具体流程。这个流程的home interface是采用EJBHome接口,remote ingerface采用EjbObject接口。我在这里说明采用什么接口的原因是在ejb 2.0中新增加了一种高效接口EjbLocalHome和EjbLocalObject。其实是与EJBHome和EJBObject相对应的,通过EjbLocalHome获得EjbLocalObject的引用。这两种不同的方法调用主要是效率不同,在《mastering ejb》一书中是这样描述的。

EjbHome->EjbOjbect
1. The client calls a local stub.
2. The stub marshals parameters into a form suitable for the network.
3. The stub goes over a network connection to the skeleton.
4. The skeleton demarshals parameters into a form suitable for Java.
5. The skeleton calls the EJB object.
6. The EJB object performs needed middleware, such as connection pooling, transactions, security, and lifecycle services.
7. Once the EJB object calls the enterprise bean instance, and the bean does
its work, each of the preceding steps must be repeated for the return
trip home.

EjbLocalHome->EjbLocalObject
1. The client calls a local object.
2. The local object performs needed middleware, such as connection pooling, transactions, security, and lifecycle services.
3. Once the enterprise bean instance does its work, it returns control to the local object, which then returns control to the client.

由上面的流程可以知道,使用local接口可以省下几步,对于这个接口我暂时没有弄明白怎么应用,暂时先放下,等以后有机会才试。

下图的EJB Model显示了EJB2.0可以使用的所有接口

针对教程第三,四章的Stateless Session Bean和Stateful Session Bean的例子,我参考了sun sl-351的教程。里面对这两种bean的图片说明不错,我就把它摘抄下来了。


 Container Creates a Stateless Session Bean

         上图中Step 1是Contariner实例化一个EJB Object,这个特性是为了更节省资源,直到Session Bean被调用的时候才实例化或者直接从pool内取出已经有的了。在《Mastering EJB》P48中,究竟一个EJBHome对应多少个Bean,多少个EJBObject对应多少个Bean,这些都是取决于所使用的容器,有可能是1:N,有可能是M:N,而这些对于编写Bean的人来说都是透明的,主要是选择一个高性能的容器。
         Step 2
         1。client调用一个业务方法,如果是stateless bean在pool中找不到,就按照以下步骤执行:
            a。容器初始化session context。(session context现在还没有弄明白是什么东西,以后再研究)
            b。如果在pool中找到了bean,就把它移动working area。否则,用无参数constructor实例化它。
            c。session context初始化完毕,把它的引用通过setSessionContext传给新的bean。
            d。通过create方法去调用bean中的ejbCreate,这两个方法是一一对应的,而且在
                  bean中必须要有一个ejbCreate。
         2。调用bean实例中的业务方法。


 Container Creates a Statelful Session Bean

当一个client端调用home的create,会发生下面以下几个步骤。
1。容器初始化EJB Object。
2。容器初始化session context。
3。bean会用一个默认的constructor创建(只要没有定义其他的constructor)
4。session context初始化完毕,把引用通过setSessionContext传给bean。
5。create参数是有client通过home object传过来的,然后再传给bean相应的ejbCreate。

两种bean类型的一个很明显的区别就是client调用home object的create方法时,statefule可以带参数,stateless不允许带参数。

session bean的remove可以通过客户端也可以通过服务器。

总结:引用《mastering ejb》的说明

The enterprise bean instance is a Java object instance of an enterprise bean class. It contains business method implementations of the methods defined in the remote and/or local interface. The enterprise bean instance is networkless in that it contains no networked logic.
The remote interface is a Java interface that enumerates the business methods exposed by the enterprise bean class. In EJB, client code always goes through the remote interface and never interacts with the enterprise bean
instance. The remote interface is network-aware in that the interface obeys the rules for Java RMI-IIOP.
The local interface is the high-performing version of the remote interface.Use the local interface when you are calling enterprise beans that live in the same process. Your calls will not undergo stubs, skeletons, network calls, or marshaling/demarshaling of parameters.
The EJB object is the container-generated implementation of the remote interface. The EJB object is a network-aware intermediary between the client and the bean instance, handling necessary middleware issues. All client invocations go through the EJB object. The EJB object delegates calls to enterprise bean instances and implements the remote interface.
The local object is the high-performing version of the EJB object. The local object implements the local interface.
The home interface is a Java interface that serves as a factory for EJB objects.Client code that wants to work with EJB objects must use the home interface to generate them. The home interface is network-aware because clients
use it across the network.
The local home interface is the high-performing version of the home interface.
The home object is the container-generated implementation of the home interface. The home object is also network-aware, and it obeys RMI-IIOP’s rules.
The local home object is the high-performing version of the home object.The local home object implements the local home interface.
The deployment descriptor is an XML file that specifies the middleware requirements of your bean. You use the deployment descriptor to inform the container about the implicit middleware you want, such as how to manage your bean, your bean’s lifecycle needs, your transactional needs, your persistence needs, and your security needs.
The vendor-specific files allow you to take advantage of vendor-specific features. These files are not portable between application servers.
The Ejb-jar file is the finished, complete .ZIP file that contains the above files. It is the unit of deployment and is given to the application server. The application server unpacks the Ejb-jar file and loads the bean.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值