. Java . Java Stateful Session EJB

Java Stateful Session EJB

Note: This Example Uses J2EE 1.3

The Remote Interface - SayMyName.java

import javax.ejb.EJBObject;	
import java.rmi.RemoteException;	
	
public interface SayMyName extends EJBObject 
{	 	
   public String getMyName() throws RemoteException;
   public int getCount() throws RemoteException;   
   public void setMyName(String nameIn) throws RemoteException;
}
download source, use right-click and "Save Target As..." to save with a .java extension.

The Home Interface - SayMyNameHome.java

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface SayMyNameHome extends EJBHome {
   SayMyName create() throws RemoteException, CreateException;
}
download source, use right-click and "Save Target As..." to save with a .java extension.

The Enterprise Bean Class - SayMyNameBean.java

import javax.ejb.SessionBean;	
import javax.ejb.SessionContext;	
	
public class SayMyNameBean implements SessionBean {	
 	
   String myName;   
   int countNameCalled;
   //All methods in the Remote interface must be implemented here:
   public String getMyName() {
      if (++countNameCalled > 10) {
         setMyName("ok");
      }
      return myName;
   }
   
   public int getCount() {
      return countNameCalled;
   }
   
   public void setMyName(String nameIn) {
      myName = nameIn;
      countNameCalled = 0;
   }   
   
   public void SayMyNameBean() {}
   
   //the following five methods must be in the Enterprise Bean Class:
   public void ejbCreate() {
      countNameCalled = 0;
   }
   //ejbCreate() must match Home Interface's create() method signature
   public void ejbRemove() {}
   public void ejbActivate() {}
   public void ejbPassivate() {}
   public void setSessionContext(SessionContext sessionContext) {}
}
download source, use right-click and "Save Target As..." to save with a .java extension.

A Client Application - SayMyNameClient.java

import javax.naming.Context;	
import javax.naming.InitialContext;	
import javax.rmi.PortableRemoteObject;	
	
public class SayMyNameClient {
	
   public static void main(String[] args) {	

      try {       
          Context initialContext = new InitialContext();
          Object initialContextObject = 
            initialContext.lookup("java:comp/env/ejb/SimpleSayMyName");
	  SayMyNameHome sayMyNameHome = 	
            (SayMyNameHome)PortableRemoteObject.narrow(
              initialContextObject, SayMyNameHome.class);	
          SayMyName sayMyName = sayMyNameHome.create();
          sayMyName.setMyName("Michelle");
	
          String myName = " ";
          int myCount = 0;
      
          while (!myName.equals("ok"))
          { 
             myName = sayMyName.getMyName();   
               //calls stateful session bean which remembers name, 
               //  counts times called
             myCount = sayMyName.getCount();
             System.out.println("Your name is " + 
                                 myName + 
                                " (#" + myCount + ")");
          }  

          sayMyName.remove();	//clean up
      

          } catch (Exception exception) {	
             System.err.println("Exception!");	
             exception.printStackTrace();	
          }      
   } 	
}
download source, use right-click and "Save Target As..." to save with a .java extension.

application-client.xml

A Client JSP - SetMyNameJSP.jsp (run first, then follow link to SayMyNameJSP.jsp)

download source, use right-click and "Save Target As..." to save with a .jsp extension.

A Client JSP - SayMyNameJSP.jsp (can run multiple times after SetMyNameJSP.jsp)

download source, use right-click and "Save Target As..." to save with a .jsp extension.

web-app.xml

ejb-jar.xml

References

online

J2EE (TM) 1.3 Tutorial from Sun

Books

Enterprise Java Beans by Richard Monson-Haefel
Mastering Enterprise JavaBeans by Ed Roman
Comments Comments are left by visitors to FluffyCat.com, are not endorsed by FluffyCat.com, and may or may not be accurate.
Comment by Larry Rate this Comment

Good catch, looks like I completely neglected to add the ejb-jar.xml file to this example. As you said, the ejb-jar file does have a session-type Stateful tag.

I see what you mean about jspInit being run only once. Maybe the bean initialization should be moved out of there. I had this example working on a machine two laptops and a desktop ago, so it will probably be a while before I can work on any improvements.

Thanks for the great questions!

Comment by archive Rate this Comment

Hi Larry,

I was trying to get my head around your example above.

1. I thought that a session ejb had to have a

<session-type>Stateful</session-type>

tag in the ejb-jar.xml file to make it stateful.

2. It looks like the example puts a reference to the remote interface into the session, this is then available to SayMyName.jsp for the rest of the session.

But because jspInit is only run once no matter who invokes the jsp, all users will be saving a reference to the same remote interface in their sessions.

Please could you clarify these 2 points.

Comment by Larry Rate this Comment

I had to break the href at the end of SetMyNameJSP.jsp into a two part string because search engines were trying to follow the link, and coming up with an error. If you were coding a JSP to actually be used instead of having it's source displayed this wouldn't be necessary.

Sign In
to add your own comment