Java Stateless Session EJB

A Stateless Session EJB

Note: This Example Uses J2EE 1.3

The Remote Interface - RockPaperScissors.java

import javax.ejb.EJBObject;	
import java.rmi.RemoteException;	
	
public interface RockPaperScissors extends EJBObject 
{	 	
   public String getRockPaperOrScissors() 
     throws RemoteException;	
}
To download source right-click here and "Save As...".

The Home Interface - RockPaperScissorsHome.java

import java.rmi.RemoteException;	
import javax.ejb.CreateException;	
import javax.ejb.EJBHome;	
	
public interface RockPaperScissorsHome extends EJBHome 
{	
   RockPaperScissors create() throws RemoteException, 
                                     CreateException;	
}
To download source right-click here and "Save As...".

The Enterprise Bean Class - RockPaperScissorsBean.java

import java.util.Random;
import javax.ejb.SessionBean;	
import javax.ejb.SessionContext;	
	
public class RockPaperScissorsBean implements SessionBean {	
 	
   Random random;   
   //All methods in the Remote interface must be implemented here:	
   public String getRockPaperOrScissors() {	
      if (random == null) {random = new Random();}
      int randomInt = random.nextInt(3);
      if (randomInt == 0) 
        {return "Rock";}
      else if (randomInt == 1) 
        {return "Paper";} 
      else
        {return "Scissors";}        
   }	
	
   public void RockPaperScissorsBean() {}
   
   //the following five methods must be in the Enterprise Bean Class:
   public void ejbCreate() {}	
   //ejbCreate() must match Home Interface's create() method signature
   public void ejbRemove() {}	
   public void ejbActivate() {}	
   public void ejbPassivate() {}	
   public void setSessionContext(SessionContext sessionContext) {}
}
To download source right-click here and "Save As...".

ejb-jar.xml

A Client Application - RockPaperScissorsClient.java

import javax.naming.Context;	
import javax.naming.InitialContext;	
import javax.rmi.PortableRemoteObject;		
	
public class RockPaperScissorsClient {	
	
   public static void main(String[] args) {	
      try {     
          
          Context initialContext = new InitialContext();
          Object initialContextObject = 
            initialContext.lookup(
              "java:comp/env/ejb/SimpleRockPaperScissors");
	  RockPaperScissorsHome rockPaperScissorsHome = 	
             (RockPaperScissorsHome)PortableRemoteObject.narrow(
               initialContextObject, RockPaperScissorsHome.class);
          RockPaperScissors rockPaperScissors = 
            rockPaperScissorsHome.create();
          String theBeansRPS = 
            rockPaperScissors.getRockPaperOrScissors();
          String theApplicationsRPS = "Scissors"; 
            //the application always picks "Scissors";
      
          if (theBeansRPS.equals("Rock") && 
              theApplicationsRPS.equals("Rock")) {
                System.out.println("Tie!");
          } else if (theBeansRPS.equals("Rock") && 
                     theApplicationsRPS.equals("Paper")) {
              System.out.println("The Application wins!");
          } else if (theBeansRPS.equals("Rock") && 
                     theApplicationsRPS.equals("Scissors")) {
              System.out.println("The Bean wins!");
          } else if (theBeansRPS.equals("Paper") && 
                     theApplicationsRPS.equals("Rock")) {
              System.out.println("The Bean wins!");
          } else if (theBeansRPS.equals("Paper") && 
                     theApplicationsRPS.equals("Paper")) {
              System.out.println("Tie!");
          } else if (theBeansRPS.equals("Paper") && 
                     theApplicationsRPS.equals("Scissors")) {
              System.out.println("The Application wins!");
          } else if (theBeansRPS.equals("Scissors") && 
                     theApplicationsRPS.equals("Rock")) {
              System.out.println("The Application wins!");
          } else if (theBeansRPS.equals("Scissors") && 
                     theApplicationsRPS.equals("Paper")) {
              System.out.println("The Bean wins!");
          } else if (theBeansRPS.equals("Scissors") && 
                     theApplicationsRPS.equals("Scissors")) {
              System.out.println("Tie!");
          } else  {System.out.println("Someone didn't choose!");}
          
          System.out.println("The Bean chose " + theBeansRPS + 
                             ", while the Application chose " + 
                             theApplicationsRPS + ".");                
                  
          rockPaperScissors.remove();	//clean up
      
       } catch (Exception exception) {	
          System.err.println("Exception!");	
          exception.printStackTrace();	
       }          
   } 	
}
To download source right-click here and "Save As...".

application-client.xml

A Client JSP - RockPaperScissorsJSP.jsp

To download source right-click here and "Save As...".

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 and may or may not be accurate.
Comment by archive on 2006-05-02 Rate this Comment

In an effort to learn ejbs i scrapped your examples of the html page and created a tgz of them in which each bean type has a different sub-directory.

If you want you can put a link to my sight.
http://www.zingaretti.com/technical_corner/technical_corner.jsp

As i make the consumable by jboss i will updated the tgz file.

 
Sign in to comment on Java Stateless Session EJB.