Day 11

Quiz

A1:

A stateful session bean maintains a conversational state with an EJB client. This means that until the end of its life cycle, the EJB container maintains an association between a stateful bean and the EJB client that it services. A stateless session bean, on the other hand, does not maintain any state information regarding the EJB client. Hence, the EJB container maintains an instance pool of stateless session beans that can be allocated to service any EJB client.

A2:

The ejbCreate() method is normally overloaded and used to initialize the session bean with different values. Because a stateless session bean does not maintain state with an EJB client, there is no need for it to have any overloaded ejbCreate() methods. The default ejbCreate() method with no parameters is sufficient.

A3:

A stateless session bean does not maintain any state. Hence, the EJB container can allocate any instance of a stateless session bean from the bean instance pool to service EJB clients. A stateful session bean needs to be activated and passivated by the EJB container to free up resources because the bean maintains a conversational state with the EJB client, and hence it has the ejbActivate() and ejbPassivate() callback methods. This is not the case with stateless session beans, and hence they do not need to have the ejbActivate() and ejbPassivate() callback methods.

Exercise

A1:

The code listing below illustrates this RMI client. You will first obtain the InitialContext object using "weblogic.jndi.WLInitialContextFactory" for the Context factory. After obtaining the IntialContext object, you will create the home object for your EJB by doing a lookup for it with its JNDI name and then performing a PortableRemoteObject.narrow() on this class. To test whether the object is properly obtained, you will perform a create on the WaiterHome object. To run the client, open a DOS session and include your EJB deployable file in the CLASSPATH of this session. Then run this Java client:

 import java.io.PrintStream;  import weblogic.utils.Debug; import javax.naming.*; import java.util.Hashtable; import javax.rmi.*; import com.sams.learnweblogic7.ejb.bmp.*; public class EJBRMIClient {   public final static String           JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";   static String url = "t3://localhost:7001";   public EJBRMIClient() {}   public static void main(String[] argv) throws Exception {     try {       InitialContext ctx = getInitialContext(url);       Object home = ctx.lookup("Waiter_Home");       WaiterHome myWaiterHome =               (WaiterHome)PortableRemoteObject.narrow(home,               WaiterHome.class);       System.out.println(myWaiterHome.create());     }     catch (Throwable t) {       t.printStackTrace();       System.exit(-1);     }    }   private static InitialContext getInitialContext(String url)        throws NamingException   {     Hashtable env = new Hashtable();     env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);     env.put(Context.PROVIDER_URL, url);     return new InitialContext(env);   } } 



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

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