Contained WithinFind More DocumentationFeatured Support Resources | Scarica il manuale in formato PDF (714 KB)
Chapter 4 Migrating from EJB 1.1 to EJB 2.0Although the EJB 1.1 specification will continue to be supported in Sun Java System Application Server 9.1, the use of the EJB 2.0 architecture is recommended, so that you can leverage its enhanced capabilities. To migrate EJB 1.1 to EJB 2.0 you need to make several modifications, including a few within the source code of the components. Essentially, the required modifications relate to the differences between EJB 1.1 and EJB 2.0, all of which are described in the following topics. EJB Query LanguageThe EJB 1.1 specification left the manner and language for forming and expressing queries for finder methods to each individual application server. While many application server vendors let developers form queries using SQL, others use their own proprietary language specific to their particular application server product. This mixture of query implementations causes inconsistencies between application servers. The EJB 2.0 specification introduces a query language called EJB Query Language, or EJB QL to correct many of these inconsistencies and shortcomings. EJB QL is based on SQL92. It defines query methods, in the form of both finder and select methods, specifically for entity beans with container-managed persistence. EJB QL’s principal advantage over SQL is its portability across EJB containers and its ability to navigate entity bean relationships. Local InterfacesIn the EJB 1.1 architecture, session and entity beans have one type of interface, a remote interface, through which they can be accessed by clients and other application components. The remote interface is designed such that a bean instance has remote capabilities; the bean inherits from RMI and can interact with distributed clients across the network. With EJB 2.0, session beans and entity beans can expose their methods to clients through two types of interfaces: a remote interface and a local interface. The 2.0 remote interface is identical to the remote interface used in the 1.1 architecture, whereby, the bean inherits from RMI, exposes its methods across the network tier, and has the same capability to interact with distributed clients. However, the local interfaces for session and entity beans provide support for lightweight access from EJBs that are local clients; that is, clients co-located in the same EJB container. The EJB 2.0 specification further requires that EJBs that use local interfaces be within the same application. That is, the deployment descriptors for an application’s EJBs using local interfaces must be contained within one ejb-jar file. The local interface is a standard Java interface. It does not inherit from RMI. An enterprise bean uses the local interface to expose its methods to other beans that reside within the same container. By using a local interface, a bean may be more tightly coupled with its clients and may be directly accessed without the overhead of a remote method call. In addition, local interfaces permit values to be passed between beans with pass by reference semantics. Because you are now passing a reference to an object, rather than the object itself, this reduces the overhead incurred when passing objects with large amounts of data, resulting in a performance gain. EJB 2.0 Container-Managed Persistence (CMP)The EJB 2.0 specification expanded CMP to allow multiple entity beans to have relationships among themselves. This is referred to as Container-Managed Relationships (CMR). The container manages the relationships and the referential integrity of the relationships. The EJB 1.1 specification presented a more limited CMP model. The EJB 1.1 architecture limited CMP to data access that is independent of the database or resource manager type. It allowed you to expose only an entity bean’s instance state through its remote interface; there is no means to expose bean relationships. The EJB 1.1 version of CMP depends on mapping the instance variables of an entity bean class to the data items representing their state in the database or resource manager. The CMP instance fields are specified in the deployment descriptor, and when the bean is deployed, the deployer uses tools to generate code that implements the mapping of the instance fields to the data items. You must also change the way you code the bean’s implementation class. According to the EJB 2.0 specification, the implementation class for an entity bean that uses CMP is now defined as an abstract class. The following topics are discussed in this section: Defining Persistent FieldsThe EJB 2.0 specification lets you designate an entity bean’s instance variables as CMP fields or CMR fields. You define these fields in the deployment descriptor. CMP fields are marked with the element cmp-field, while container-managed relationship fields are marked with the element cmr-field. In the implementation class, note that you do not declare the CMP and CMR fields as public variables. Instead, you define get and set methods in the entity bean to retrieve and set the values of these CMP and CMR fields. In this sense, beans using the 2.0 CMP follow the JavaBeans model: instead of accessing instance variables directly, clients use the entity bean’s get and set methods to retrieve and set these instance variables. Keep in mind that the get and set methods only pertain to variables that have been designated as CMP or CMR fields. Defining Entity Bean RelationshipsAs noted previously, the EJB 1.1 architecture does not support CMRs between entity beans. The EJB 2.0 architecture does support both one-to-one and one-to-many CMRs. Relationships are expressed using CMR fields, and these fields are marked as such in the deployment descriptor. You set up the CMR fields in the deployment descriptor using the appropriate deployment tool for your application server. Similar to CMP fields, the bean does not declare the CMR fields as instance variables. Instead, the bean provides get and set methods for these fields. Message-Driven BeansMessage-driven beans are another new feature introduced by the EJB 2.0 architecture. Message-driven beans are transaction-aware components that process asynchronous messages delivered through the Java Message Service (JMS). The JMS API is an integral part of the Java EE platform. Asynchronous messaging allows applications to communicate by exchanging messages so that senders are independent of receivers. The sender sends its message and does not have to wait for the receiver to receive or process that message. This differs from synchronous communication, which requires the component that is invoking a method on another component to wait or block until the processing completes and control returns to the caller component. Migrating EJB Client ApplicationsThis section includes the following topics: Declaring EJBs in the JNDI ContextIn Sun Java System Application Server 9.1, EJBs are systematically mapped to the JNDI sub-context ejb/. If you attribute the JNDI name Account to an EJB, the Sun Java System Application Server 9.1 automatically creates the reference ejb/Account in the global JNDI context. The clients of this EJB therefore have to look up ejb/Account to retrieve the corresponding home interface. Let us examine the code for a servlet method deployed in Sun ONE Application Server 6.x. The servlet presented here calls on a stateful session bean, BankTeller, mapped to the root of the JNDI context. The method whose code you are considering is responsible for retrieving the home interface of the EJB, to enable a BankTeller object to be instantiated, and a remote interface for this object to be retrieved, so that you can make business method calls to this component. /**
* Look up the BankTellerHome interface using JNDI.
*/
private BankTellerHome lookupBankTellerHome(Context ctx)
throws NamingException
{
try
{
Object home = (BankTellerHome) ctx.lookup("ejb/BankTeller");
return (BankTellerHome) PortableRemoteObject.narrow(home,
BankTellerHome.class);
}
catch (NamingException ne)
{
log("lookupBankTellerHome: unable to lookup BankTellerHome" +
"with JNDI name ’BankTeller’: " + ne.getMessage() );
throw ne;
}
}
As the code already uses ejb/BankTeller as an argument for the lookup, there is no need for modifying the code to be deployed on Sun Java System Application Server 9.1. Recap on Using EJB JNDI ReferencesThis section summarizes the considerations when using EJB JNDI references. Where noted, the consideration details are specific to a particular source application server platform. Placing EJB References in the JNDI ContextIt is only necessary to modify the name of the EJB references in the JNDI context mentioned above (moving these references from the JNDI context root to the sub-context ejb/) when the EJBs are mapped to the root of the JNDI context in the existing WebLogic application. If these EJBs are already mapped to the JNDI sub-context ejb/ in the existing application, no modification is required. However, when configuring the JNDI names of EJBs in the deployment descriptor within the Sun Java Studio IDE, it is important to avoid including the prefix ejb/ in the JNDI name of an EJB. Remember that these EJB references are automatically placed in the JNDI ejb/ sub-context with Sun Java System Application Server 9.1. So, if an EJB is given to the JNDI name BankTeller in its deployment descriptor, the reference to this EJB will be translated by Sun Java System Application Server 9.1 into ejb/BankTeller, and this is the JNDI name that client components of this EJB must use when carrying out a lookup. Global JNDI context versus local JNDI contextUsing the global JNDI context to obtain EJB references is a perfectly valid, feasible approach with Sun Java System Application Server 9.1. Nonetheless, it is preferable to stay as close as possible to the Java EE specification, and retrieve EJB references through the local JNDI context of EJB client applications. When using the local JNDI context, you must first declare EJB resource references in the deployment descriptor of the client part (web.xml for a Web application, ejb-jar.xml for an EJB component). Migrating CMP Entity EJBsThis section describes the steps to migrate your application components from the EJB 1.1 architecture to the EJB 2.0 architecture. In order to migrate a CMP 1.1 bean to CMP 2.0, we first need to verify if a particular bean can be migrated. The steps to perform this verification are as follows.
|