The Stateful session bean can maintain a status of a call customer, and maintain this state in different ways, because for each concired user, there must be a corresponding stateful session bean, in order to improve the efficiency of the system, Stateful Session Bean can be written to a secondary storage device (such as a hard disk) after a certain customer idle time, and then restore the secondary storage device to the memory after the customer issues a new call request. But under multi-users, the Stateless Session Bean has higher operating efficiency than Stateful Session Bean. The Javax.ejb.EnterpriseBean interface inherits java.io.serializable to implement write read operations. When the EJB container calls the ejbpassivate () method passivates the bean, you can write it to the secondary storage device, and the container calls the ejbactivate () method to activate the bean, and read it from the secondary storage device. Status Bean's passivation process counting Remote interface remote interface defines a business method count (), which will be implemented in the enterprise bean class. Activation State Bean
package com.wiley.compBooks.roman.session.count; 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;} Source Count.java package com.wiley.compBooks.roman.session.count; import javax.ejb *;.. / ** * Demonstration Stateful Session Bean This bean is * Initialized to Some Integer Value and Has A Business * Method That Increments the value. * * this exampl e shows the basics of how to write a stateful * session bean and how passivation / activation works * / public class CountBean implements SessionBean {private SessionContext ctx;. // 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) ""); "ejbcreate ()"); "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) {}} Source CountBean.javabean implements Javax.ejb.SessionBean. So it must define all methods defined by the sessionBean. Ojbcreate () initializes the parameters of VAL. It will be the initial state of the Counter. The VAL variable is protected during passivation and activation of the bean. Count BEAN's Home interface
package com.wiley.compBooks.roman.session.count; 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;} Source Counthome.java.
Counting Bean Configuration Descriptor Count BEAN Configuration Descriptor Counting Bean Environment Property Generates the EJB-JAR file counting bean client code
Package com.wiley.compbooks.roman.session.count; import javax.ejb. *; import javax.naming. *; import java.util.properties; / ** * this class is a simple esample of client code invokes * methods on a simple Stateless Enterprise Bean. * * We create 3 EJB Objects in this example, but we allow * the container to have only 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 * / Source CountClient.java 1, JNDL is required to initialize the context 2. Use the JNDL positioning home interface 3, use the Home object to establish 3 different count EJB objects, so the session 4 is established with three different clients 4, configuration descriptor restriction At the same time, there can be only two beans work, so three beans can be passivated. Print a message when ejbpassivate () is called. 5. Call the count () method on each EJB object, call the ejbactivate () method to activate the bean, which prints a message. 6, the last EJB object is deleted.
Package com.wiley.compbooks.roman.session.count; import javax.ejb. *; import javax.naming. *; import java.util.properties; / ** * this class is a simple esample of client code invokes * methods on a simple Stateless Enterprise Bean. * * We create 3 EJB Objects in this example, but we allow * the container to have only 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) CTX.lookup ("countHome"); / * * 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 (country); / * * 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 around passidid and * activated prot} * / system.out.println ("Calling Count () on beans ..." (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 the * / for (int i = 0; i <3; i ) {count [i] .remove ();}} catch (Exception E) {E.PrintStackTrace ();}}} Source CountClient.java Run Client: For BEA's WebLogic, execution:
Java -djava.naming.factory.initial = WebLogic.jndi.tengahinitialContextFactory -djava.naming.Provider.ur = T3: // localhost: 7001 com.wiley.compbooks.RMan.Session.Count.countClient client output:
Instantiating Beans ... 1 2 3 Calling Count () on Beans ... 2 3 4 Server Output:
ejbCreate () count () ejbCreate () count () ejbCreate () ejbPassivate () count () ejbPassivate () ejbActivate () count () ejbPassivate () ejbActivate () count () ejbPassivate () ejbActivate () count () ejbPassivate ( EJBActivate () Ejbactivate () Ejbremove () ejbremove () ejbremove ()