Tutorial for building J2EE Applications using JBOSS and ECLIPSE Chapter 4

來源:互聯網
上載者:User

一路跌跌撞撞的把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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.