JaVa
   

Enterprise Beans and the EJB JAR File

Enterprise JavaBeans use the JAR file format to package enterprise beans. The EJB JAR file is used to package un-assembled enterprise beans and to package assembled beans. Un-assembled enterprise beans have only generic information created by the bean developer. Assembled enterprise beans have information for a particular environment stored in the deployment descriptor by the app assembler. Basically, there are different roles for building and deploying enterprise beans. In this tutorial, we are both the bean provider and the app assembler (in addition to the deployer and the administrator). However, the techniques we present can be adapted to situations in which assembling and deploying are performed by separate organizations. Like the Web app WAR file, the EJB JAR file has a deployment descriptor. It is stored in the META-INF directory in a file called ejb-jar.xml. This deployment descriptor contains the following information:

The following listing is an example of an EJB deployment descriptor; it’s based on an example in .

<ejb-jar>
<description>
This ejb-jar file contains the Enterprise beans for the Model 2 Hello World app.
</description>
<ejb-client-jar>client-greet-ejbs.jar</ejb-client-jar>
<enterprise-beans>
 <entity>
 <description>
 The GreetingEntityBean is a do nothing bean to demonstrate
 how to deploy an Enterprise bean with Ant.
 </description>
 <ejb-name>GreetingEntityBean</ejb-name>
 <home>xptoolkit.ejbs.GreetingEntityHome</home>
 <remote>xptoolkit.ejbs.GreetingEntityRemote</remote>
 <ejb-class>xptoolkit.ejbs.GreetingEntityBean</ejb-class>
 <transaction-type>Container</transaction-type>
 <reentrant>True</reentrant>
 <prim-key-class>java.lang.Integer</prim-key-class>
 <persistence-type>Bean</persistence-type>
 </entity>
 <session>
 <description>
 The GreetingSessionBean is a do nothing bean to demonstrate
 how to deploy an Enterprise bean with Ant.
 </description>
 <ejb-name>GreetingSessionBean</ejb-name>
 <home>xptoolkit.ejbs.GreetingSessionHome</home>
 <remote>xptoolkit.ejbs.GreetingSessionRemote</remote>
 <ejb-class>xptoolkit.ejbs.GreetingSessionBean</ejb-class>
 <session-type>Stateful</session-type>
 <transaction-type>Container</transaction-type>
 <ejb-ref>
 <description>
 This sets up a references from the Entity bean to the session bean.
 Thus, the session bean can look up the Entity bean in its environment space.
 </description>
 <ejb-ref-name>ejb/GreetingEntityBean</ejb-ref-name>
 <ejb-ref-type>Entity</ejb-ref-type>
 <home>xptoolkit.ejbs.GreetingEntityHome</home>
 <remote>xptoolkit.ejbs.GreetingEntityRemote</remote>
 <ejb-link>GreetingEntityBean</ejb-link>
 </ejb-ref>
 </session>
</enterprise-beans>
<assembly-descriptor>
 <container-transaction>
 <method>
 <ejb-name>GreetingSessionBean</ejb-name>
 <method-name>*</method-name>
 </method>
 <trans-attribute>Supports</trans-attribute>
 </container-transaction>
 <container-transaction>
 <method>
 <ejb-name>GreetingEntityBean</ejb-name>
 <method-name>*</method-name>
 </method>
 <trans-attribute>Supports</trans-attribute>
 </container-transaction>
</assembly-descriptor>
</ejb-jar>


This listing defines two enterprise beans: GreetingSessionBean and GreetingEntityBean. The session bean has a reference to the entity bean. We’ll explain this example in more detail in . Notice that the enterprise-bean element defines entity and session sub-elements. The entity and session elements contain references to the remote, home, and bean classes. These classes are defined in the root of the bean class just like classes of a regular JAR file. For example, the directory structure of the EJB file described in the previous listing may look like this:

EJB Jar File root
|
| client-greet-ejbs.jar
|
+---META-INF
| MANIFEST.MF
| ejb-jar.xml
|
\---xptoolkit
 |
 \---ejbs
 GreetingEntityBean.class
 GreetingEntityHome.class
 GreetingEntityRemote.class
 GreetingSessionBean.class
 GreetingSessionRemote.class
 GreetingSessionHome.class


This example is based on one presented in . For more information about EJB JAR files and EJB deployment descriptors, refer to the official EJB specifications online at http://java.oracle.com/products/ejb/.


JaVa
   
Comments