3 Replies Latest reply on May 11, 2006 5:09 AM by Dimitris Andreadis

    Stateless is not Stateless ?

    Feris Thia Newbie

      Hi All,

      Just curious with JBoss Stateless Session Bean. I currently have jboss-4.0.3SP1 running as my application server. Then I try to deploy a simple stateless session bean.

      I have 2 interfaces (home and remote) and the bean class as follows

       //PlayerHome.java
      
       package jndi.bean;
      
       import java.rmi.RemoteException;
      
       import javax.ejb.CreateException;
       import javax.ejb.EJBHome;
      
       public interface PlayerHome extends EJBHome {
       public Player create() throws RemoteException, CreateException;
       }
      



       //Player.java
       package jndi.bean;
      
       import java.rmi.RemoteException;
      
       import javax.ejb.EJBObject;
      
       public interface Player extends EJBObject {
       public String getPlayerID() throws RemoteException;
      
       public void setPlayerID(String playerid) throws RemoteException;
       }
      



       //PlayerBean.java
       package jndi.bean;
      
       import java.rmi.RemoteException;
       import java.util.*;
      
       import javax.ejb.EJBException;
       import javax.ejb.SessionBean;
       import javax.ejb.SessionContext;
      
       public class PlayerBean implements SessionBean {
       private String playerID;
       private String position;
       private String playerName;
       private int salary;
      
       Collection objCollection;
      
       public String getPlayerID()
       {
       return playerID;
       }
      
       public void setPlayerID(String playeridvalue)
       {
       playerID = playeridvalue;
       }
      
       public String getPlayerName() {
       return playerName;
       }
      
       public void setPlayerName(String playerName) {
       this.playerName = playerName;
       }
      
       public String getPosition() {
       return position;
       }
      
       public void setPosition(String position) {
       this.position = position;
       }
      
       public int getSalary() {
       return salary;
       }
      
       public void setSalary(int salary) {
       this.salary = salary;
       }
      
       public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {
       // TODO Auto-generated method stub
      
       }
      
       public void ejbCreate() {
      
       }
      
       public void ejbRemove(){
       // TODO Auto-generated method stub
      
       }
      
       public void ejbActivate() throws EJBException, RemoteException {
       // TODO Auto-generated method stub
      
       }
      
       public void ejbPassivate() throws EJBException, RemoteException {
       // TODO Auto-generated method stub
      
       }
       }
      
      


      and the ejb-jar.xml deployment descriptor

       <?xml version="1.0" encoding="UTF-8"?>
       <ejb-jar id="ejb-jar_ID" version="2.1"
       xmlns="http://java.sun.com/xml/ns/j2ee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
       <description>jBoss test application</description>
       <display-name>Player Test</display-name>
       <enterprise-beans>
       <session>
       <ejb-name>PlayerBean</ejb-name>
       <home>jndi.bean.PlayerHome</home>
       <remote>jndi.bean.Player</remote>
       <ejb-class>jndi.bean.PlayerBean</ejb-class>
       <session-type>Stateless</session-type>
       <transaction-type>Bean</transaction-type>
       </session>
       </enterprise-beans>
       </ejb-jar>
      
      


      and all the code deployed successfully....

      Then I try to access the ejb from a client

       //PlayerTest.java
       import java.util.*;
      
       import javax.naming.InitialContext;
       import javax.rmi.PortableRemoteObject;
      
       import jndi.bean.*;
      
       public class PlayerTest {
      
       public static void main(String[] args) {
      
       try
       {
       Properties prop = new Properties();
       prop.setProperty("java.naming.factory.initial",
       "org.jnp.interfaces.NamingContextFactory");
       prop.setProperty("java.naming.provider.url",
       "192.168.100.34:1099");
      
       InitialContext ctx = new InitialContext(prop);
      
       Object ref = ctx.lookup("PlayerBean");
      
       PlayerHome myHome = (PlayerHome) PortableRemoteObject.narrow(ref,PlayerHome.class);
       Player myplayer = (Player) myHome.create();
      
       myplayer.setPlayerID("J-001");
      
       System.out.println(myplayer.getPlayerID());
       }
       catch(Exception e)
       {
       e.printStackTrace();
       }
      
       }
       }
      


      it has the output of :

       J-001
      


      Then.... I try to call it without the setter method ... so I remove the following code from PlayerTest.java

       myplayer.setPlayerID("J-001");
      


      What I expected is a null value. But then it comes out "J-001" again. I try it again from another client.... still got "J-001".

      Why is it happening ? Is there something wrong about my code ? Why JBoss seeems to cache the Stateless ? As I knows, stateless session bean will be removed after we finished using it. Can somebody give me any clue ?

      Regards,

      Feris

        • 1. Re: Stateless is not Stateless ?
          Dimitris Andreadis Master

          You haven't undertood the notion of statelessness in EJBs. Stateless doesn't mean that it cannot maintain state. But it means that is shouldn't expose it (like your getters/setters). It also means that the server is allowed to create a number of them (a pool) to serve client requests and will just reuse one of them whenever a call comes in.

          Since your client is only one it just happens that it gets to access the same physical bean in the 2nd invocation. If you had 10 concurrent clients each one maybe accessing a different physical instance on every invocation. And bean.remove normally has no effect.

          • 2. Re: Stateless is not Stateless ?
            Feris Thia Newbie

            Thanks Dimitri,

            If I'm not supposed to exposed setter/getter ... what is the point of statelessness ? What is it different exactly with statefullness ?

            I just know that now I have completely wrong conception about these (statefull and stateless). If there's any resources in Internet that can make me clear about these ?

            Regards,

            Feris

            • 3. Re: Stateless is not Stateless ?
              Dimitris Andreadis Master

              "Mastering Enterprise JavaBeans", is freely available in .pdf and explains the lifecycle of EJBs.

              The typical stateful session bean is the shoping cart, representing a user session, holding the books you buy.

              An example stateless session bean is one that every method implements a small self-contained workflow operation (e.g. transfer money from acount A to account B).