Example 1: Developing of Stateless Session Bean

The purpose

To develop a component capable of basic mathematic calculations (addition and multiplication).

Decision of the bean type

Since the required operations are idempotent and no state has to be maintained, the stateless session bean is sufficient.

Development steps

  1. First, define the remote interface with the bussiness logic methods:
       // File Comp.java
    
       public interface Comp extends javax.ejb.EJBObject {
         int add(int a, int b) throws java.rmi.RemoteException;
         int multiply(int a, int b) throws java.rmi.RemoteException;
       }
       
  2. Next, create the home interface. Since the bean is stateless, only one create() method with no arguments is allowed:
       // File CompHome.java
    
       public interface CompHome extends javax.ejb.EJBHome {
         Comp create()
           throws java.rmi.RemoteException, javax.ejb.CreateException;
       }
       
  3. Implement the actual bean class with the required bussiness logic:
       // File CompBean.java
    
       public class CompBean implements javax.ejb.SessionBean {
    
         private javax.ejb.SessionContext _context;
    
         public void setSessionContext(javax.ejb.SessionContext sessionContext) {
         _context = sessionContext;
         }
    
         public void ejbCreate() throws java.rmi.RemoteException { }
    
         public int add(int a, int b) throws java.rmi.RemoteException
         { return a + b; }
    
         public int multiply(int a, int b) throws java.rmi.RemoteException
         { return a * b; }
    
         public void ejbRemove() { }
         public void ejbActivate() { }
         public void ejbPassivate() { }
         public String toString() 
          { return "CompBean[context=" + _context + "]"; }
    
       }
       

    Note that the most of the notification-processing methods are empty. This is because no functionality is required in this case there.

  4. The client application which uses the bean could look like the following:
       // File CompClient.java
    
       public class CompClient {
      
         public static void main(String[] args) throws Exception {
    
           // get a JNDI context using the Naming service
           javax.naming.Context context = new javax.naming.InitialContext();
    
           Object ref = context.lookup("myCompBean");
           CompHome home = 
             (CompHome) javax.rmi.PortableRemoteObject.narrow(ref, CompHome.class);
    
           Comp c = home.create();
           System.out.println("2 + 3 = " + c.add(2,3));
           System.out.println("2 * 3 = " + c.multiply(2,3));
           c.remove();
         }
       }
       
  5. Write the XML deployment descriptor and specify the bean name, home and remote interface, the bean class and the bean type
    
    < ?xml version="1.0"? >
    
    < !DOCTYPE ejb-jar 
      PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN" 
             "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd" >
    
    < ejb-jar >
        < description >
            This is Mour's example of a Stateless session bean
        < /description >
        < enterprise-beans >
            < session >
                < description >
                    This Stateless session bean implements a simple Computer
                < /description >
                < ejb-name > comp < /ejb-name >
                < home > CompHome < /home >
                < remote > Comp < /remote >
                < ejb-class > CompBean < /ejb-class >
                < session-type > Stateless < /session-type >
            < /session >
        < /enterprise-beans >
    < /ejb-jar >
    
  6. Generate supporting classes to deploy the bean to the container (the commands presented here are specific to Inprise Application Server 4)
        vbjc CompHome.java
        java2iiop -compile CompHome
      
  7. Create META-INF directory and put the development descriptor(s) there
  8. Compile all previously generated container classes, the remote interface and the bean class
      vbjc *.java 
  9. Pack the home and remote interface, the bean class and generated classes to the jar file, together with the deployment descriptor
     jar cMf comp_bean.jar META-INF *.class 
  10. Verify the correctness of the file to be deployed
     vbj com.inprise.ejb.util.Verify comp_bean.jar
      
  11. Run the container implementation, passing it the bean to be deployed to the container
     vbj com.inprise.ejb.Container ejbcontainer comp_bean.jar -jts -jns -jss
     
First Previous Next

Slide: 22