Testing the Life Cycle of Stateful Session Beans


In this section, you leverage the skills you have already learned by developing the HelloWorld stateless session bean, but this time you develop a stateful session bean that you influence to go through its life cycle states.

The stateful session bean you're developing is named "Passivation" and consists of the following class files:

  • examples.Passivation (remote interface)

  • examples.PassivationHome (home interface)

  • examples.PassivationBean ( Implementation class)

You'll also create examples.PassivationClient , the test client class file.

The following list describes how you influence this stateful bean to transition through its life cycle states:

  • The test client will create multiple instances (50, for instance) of the Passivation stateful session bean.

  • Each instance of the stateful bean maintains a conversation with the client by incrementing a simple counter and returning the value to the client thread.

  • You cause an instance of the stateful bean to be idle for a while by sleeping the client thread for a short period.

  • You modify the <idle-timeout-seconds> and <max-beans-in-cache> elements in the weblogic-ejb-jar.xml file to cause the EJB container to passivate and activate the stateful bean instances.

To develop this example, follow these steps:

  1. Set up a new working directory and subdirectory structure, using the same structure as the earlier HelloWorld example.

  2. Create the following class files and save them into the working /src directory:

    • Use Listing 20.10 to create the home interface for a stateful bean.

    Listing 20.10 The Home Interface of the Passivation Stateful Bean
     package examples; import javax.ejb.*; import java.rmi.RemoteException; public interface PassivationHome extends EJBHome { Passivation create(int val) throws RemoteException, CreateException; } 
    • Use Listing 20.11 to create the remote interface for the stateful bean.

    Listing 20.11 The Remote Interface of the Passivation Stateful Bean
     package examples; import javax.ejb.*; import java.rmi.RemoteException; public interface Passivation extends EJBObject { public int count() throws RemoteException; } 
    • Use Listing 20.12 to create the Implementation class for the stateful bean.

    Listing 20.12 The Implementation Class for the Passivation Stateful Bean
     package examples; import javax.ejb.*; public class PassivationBean implements SessionBean { // The current counter will be the conversational state. 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) { } } 
    • Use Listing 20.13 to create the test client for the stateful bean.

    Listing 20.13 The Test Client for the Passivation Stateful Bean
     package examples; import java.rmi.*; import javax.rmi.PortableRemoteObject; import javax.naming.*; import java.util.*; public class PassivationClient { private static Context createJNDIContext() throws NamingException, RemoteException { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); env.put(Context.PROVIDER_URL,"t3://localhost:7001"); Context context = new InitialContext( env ); return context; } public static void main(String[] args) { try { // use JNDI to look up the home interface for HelloWorld Context context = createJNDIContext(); PassivationHome home = (PassivationHome ) PortableRemoteObject.narrow( context.lookup("examples.PassivationEJB"), examples.PassivationHome.class ); /* * An array to hold 50 count EJB objects */ Passivation count[] = new Passivation[50]; int countVal = 0; /* * Create and count() on each member of the array */ System.out.println("Instantiating Stateful Beans..."); for (int i=0; i < 50; i++) { /* * Create an EJBObject and initialize * it to the current count value. */ count[i] = home.create(countVal); countVal = count[i].count(); System.out.println(countVal); /* * Sleep for 1 second */ Thread.sleep(1000); } /* * Call count() on each EJB object to * ensure the beans are activated if in a passivated state */ System.out.println("Calling count() on beans..."); for (int i=0; i < 50; i++) { /* * Add 1 and print */ countVal = count[i].count(); System.out.println(countVal); /* * Sleep for 1 second */ Thread.sleep(1000) ; } /* * Remove objects */ for (int i=0; i < 50; i++) { count[i].remove(); } } catch (Exception e) { e.printStackTrace(); } } } 
  3. Use Listing 20.14 to create the ejb-jar.xml deployment descriptor and save it in the working /deployment directory.

    Listing 20.14 The ejb-jar.xml Deployment Descriptor for the Passivation Stateful Bean
     <!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'> <!-- Generated XML! --> <ejb-jar> <enterprise-beans> <session> <ejb-name>Passivation</ejb- name > <home>examples.PassivationHome</home> <remote>examples.Passivation</remote> <ejb-class>examples.PassivationBean</ejb-class> <session-type>  Stateful  </session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> </ejb-jar> 

    What differentiates a stateless and stateful session bean at deployment is how the bean is defined in the weblogic-ejb-jar.xml deployment descriptor using the <session-type> element.

  4. Use Listing 20.15 to create the weblogic-ejb-jar.xml deployment descriptor and save it in the working /deployment directory.

    Listing 20.15 The weblogic-ejb-jar.xml Deployment Descriptor for the Passivation Stateful Bean
     <!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB//EN' 'http://www.bea.com/servers/wls700/dtd/weblogic-ejb-jar.dtd'> <!-- Generated XML! --> <weblogic-ejb-jar> <weblogic-enterprise-bean> <ejb-name>Passivation</ejb-name> <stateful-session-descriptor> <stateful-session-cache> <max-beans-in-cache>  45  </max-beans-in-cache> <idle-timeout-seconds>  500  </idle-timeout-seconds> <cache-type>  LRU  </cache-type> </stateful-session-cache> <stateful-session-clustering> </stateful-session-clustering> </stateful-session-descriptor> <transaction-descriptor> </transaction-descriptor> <jndi-name>examples.PassivationEJB</jndi-name> </weblogic-enterprise-bean> </weblogic-ejb-jar> 

    The elements of the weblogic-ejb-jar.xml deployment descriptor file that can influence passivation of a stateful session bean are highlighted in Listing 20.15. You can modify these values, however, for the initial test; use the specified values.

  5. Compile and build the Passivation.jar deployment unit using a build script (provided in Listing 20.6).

  6. Deploy the stateful bean to an active WebLogic server by copying the Passivation.jar file into your WebLogic domain's applications directory.

To test your Passivation stateful session bean, follow these steps:

  1. Open a command-prompt window and set your Java environment.

  2. Set the CLASSPATH to include the location of the Passivation.jar file, as shown in this statement:

       
      Set CLASSPATH=Passivation.jar;%CLASSPATH%  
  3. Run the application using the following command, as shown in Figure 20.10:

    Figure 20.10. Run the test Java client against the Passivation session bean.

    graphics/20fig10.gif

       
      java examples.PassivationClient  

As shown in Figure 20.10, the EJB container is passivating and activating the stateful bean. You can also use the Administration Console to monitor the activity and status of your deployed stateful session beans (see Figure 20.11).

Figure 20.11. Monitor the cache, activation, and passivation activity of a deployed session bean from the Administration Console.

graphics/20fig11.jpg



BEA WebLogic Platform 7
BEA WebLogic Platform 7
ISBN: 0789727129
EAN: 2147483647
Year: 2003
Pages: 360

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net