3 Replies Latest reply on Jun 30, 2005 2:43 PM by bill.burke

    Porting EJB2.1 Statefull Session Bean to EJB3

    adrian_p

      Hello,

      I'm currently trying to port some tutorial bean into EJB3. I have to mention that i'm a beginner.

      The bean i want to port is showing the instantiate, pasivate, activate mechanism in statefull session beans.

      It's a very simple bean and client... My problem is related to ejbCreate method and it's related create method in old EJB2.1 home interface.
      I don't know how to do this in EJB3. I've looked everywhere. Also i've read a couple of times the referring section in EJB3 Public Review Core Documentation but i've found nothing.

      Here's the code i want to port:

      CountHome.java

      package examples;
      import javax.ejb.*;
      import java.rmi.RemoteException;
      
      public interface CountHome extends EJBHome {
      
       Count create(int val) throws RemoteException, CreateException;
      }
      


      Count.java

      package examples;
      
      import javax.ejb.*;
      import java.rmi.RemoteException;
      
      public interface Count extends EJBObject {
       public int count() throws RemoteException;
      }
      



      CountBean.java
      package examples;
      import javax.ejb.*;
      
      public class CountBean implements SessionBean {
      
       public int val;
      
       public int count() {
       System.out.println("count()");
       return ++val;
       }
      
       public void ejbCreate(int val) throws CreateException {
       this.val = val;
       System.out.println("ejbCreate()");
       }
      
       public void ejbRemove() {
       System.out.println("ejbRemove()");
       }
      
       public void ejbActivate() {
       System.out.println("ejbActivate()");
       }
      
       public void ejbPassivate() {
       System.out.println("ejbPassivate()");
       }
      
       public void setSessionContext(SessionContext ctx) {
       System.out.println("setSessionContext()");
       }
      }




      CountClient.java
      package examples;
      
      import javax.ejb.*;
      import javax.naming.*;
      import java.util.Properties;
      
      
      public class CountClient {
      
       public static final int noOfClients = 3;
      
       public static void main(String[] args) {
      
       try {
      
       Properties props = System.getProperties();
      
       Context ctx = new InitialContext(props);
       CountHome home = (CountHome)javax.rmi.PortableRemoteObject.narrow(ctx.lookup("CountHome"), CountHome.class);
      
       Count count[] = new Count[noOfClients];
      
       int countVal = 0;
       System.out.println("Instantiating beans...");
       for (int i=0; i < noOfClients; i++) {
       count = home.create(countVal);
       countVal = count.count();
       System.out.println(countVal);
       Thread.sleep(500);
       }
      
       System.out.println("Calling count() on beans...");
       for (int i=0; i < noOfClients; i++) {
       countVal = count.count();
       System.out.println(countVal);
       Thread.sleep(500);
       }
       for (int i=0; i < noOfClients; i++) {
       count.remove();
       }
       } catch (Exception e) {
       e.printStackTrace();
       }
       }
      }



      I hope someone can help me with this...

      Thanks a lot in advance...
      Adrian

        • 1. Re: Porting EJB2.1 Statefull Session Bean to EJB3
          bill.burke

          Turn your ejbCreate method into a business interface method. EVery time you look up a SFSB within JNDI a new session is created for you. Just invoke the new intialize method you create before you continue doing the rest of your business logic.

          • 2. Re: Porting EJB2.1 Statefull Session Bean to EJB3
            adrian_p

            Thanks Bill,

            I think I've tried that but i'll do it again. I hope this time will work.
            I was searching for a solution to this problem and i've found some oracle tutorials related to making an EJB3 bean compatible with EJB2.1 clients

            They were using an @RemoteHome annotation from javax.ejb.RemoteHome.
            It was used like this

            @RemoteHome(home="example.CartHome",remote="example.Cart")

            i've found it also in Jboss EJB3 implementation in org.jboss.annotation.ejb.RemoteHome

            Please, can you tell me how to use it? I think that jboss implementation is different from the one in the mentioned oracle tutorial.


            Thanks in advance

            • 3. Re: Porting EJB2.1 Statefull Session Bean to EJB3
              bill.burke

              Yes, I believe we support @RemoteHome

              You have to use it in conjection with the @Init annotation. So, youre ejbCreate method must be annotated with @Init and

              The jBoss one is different because we actually follow the spec I believe...