Software Development

Transitioning into OOP: Implement a simple session Enhanced JavaBean

Get familiar with J2EE development in an object-oriented environment with this tutorial on creating an Enhanced JavaBean simple session.

Our last article explored how we can implement client programs to connect with RMI servers and how we generate the runtime deployment classes for these clients. In this article, we return to our discussion of Enhanced JavaBeans (EJBs) to help to lay to rest the fears you may have about building your first EJBs and to help you venture into the world of EJB development.

Install the EJB runtime and development environment
As we discussed in our last EJB article, an EJB must be executed inside of an EJB container. Therefore, we must install a J2EE-compliant EJB container. To help us deploy our EJB, we also need a deployment tool. The J2EE reference implementation from Sun includes an EJB container and an EJB deployment tool. The J2EE reference implementation can be found in the J2EE development kit from Sun.

The J2EE development kit relies on the Java 2 SDK, Standard Edition (J2SE) development kit, so you will need to download it as well. You need the J2SE SDK to run the reference implementation J2EE server and to build and run EJB applications.

Once you have downloaded the development kits, install them and select your desired location for each kit. Define an environment variable named JAVA_HOME and point it to the install directory for the J2SE development kit. Next, define an environment variable named J2EE_HOME and point it to the install directory for the J2EE development kit.

Now, you are ready to begin writing your EJB.

Writing a simple session EJB
Developing an EJB involves most of the same steps and concepts that we have discussed for developing plain-old Java objects (POJOs), with a few slight differences. These are the four basic steps to develop and deploy an EJB:
  1. Write the classes and interfaces for your EJB.
  2. Write a deployment descriptor for your EJB.
  3. Package the EJB and associated files inside a jar file.
  4. Deploy the EJB.

There are two different types of session beans. Before we start coding, let's look at their similarities and differences.

An overview of stateful session EJBs
A stateful session EJB maintains a conversational state with one client for the duration of a single session. This implies that the stateful session EJB can maintain instance variables across multiple invocations from one client during a single session.

Once the client finishes interacting with the EJB and the EJB container removes the EJB, the session for the EJB ends and all state data for the EJB is discarded.

An overview of stateless session EJBs
A stateless session EJB does not maintain a conversational state for each individual client. Each invocation of a stateless session EJB should be considered as a request to a brand new object instance, since any instance-variable state will be lost between invocations.

Stateless session EJBs are not persisted to secondary storage by the EJB container, so a programmer must recognize that all data is transient between invocations for each client. The transient nature of stateless session EJBs allows an EJB container to reuse EJB instances and, therefore, usually optimize the performance of the EJBs.

Session Bean relationships
Figure A illustrates the relationships that exist between an EJB client, an EJB container, and a couple of session EJBs.

Figure A
Session Bean relationships


A session EJB is composed of a minimum of three mandatory classes/interfaces. The first mandatory class/interface is the Component interface. The Component interface defines the business methods visible to an EJB client and it must extend javax.ejb.EJBObject.

As an example, we will implement a very simple stateless session EJB that returns an account balance in a method named getBalance. The code for our Component interface is as follows:
package com.jeffhanson.ejb;

public interface Account extends javax.ejb.EJBObject, java.rmi.Remote
{
   public String getBalance() throws java.rmi.RemoteException;
}


The second mandatory class/interface is the Home interface. The Home interface is used by an EJB client to obtain a reference to a bean's component interface and it must extend javax.ejb.EJBHome. The code for our Home interface is as follows:
package com.jeffhanson.ejb;

public interface AccountHome extends javax.ejb.EJBHome
{
   public Account create() throws javax.ejb.CreateException,
                                  java.rmi.RemoteException;
}


The third mandatory class/interface is the Enterprise Bean class. The Enterprise Bean class supplies the implementation for the Component interface and the Home interface. The Enterprise Bean class for a session EJB must implement the javax.ejb.SessionBean interface. The code for our Enterprise Bean class is as follows:
package com.jeffhanson.ejb;

public class AccountBean
   implements javax.ejb.SessionBean
{
   private javax.ejb.SessionContext ctx;
 
   public void ejbActivate() {}
 
   public void ejbCreate() {}

   public void ejbRemove() {}
 
   public void ejbPassivate() {}
 
   public void setSessionContext(javax.ejb.SessionContext ctx)
   {   
      this.ctx = ctx;
   }
 
   // business method
   public String getBalance()
      throws java.rmi.RemoteException
   {
      String balance = dataObj.getAccountBalance();
      return(balance);
   }
}


Walk through
In this article, we attempted to ease any fears you may have about building your first EJBs by walking you through the implementation details of a simple stateless session EJB. The next article will explore the details of entity beans and how they are used to encapsulate data entities to be accessed by business objects.
0 comments

Editor's Picks