Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

EJB and RMI: A Practical Introduction : Page 3

Since both RMI and EJB offer technology for the development, installation, and management of distributed Java applications in the enterprise, Java developers frequently ask whether one is a better option than the other for their given architectures. This article provides practical demonstrations for using RMI and EJB technologies, both individually and in tandem.


advertisement
EJB Architecture
Enterprise JavaBeans defines the server componential model and the interface for server-vendor-independent programming of Java applications. EJB initially utilized two fundamental models for constructing enterprise applications: the session bean and the entity bean. In the first model (session bean), the client begins a session with an object, which behaves like the application, carrying out units of work on behalf of the client with the ability to perform multiple database transactions. In the second model (entity bean), the client gets access to an object stored in a database, which represents a certain entity.

Figure 2 presents the general architecture of EJB technology.

Click to enlarge
Figure 2: General Architecture of EJB Technology



EJB architecture allows those working with an application server and its components to use absolutely any client. The absence of a precisely fixed, specific protocol makes this possible. The server can support various communication protocols with the client (e.g., RMI, IIOP (CORBA), and DCOM), simultaneously. Therefore, the client does not have to be written in Java.

An EJB server provides a set of services for EJB support. These services include management functions for distributed transactions, distributed objects, and remote object calls, along with low-level system services. More to the point, the EJB server offers all the necessary resources for supporting EJB components.

Using EJB Under RMI
A number of scripts are available for the construction of EJB applications and components. EJB development differs depending on what you're creating, whether a session bean component, an entity bean, an integral application, or the whole system, which includes a few of these types at once.

Consider the simple algorithm for developing a session bean component that updates a special account. Developing an EJB component is not, in and of itself, complex. First, you describe the business logic of your component or application using an IDE (e.g., Eclipse or NetBeans). After compilation, pack the components in an EJB JAR file (a JAR archive that contains a serialized copy of the DeploymentDescriptor class). The file contains adjustments for security and various descriptions. Furthermore, you must deploy this component (session bean) into an EJB server with server-specific tools that the vendor bundles with the server. After the deployer (for example, a database administrator) adjusts various specific attributes of this component (e.g., a mode of transactions or a level of security). As soon as the component is established on a server, clients can call remote methods of the received objects.

To demonstrate using EJB under RMI, this section uses an example from the field of electronic commerce: shopping cart. Shopping cart is an abstract container into which the e-shopper puts the goods he or she is going to purchase.

Consider an example of shopping cart as an EJB component. First, you create the remote interface for your component:

public interface ShoppingCart extends javax.ejb.EJBObject { boolean addItem(int itemNumber) throws java.rmi.RemoteException; boolean purchase() throws java.rmi.RemoteException; }

This interface defines two methods: addItem() and purchase(). The first adds goods to shopping cart, and the second completes the transaction. After that, you must write a class of your component:

public class ShoppingCartEJB implements SessionBean { public boolean addItem(int itemNumber) { // process of adding any goods into shopping cart // here can be database interaction with the help of JDBC } public boolean purchase () { // code to proceed with purchasing } public ejbCreate(String accountName, String account) { // object initializing } }

Notice this class does not implement the remote interface of the component described previously. The EJBObject class will do that later. Also note that components such as session bean do not support automatic persistence mode. Therefore, direct access to a database should be made in the session bean's methods. For example, in the method purchase() you can use JDBC calls. You can also use them for updating the information in a database (if needed).

The EJBObject class, which a server's EJB container creates during component installation, implements the remote interface. This class operates as a proxy, passing method calls through itself and transferring them to a component (which is deployed on the server).

On the client side, the client should first use the JNDI service to locate the EJBHome object of the required component. In the shopping cart example, you do this with the following code:

public interface CartHome extends javax.ejb.EJBHome { Cart create(String customerName, String account) throws RemoteException; }

Interface CartHome contains a method create(), which the client will call each time it requests a new copy of your component. This method is already implemented in the EJBObject class, so by calling it, the client also calls the ejbCreate() method of your component's class.

Now consider an example of how the code on the client side, which uses a session bean for shopping cart, can look. The client can be a "thin client" (browser) or an application written either in Java or C++ (using CORBA technology). But this article doesn't consider the latter because its covers EJB under RMI, not IIOP.

You can get the EJBHome object for the class ShoppingCart by using the following piece of code:

Context initialContext = new InitialContext(); CartHome cartHome = (CartHome) initialContext.lookup("applications/mall/shopping carts");

In this snippet, InitialContext() gets the root of all JNDI names in its hierarchy, lookup() gets the CartHome object, and "applications/mall/shopping carts" is a JNDI path to the required CartHome class. Thus, now you have the cartHome reference to the EJBHome object for getting access to ShoppingCartEJB. However, don't forget that the JNDI naming space can be configured to include EJB containers located on different computers in a network. The client cannot determine the actual arrangement of the EJB container.

The following code shows how the client uses your component's EJBHome object call methods:

ShoppingCartEJB cart = cartHome.create("green","333"); cart.addItem(162); cart.addItem(375); cart.purchase();

Here, you used the create() method to create a new object of the session bean component. The variable cart now contains the reference to a remote EJB object and allows you to call its methods: addItem() and purchase().



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap