package examples;import javax.ejb.*; import java.rmi.RemoteException;/** * These are CountBean's business logic methods. * * This interface is what clients operate on when they * interact with EJB objects. The container vendor will * implement this interface; the implemented object is * the EJB object, which delegates invocations to the * actual bean. */ public interface Count extends EJBObject { /** * Increments the int stored as conversational state */ public int count() throws RemoteException; }package examples;import javax.ejb.*;/** * Demonstration Stateful Session Bean. This Bean is initialized * to some integer value, and has a business method which * increments the value. * * This example shows the basics of how to write a stateful * session bean, and how passivation/activation works. */ public class CountBean implements SessionBean {
// The current counter is our conversational state. public int val; // // Business methods // /** * Counts up */ public int count() { System.out.println("count()"); return ++val; } // // EJB-required methods // 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) { } }package examples;import javax.ejb.*; import javax.naming.*; import java.util.Properties;/** * This class is a simple example of client code. * * We create 3 EJB Objects in this example, but we only allow * the container to have 2 in memory. This illustrates how * beans are passivated to storage. */ public class CountClient { public static void main(String[] args) { try { /* * Get System properties for JNDI initialization */ Properties props = System.getProperties(); /* * Get a reference to the Home Object - the * factory for EJB Objects */ Context ctx = new InitialContext(props); CountHome home = (CountHome) javax.rmi.PortableRemoteObject.narrow( ctx.lookup("CountHome"), CountHome.class); /* * An array to hold 3 Count EJB Objects */ Count count[] = new Count[3]; int countVal = 0; /* * Create and count() on each member of array */ System.out.println("Instantiating beans..."); for (int i=0; i < 3; i++) { /* * Create an EJB Object and initialize * it to the current count value. */ count[i] = home.create(countVal); /* * Add 1 and print */ countVal = count[i].count(); System.out.println(countVal); /* * Sleep for 1/2 second */ Thread.sleep(500); } /* * Let's call count() on each EJB Object to * make sure the beans were passivated and * activated properly. */ System.out.println("Calling count() on beans..."); for (int i=0; i < 3; i++) { /* * Add 1 and print */ countVal = count[i].count(); System.out.println(countVal); /* * Sleep for 1/2 second */ Thread.sleep(500); } /* * Done with EJB Objects, so remove them */ for (int i=0; i < 3; i++) { count[i].remove(); } } catch (Exception e) { e.printStackTrace(); } } } package examples;import javax.ejb.*; import java.rmi.RemoteException;/** * This is the home interface for CountBean. This interface * is implemented by the EJB Server's glue-code tools - the * implemented object is called the Home Object, and serves * as a factory for EJB Objects. * * One create() method is in this Home Interface, which * corresponds to the ejbCreate() method in the CountBean file. */ public interface CountHome extends EJBHome { /* * This method creates the EJB Object. * * @param val Value to initialize counter to * * @return The newly created EJB Object. */ Count create(int val) throws RemoteException, CreateException; } 我调过的没有问题
import java.rmi.RemoteException;/**
* These are CountBean's business logic methods.
*
* This interface is what clients operate on when they
* interact with EJB objects. The container vendor will
* implement this interface; the implemented object is
* the EJB object, which delegates invocations to the
* actual bean.
*/
public interface Count extends EJBObject { /**
* Increments the int stored as conversational state
*/
public int count() throws RemoteException;
}package examples;import javax.ejb.*;/**
* Demonstration Stateful Session Bean. This Bean is initialized
* to some integer value, and has a business method which
* increments the value.
*
* This example shows the basics of how to write a stateful
* session bean, and how passivation/activation works.
*/
public class CountBean implements SessionBean {
// The current counter is our conversational state.
public int val; //
// Business methods
// /**
* Counts up
*/
public int count() {
System.out.println("count()");
return ++val;
} //
// EJB-required methods
// 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) {
}
}package examples;import javax.ejb.*;
import javax.naming.*;
import java.util.Properties;/**
* This class is a simple example of client code.
*
* We create 3 EJB Objects in this example, but we only allow
* the container to have 2 in memory. This illustrates how
* beans are passivated to storage.
*/
public class CountClient { public static void main(String[] args) { try {
/*
* Get System properties for JNDI initialization
*/
Properties props = System.getProperties(); /*
* Get a reference to the Home Object - the
* factory for EJB Objects
*/
Context ctx = new InitialContext(props);
CountHome home = (CountHome)
javax.rmi.PortableRemoteObject.narrow(
ctx.lookup("CountHome"), CountHome.class); /*
* An array to hold 3 Count EJB Objects
*/
Count count[] = new Count[3]; int countVal = 0; /*
* Create and count() on each member of array
*/
System.out.println("Instantiating beans...");
for (int i=0; i < 3; i++) {
/*
* Create an EJB Object and initialize
* it to the current count value.
*/
count[i] = home.create(countVal); /*
* Add 1 and print
*/
countVal = count[i].count(); System.out.println(countVal); /*
* Sleep for 1/2 second
*/
Thread.sleep(500);
} /*
* Let's call count() on each EJB Object to
* make sure the beans were passivated and
* activated properly.
*/
System.out.println("Calling count() on beans...");
for (int i=0; i < 3; i++) { /*
* Add 1 and print
*/
countVal = count[i].count(); System.out.println(countVal); /*
* Sleep for 1/2 second
*/
Thread.sleep(500);
} /*
* Done with EJB Objects, so remove them
*/
for (int i=0; i < 3; i++) {
count[i].remove();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package examples;import javax.ejb.*;
import java.rmi.RemoteException;/**
* This is the home interface for CountBean. This interface
* is implemented by the EJB Server's glue-code tools - the
* implemented object is called the Home Object, and serves
* as a factory for EJB Objects.
*
* One create() method is in this Home Interface, which
* corresponds to the ejbCreate() method in the CountBean file.
*/
public interface CountHome extends EJBHome { /*
* This method creates the EJB Object.
*
* @param val Value to initialize counter to
*
* @return The newly created EJB Object.
*/
Count create(int val) throws RemoteException, CreateException;
}
我调过的没有问题