Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


How to Utilize EJB 2.0 Local References-3 : Page 3




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Creating a Local Interface and Accessing a Bean Through It
In order to change an existing remote reference into a local reference, three things must be changed —the deployment descriptor, the bean's interfaces, and the actual call to the other enterprise bean. In the previous section you learned how to specify a local reference via the deployment descriptor. Now you need to convert the remote interfaces into local interfaces and then modify the actual JNDI lookup call to use the new local interfaces.

The Home Interface
To specify a home interface, you must import "javax.ejb.EJBLocalHome" rather than "javax.ejb.EJBHome". The interface declaration must also be modified so that it inherits from "EJBLocalHome" (a standard Java interface), rather than "EJBHome" (an RMI interface). Also, the only exception that must be thrown by methods declared in this interface is:

 javax.ejb.CreateException as java.rmi.RemoteException is no longer needed.

The Local Interface
To specify a local interface, you must import "javax.ejb.EJBLocalObject" rather than "javax.ejb.EJBObject". The interface declaration must also be modified so that it inherits from "EJBLocalObject" (a standard Java interface), rather than "EJBObject" (an RMI interface).

Making the Call
Once the interfaces have been created and the deployment descriptor settings specified, all that remains is to perform the actual JNDI call to lookup the reference to the other enterprise bean. When making a call to a bean's remote interface, the javax.rmi.PortableRemoteObject's "narrow()" method is used to marshal the call via RMI. With a local reference, this overhead is not necessary. Instead, a simple JNDI lookup and appropriate typecast is required. The following code provides an example:

home = (ProductHome) initCtx.lookup("java:comp/env/ejb/Product"); 

Not only is this more efficient, but it is also much cleaner looking than the method used for obtaining a remote reference

The local interface provides a more efficient means of communicating between two enterprise beans that reside within the same container. This technique is especially useful when working with entity beans, as it is a well-established practice to access entity beans through session beans, rather than the client accessing the entity beans directly.

If you want to begin to use local references in your own code, be sure and check with your vendor (BEA, IBM, Borland, Silverstream, etc.) to determine if the application server version your are working is able to support local references. It will still take some time before the 2.0 spec is adopted across the board.

DevX Java Pro Kyle Gabhart is a consultant for Objective Solutions, a high-end engineering services company focusing on the medical, semiconductor, and telecommunications domains. He is an Associate Member of the Worldwide Institute of Software Architects (WWISA), an organization dedicated to promoting and enriching software architecture as a profession. Kyle is a co-author of "Professional Java and XML".
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date