Содержащиеся в
Найти другие документыРесурсы поддержки | Загрузить это руководство в формате PDF (625 КБ)
Chapter 5 Migrating EJBAlthough the EJB 1.1 and EJB 2.0 specification will continue to be supported in Sun Java System Application Server 9, the use of the EJB 3.0 architecture is recommended to leverage its enhanced capabilities. To migrate a number of modifications are required, including within the source code of components. You can use the Migration Tool to migrate from EJB 1.1 to EJB 2.0. The Migration Tool will only perform EJB 1.1 to EJB 2.0 conversions if you selected the "Migrate CMPs from EJB 1.1 to 2.0 specs." option. You should choose the this option only if all source files (.java files) and the deployment descriptors (ejb-jar.xml and sun-j2ee-ri.xml files) are available in the input source code. This migration requires and modifies all of these files to affect this migration effectively. If, after verification, the Migration Tool is satisfied that a CMP EJB 1.1 enterprise bean can be migrated to EJB 2.0, it will make the appropriate changes. The modifications required to migrate from EJB 2.0 to EJB 3.0 are related to the differences between EJB 2.0 and EJB 2.0. These differences are described in the following topics. Metadata AnnotationsAn annotation consists of the @ sign preceding the annotation type, followed by a parenthesized list of element-value pairs. The EJB 3.0 Specification defines a variety of annotation types such as those that specify a bean's type (@Stateless, @Stateful, @MessageDriven, @Entity), whether a bean is remotely or locally accessible (@Remote, @Local), transaction attributes (@TransactionAttribute), and security and method permissions (@MethodPermissions, @Unchecked, @SecurityRoles). Annotations for the EJB 3.0 annotation types generate interfaces required by the class as well as references to objects in the environment. In many cases, defaults can be used instead of explicit metadata annotation elements. In these cases, a developer doesn't have to completely specify a metadata annotation to obtain the same result as if the annotation was fully specified. For example, by default, an entity bean (annotated by @Entity) has a default entity type of CMP, indicating that it has container-managed persistence. These defaults can make annotating enterprise beans very simple. In fact, in many cases, defaults are assumed when an annotation is not specified. In those cases, the defaults represent the most common specifications. For example, container-managed transaction demarcation (where the container, as opposed to the bean, manages the commitment or rollback of a unit of work to a database) is assumed for an enterprise bean if no annotation is specified. These defaults illustrate the "coding by exception" approach that guides EJB 3.0 technology. The intent is to simplify things for developers by forcing them to code things only where defaults are not adequate. EJB Query LanguageEJB 3.0 has made enhancements to EJB QL to provide additional functionality. Addition of projection, explicit inner and outer join operations, bulk update and delete, subqueries, and group-by. Addition of a dynamic query capability and support for native SQL queries. 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. The 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. Local InterfacesUnder the EJB 3.0 API, the business interface of an enterprise bean is a plain Java interface, not an EJBObject or EJBLocalObject interface. However, usage of the earlier EJBObject and EJBLocalObjectinterface types continues to be supported under EJB 3.0. 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. In 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. 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 3.0 Entity ModelTo create the session bean, a developer only needs to code a bean class and annotate it with appropriate metadata. A session bean need not home interface, remote interface, or deployment descriptor to code. All that is required in addition to the bean class is a business interface, and that can be generated by default. The bean class is coded as a plain old Java object (POJO) rather than a class that implements an interface such as javax.ejb.SessionBean. Because the bean class does not implement an interface such as javax.ejb.SessionBean, a developer no longer has to implement methods such as ejbRemove, ejbActivate, or ejbPassivate in the bean class. However a developer can implement any or all of these callbacks if they are needed. If the bean class implements one of these callbacks, the EJB container calls it just as it does for EJB 2.1 technology. The new Java Persistence API in EJB 3.0 defines a new single model for implementing persistence in the Java platform. In EJB 3.0 technology, an entity bean class is a concrete class. It's no longer an abstract class. The EJB 2.0 specification uses the CMP model. It expanded the existing 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.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 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. The following topics are discussed in this section: Defining Persistent FieldsIn EJB 3.0, get and set methods are concrete, not abstract. In addition, these methods can include logic, something that was not possible previously. This is useful for actions such as validating fields. Another improvement is that access to the persistence fields is not limited to the get and set methods. The persistence fields are also accessible through a bean class's business methods. One restriction however is that in EJB 3.0 technology, only methods within the class can access persistence fields -- in other words, you cannot expose the instance variables outside of the class. The 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. Dependency InjectionPrevious versions of the EJB architecture forced the developer into complying with the requirements of the EJB container in terms of providing classes and implementing interfaces. By comparison, In EJB 3.0, dependency injection reflects the fact that the bean tells the EJB container what it needs, and then container satisfies those needs. Message-Driven BeansEJB 3.0 enhances message-driven beans with support for interceptors. Message-driven beans were a 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). Migrating EJB Client ApplicationsExisting EJB 2.1 and earlier applications are supported to run unchanged in EJB 3.0 containers. All EJB 3.0 implementations support EJB 1.1, EJB 2.0, and EJB 2.1 deployment descriptors for applications written to earlier versions. Clients written to the new EJB 3.0 APIA client written to the EJB 3.0 API may be a client of a component written to the EJB 2.1 or earlier API. Such clients may access components written to the EJB 3.0 APIs and components written to the earlier EJB APIs within the same transaction. Such clients access components written to the earlier EJB APIs using the EJB 2.1 client view home and component interfaces. The EJB annotation may be used for the injection of home interfaces into components that are clients of beans written to the earlier EJB client view. Declaring EJBs in the JNDI ContextIn Application Server 9, EJBs are systematically mapped to the JNDI sub-context ejb/. If you attribute the JNDI name Account to an EJB, the Application Server 9 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 Application Server 9. 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 Application Server 9. So, if an EJB is given to the JNDI name BankTeller in its deployment descriptor, the reference to this EJB will be translated by Application Server 9 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 Application Server 9. 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 EJBsWith the introduction of EJB 3.0, you can use JDO (in addition to CMP 2.0), which is an architecture that provides a standard way to transparently persist plain Java objects. EJB 2.x and EJB 3.0 uses CMP 2.0. 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.
|