Porting EJB2.1 Statefull Session Bean to EJB3
adrian_p Jun 29, 2005 4:10 PMHello,
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
 
    