I can listen to only so much hype about a new technology before I finally have to shout, Let me see some code! Ive been hearing about Enterprise JavaBeans (EJB) for some time now, and I have been responsible for spreading some of that EJB hype around myself. For instance, in the March 1999 issue of Midrange Computing, which focused on Java (and of which I was the editor), IBMs Keith Rutledge said, EJB is a standard computing model that works across computing platforms from different vendors.... EJB
makes it easier to write application logic and applications by handling the low-level infrastructure taskssuch things as threading, state management, resource pooling, and the like.
Enough already; its time to look under the hood of an EJB application. The beauty of EJB is that the client Java application programmers dont have to deal with the complexity of the host Java classes. The code presented in this article focuses on the client developers view. I overview server-side EJB development without presenting its code; the client programmer doesnt need to know about it anyway.
Session Beans and Entity Beans
There are two basic types of EJB: session beans and entity beans. Examples of session beans might be a shopping cart or a grocery list of an e-commerce site. But ignore session beans for now; you wont be using them until you begin the development of advanced e- commerce applications. This articles focus is on entity beans. Entity beans are essentially Java wrappers for persistent data, and for us Java application programmers, our persistent data store is DB2/400. Typically, we will have an entity bean for each record format for the database files that comprise our e-commerce applications.
Wrapping Your Order: Paper or Plastic?
Remember that this is object-oriented (OO) programming. An entity bean not only provides methods that access the information of a Relational Database (RDB) file but also provides business methods that modify that information as well as methods that collaborate with other EJB. This articles example bean is an OO wrapper for the information that is
contained in an RDB file called Order. The Order file has the simple format of order number (which is also the primary key), customer number, and due date.
There are four basic components to any EJB: two interfaces (one home interface and one remote interface) and two classes (the entity bean itself and a primary key class). An interface is simply a list of methods that some Java class must later implementsimilar to a set of RPG IV D-specs that contains function and procedure prototypes. A beans home interface provides the CRUD: the EJB life-cycle methods for Creation, Retrieval, Update, and Deletion of an entity bean. Figure 1 shows the OrderHome interface with the declaration of the create and findByPrimaryKey methodsin other words, the Create and Retrieval of CRUD. So wheres the U and D? The Delete is provided for with the declaration of the remove method in the OrderHomes base interface, EJBHome (see Figure 2). An Update method is not a required interface for the client, because the entity bean on the server makes the state of a modified entity persistent by updating the database at the appropriate times (using a function called ejbStore).
The Order interface, the second of the two interfaces, is in Figure 3; it simply provides two methods: getDueDate and getCustNo. For a more complete example, Id also have numerous other methods (methods, for instance, that modify the entitys attributes or provide collaboration with other associated entity beans). In the Order example, I would probably have getCustomer, addLineItem, and getLineItems methods.
The two classes are the primary key class and the entity bean. The primary key class is simply a class whose data members represent the full key of the entity. For the order entity, the order number is the full key, so the OrderPK class in Figure 4 is composed of but one attribute: an Integer order number. The entity bean class, OrderBean, is where the code for the Order and OrderHome interfaces is implemented. Figure 5 lists the methods of the OrderBean class minus their code implementations (which are beyond the scope of this article).
It is important to note that the client programmer will not be using the entity beannot directly, that is. The client programmer uses the entity bean through what is known as the remote interface. The remote interface is composed of the business methods of the entity bean. The remote interface to the host-based OrderBean is shown in Figure 3. The entity bean is the Java class that provides the code implementation for the home and remote interfaces.
Bean-managed vs. Container-managed
The entity bean itself or its container can manage the persistent storage of an entity beans attributes. Persistence management by the entity bean is easier for us legacy programmers to understand because we are used to being responsible for implementing the code to create, read, update, and delete the DB2/400 records. In the documentation for EJB, it is almost expected that youll use Java Database Connectivity (JDBC) for database management, but its up to you. You could use IBMs Java Toolbox for the AS/400s record-level access classes, or you could use an OO database management system.
But an EJB strategy that may not be as easy to understand is container managed persistence. Suns preliminary EJB 2.0 specification includes strategies for the automation of RDB access for entity beans. That means you dont have to write any CRUD code; all you have to do is specify, in a control file known as the deployment descriptor, the persistent storage type of JDBC and the file name followed by a table that maps the database field names with the entity beans attributes. Figure 6 shows an example deployment descriptor that you could use with BEAs Weblogic for the specification of container-managed persistence for my Order entity bean.
Client Code
Regardless of which strategy I use to manage persistence, the client code required to access the Order entity bean (shown in Figure 6) remains the same. The Java class, appropriately
called Client, begins by qualifying itself as part of Midrange Computings EJB client package before it imports all of Midrange Computings EJB interface classes:
package com.midrangecomputing.ejb.client;
import com.midrangecomputing.ejb.interfaces.*;
The Client class then imports the Java packages required for the remote access of EJB. The first method of the Client class is called getInitialContext. The code for this method is typical of EJB clients; it specifies the properties of the EJB host, such as the URL of the host and its socket number as well as the user name and the users password.
This particular EJB client application uses a hardcoded order number to retrieve a remote reference to an entity bean. If the order is not found, the application creates one using a hardcoded customer number and due date. All this processing is done in the method called main. The main method begins by creating the object variables that contain the hardcoded values for the order number, customer number, and due date. The main method then creates an instance of the Order primary key class and sets its order number attribute to be the Integer object that contains the hardcoded order number:
OrderPK orderKey = new OrderPK();
orderKey.ordNo = ordNo;
At this point, the client application is ready to retrieve a remote reference to a server instance of the Order entity bean:
Context ctx = getInitialContext();
The client first invokes the getInitialContext method, which specifies to whom it is communicating. Then, it uses the lookup method of the Context class to enable a connection with the servers implementation of the OrderHome interface. Remember that the home interface provides the life-cycle methods of an entity bean; it is with the variable called home that the client application will retrieve or create an order entity:
OrderHome home = (OrderHome) ctx.lookup(ejb.OrderHome);
The client then creates a variable whose type is the Order entity beans remote interface. Remember that the remote interface specifies the business methods for an entity bean:
Order remoteOrder = null;
The client then attempts to retrieve a remote reference to the Order entity bean with the findByPrimaryKey method of the OrderHome interface:
remoteOrder (Order)
home.findByPrimaryKey(orderKey);
But if a reference to the remote order could not be had, the client application creates a new one using the create method of the OrderHome interface:
remoteOrder = home.create(ordNo, custNo, dueDate);
Either way, whether found by primary key or newly constructed, the remoteOrder variable contains a remote reference to the Order entity bean, which means the client application can invoke the methods of the remote interface:
System.out.println(Order......: + ordNo +
Customer..: + remoteOrder.getCustNo() +
Due Date..: + remoteOrder.getDueDate());
The use of the remote invocation of the server-based entity bean for system output may seem overly trivial, but it nonetheless suggests the power of EJB. In a robust EJB application, the remote interface will contain business methods that the client is able to invoke to perform the complex tasks associated with that entity.
Needful Things
If the techniques shown in this order processing example were all that EJB provides, thered be no reason to use this technology. And thered be no reason to pay thousands, or tens of thousands, of dollars for an EJB server. The code that Ive shown for the client side is simply a slight variation of Javas Remote Method Invocation (RMI). So why not just use RMI, which is free? An Internet application that uses the object distribution strategies of RMI will work fine when 10 clients are accessing it. It will probably work fine when you have 20 clients attached, but when you have 100 or 1,000 clients...? RMI applications dont scale well without complex systems programming. We are not systems programmers; we are business programmers. EJB applications scale well because the EJB servers handle performance, using such features as caching, resource pooling, and Least Recently Used (LRU) algorithms for memory management. Furthermore, EJB has sophisticated mechanisms for the management of transactions and commitment controlsomething all complex e-commerce applications will require.
The EJB Interface
The development of EJB applications can become quite complex, but the use of entity beans in client applications is simple. You use the home interface (OrderHome in my example) to control the life cycle of an entity bean. You use the remote interface (Order in my example) to invoke the business methods of an entity bean. You can manage the persistence of entity beans yourself, or you can delegate that responsibility to the container- managed persistence provided by the EJB server. By the end of this year, we will begin to see EJB Internet applications from Integrated Software Vendors (ISVs) that run great on the AS/400. Use of the Enterprise JavaBeans specification will become pervasive because it provides the technologies required for robust Internet e-commerce applications.
package com.midrangecomputing.ejb.interfaces;
import javax.ejb.*;
import java.rmi.RemoteException;
import java.util.*;
public interface OrderHome extends EJBHome {
public Order create(Integer ordNo, Integer custNo, Date dueDate)
throws CreateException, RemoteException;
public Order findByPrimaryKey(OrderPK primaryKey)
throws FinderException, RemoteException;
}
Figure 1: The OrderHome interface provides the life-cycle methods for an entity bean.
import java.rmi.*;
import javax.ejb.*;
public interface EJBHome implements Remote {
void remove(Handle);
void remove(Object);
EJBMetaData getEJBMetaData();
}
Figure 2: The OrderHomes base interface, EJBHome, provides the methods required for the deletion of an entity.
package com.midrangecomputing.ejb.interfaces;
import java.rmi.RemoteException;
import javax.ejb.*;
public interface Order extends EJBObject { public java.util.Date getDueDate()
throws RemoteException;
public Integer getCustNo()
throws RemoteException;
}
Figure 3: The Order interface provides business methods for the manipulation of a business entity.
package com.midrangecomputing.ejb.interfaces;
public class OrderPK implements java.io.Serializable
{ public Integer ordNo; }
Figure 4: The attributes of the OrderPK class correspond to the full key of the RDB Order file.
package com.midrangecomputing.ejb.server;
import com.midrangecomputing.ejb.interfaces.*;
import javax.ejb.*;
import java.util.*;
import java.sql.*;
class OrderBean implements EntityBean {
Integer ordNo;
Integer custNo;
Date dueDate;
OrderBean();
boolean isModified();
String id();
void setModified(boolean);
void ejbActivate();
void setEntityContext(javax.ejb.EntityContext);
void unsetEntityContext();
void ejbPassivate();
void ejbLoad();
OrderPK ejbFindByPrimaryKey(OrderPK orderPK);
void ejbStore();
void ejbRemove();
OrderPK ejbCreate(Integer ordNo, Integer custNo, Date dueDate);
void ejbPostCreate(Integer ordNo, Integer custNo, Date dueDate);
Integer getCustNo();
Date getDueDate();
Connection getConnection();
static static {};
}
Figure 5: The OrderBean class provides code implementations for all the methods of the OrderHome and Order interfaces (shown in Figures 1-3).
(jdbc
tableName Order
(attributeMap
; EJBean attribute Database column name
;ordNo ORDNO
custNo CUSTNO
dueDate DUEDATE
)
)
Figure 6: The Client application uses an EJB remote interface to access the methods of a host-based entity bean.
LATEST COMMENTS
MC Press Online