EJB 3.0 module packaging overview
This page describes application packaging when you use EJBs 3.0 beans.
Packaging applications that use EJB 3.0 beans is similar to the assembly requirements for J2EE 1.4 applications: components are packaged into modules, and modules are packaged into application enterprise archive (EAR) files. The components and modules both have describing metadata provided in an XML deployment descriptor. The EJB 3.0 spec supports an additional method to describing metadata and for packaging persistence units.
The EAR file is a package file format similar to a .zip or .tar file format. The EAR file can be visualized as a collection of logical directories and files that are packaged together into a simple file. Each EAR file includes one or more Java EE module files, which can include the following:
- Java application archive (JAR) files for EJB modules. Java EE application client modules and utility class modules.
- WAR files for Web modules.
- Other technology-specific modules such as resource application archive (RAR) files and other types of modules.
EJB modules without deployment descriptors
We can package EJB modules without a deployment descriptor if we are using EJB 3.0 beans. To do this, create a JAR file with metadata in an annotation which is located in the EJB component. EJB 3.0 beans do not need an entry in the ejb-jar.xml file for metadata that we have defined through annotations.
If we installed the Feature Pack for EJB 3.0, the default was to scan annotations during the installation of an EJB 3.0 module. For WAS, V7.0, the default is not to scan pre-Java EE 5 modules during the application install or at server startup
To preserve backward compatibility with both the Feature Pack for EJB 3.0 and the Feature Pack for Web Services, we have a choice whether or not to scan legacy Web modules for additional metadata. A server level switch is defined for each feature pack scan behavior. If the default is not appropriate, the switch must be set on each server and admin server that requires a change in the default. The switches are server custom properties com.ibm.websphere.webservices.UseWSFEP61ScanPolicy={true|false} and com.ibm.websphere.ejb.UseEJB61FEPScanPolicy={true|false}. To define these properties in the admin console click Application servers > server name > Process definition > Java Virtual Machine > Custom properties.
EJB modules with deployment descriptors
We can continue to use EJB modules with deployment descriptors. Modules with deployment descriptors can support any EJB specification version level, including EJB 3.0, but generally these descriptors should reflect the implementation requirements of the components in the module.
An EJB module can have an EJB 2.1-, or earlier, style deployment descriptor, or an EJB 3.0-style deployment descriptor.
For EJB 2.1-style deployment descriptors, it is assumed that the deployment descriptor contains the full metadata for the module, and no additional scanning of annotation metadata occurs.
The EJB container annotation scanning is performed on EJB modules that either have no deployment descriptor or have an ejb-jar.xml deployment descriptor at the EJB 3.0 schema level. In other words, the scan finds the annotation and its describing metadata.
We cannot scan for component annotation metadata contained within shared libraries defined using the WAS system management shared library feature.
Persistence units
Persistence units, including the persistence.xml file and the classes associated with it, can be packaged in the module for which they are required. They can also be packaged in the separate utility JAR file that is packaged in the EAR file with its dependent module.
When a separate utility JAR file is packaged, it is necessary for the module that desires it to use the persistence units to declare a dependency on the utility JAR file using the typical MANIFEST. MF Class-Path: declarations. See the example scenario for this packaging method under the section in this topic called "Session facades used for persistence scenario"
Packaging of persistence units contained within shared libraries defined using the WAS system management shared library feature is not supported at this time.
Application packaging
We can mix EJB 2.x and earlier beans with EJB 3.0 beans in the same application, but you do need to separate EJB 2.x and earlier beans from EJB 3.0 beans so that they are not in the same modules. EJB 3.0 beans are not recognized in modules that contain EJB 2.1-style, or earlier, deployment descriptors.
In the case that the EAR file only contains the JAR and WAR files, and no application.xml file, the product provides a default J2EE 1.4 deployment descriptor that is based on the following defaults that are outlined in the Java EE specification:
- The application name is assumed to be the name of the EAR file, but with the EAR file extension removed.
- Files that are ending in .war are assumed to be Web modules. The context root of the Web module is the name of the file that is relative to the root of the application package, but with the WAR file extension removed.
- Files that are ending in .jar that are not in the /lib directory, and that contain either an ejb-jar.xml file or at least one class that defines a @Stateful or @Stateless annotation, are assumed to be EJB modules.
- Other JAR files that are not in the /lib directory are not assumed to be EJB modules.
If the application archive file contains an application.xml descriptor, processing occurs according to the directives in that descriptor.
AutoLink provides the ability to automatically resolve EJB references to components contained with an EAR file, without having to specify a JNDI binding name. This simplifies application deployment with large numbers of beans and references if they are unique and unambiguous. Restriction: AutoLink should not be used for references to components deployed on a cluster.
JPA packaging
It is recommend that persistence units be packaged in separate JAR files to make them more accessible and reusable. These can be tested outside the container, with or without actual database persistence occurring. Persistence units can be included in standalone applications or into EAR files as utility JAR files. Because of the variety of use cases and potential performance issues when scanning large quantities of classes, IBM recommends that the persistence unit defines the classes of the persistence units.
Session facades used for persistence scenario
A common pattern is to use session facades for persistence. Using session bean facades to drive JPA is supported. The EntityManager interface is not thread safe, therefore, servlets should never inject @PersistenceContext. Servlets must either use the facade pattern or use an EntityManagerFactory instance to create an EntityManager on each request.
IBM recommends that JPA persistence units be defined in a separate JAR file, apart from the session bean facades. Not only is this a best practice that gives greater flexibility in sharing, it also avoids problems mixing JPA and non-JPA annotated classes.
Typically, a JAR file is created to hold the entity classes and the JPA persistence.xml definition and added to the EAR file as a utility JAR file. The EJB 3.0 module adds a dependency on the JAR file by declaring it in the EJB 3.0 module MANIFEST.MF. For example, if an EAR contains a TradeApp.ear, TradeWeb.war, EJB3Trade.jar, and TradeInfo.jar file, the EJB3Trade.jar file would have a MANIFEST.MF that looks like the following:
Manifest-Version: 1.0 Class-Path: TradeInfo.jarThe session facade in the EJB3Trade.jar file refers to JPA entity classes and persistence units in the TradeInfo.jar file. The Web app defined in the TradeWeb.war file can do the same to work with the JPA entity objects as Data Transfer Objects flowing between the Web and EJB container tiers.Cross-tier and cross version session bean reference scenario
There are several ways to define and use references to EJB 3.0 session beans. For EJB 3.0 session to session, the @EJB injection target can be used. For cross-tier, for example, Web app to EJB 3.0 session, or cross-version, for example, EJB 2.1 session to EJB 3.0 session, an XML deployment descriptor reference can be used to define ejb-refs and ejb-local-refs. There are two variations of these, depending on whether an EJB 3.0 business interface is referred to, or a pre-EJB 3.0 component-style interface that also defines an EJBLocalHome is referred to. Web apps and client applications can also utilize the @EJB annotation if the component being referenced can be resolved using autolink.
For migration scenarios where session beans are being converted from EJB 2.1 beans to EJB 3.0 beans, the pattern is typically to edit the Session bean class, replace the implements SessionBean with implements the business interface, remove extends EJBLocalObject from the local interface and non-business throws clauses, and add the @Stateful @Local @LocalHome(<localhome>.class) or similar annotations. Existing ejb-refs and ejb-local-refs are bound to the new implementation of the session bean.
The default binding name does change.
The previous scenario uses an EJB 2.1-style client pattern with an EJB 3.0-style session bean implementation. For a more current client style, the client-side can be cleaned up to lookup the session bean business interface directly, rather than going through a home interface. In this case, it is not be necessary to define the @LocalHome(<localhome>.class) annotation. Use a variant definition of ejb-ref and ejb-local-ref to do this. Use a null value for the local-home element value and bind the ejb-local-ref to the session bean's ejblocal: binding rather than the home binding. For example:
<ejb-local-ref id="EJBLocalRef_1154112538064"> <description>com.ibm.persistence.ejb3.order.facadecom.ibm.persistence.ejb3.order.facade</description> <ejb-ref-name>ejb/OrderEJB</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <local-home></local-home> <local>com.ibm.persistence.ejb3.order.facade.OrderProcessor</local> </ejb-local-ref>The client code also needs to be adjusted to do the appropriate casting for the object being looked up. In this case, the business interface instead of the home interface:try { InitialContext ctx = new InitialContext(); orderProcessor = (OrderProcessor)ctx.lookup("java:comp/env/ejb/OrderEJB"); } catch(Exception e) { e.printStackTrace(System.out); throw new ServletException(e); }
Related concepts
EJB 3.0 deployment overview
Related tasks
Assembling EJB 3.0 modules
Develop enterprise beans
Task overview: Using enterprise beans in applications
Related
Options for the AdminApp object install, installInteractive, edit, editInteractive, update, and updateInteractive commands