Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Session Beans -) stateless -) stateful Session Beans A session bean represents a single client inside the J2EE server. To access an application that is deployed on the server, the client invokes the session bean's methods. The session bean performs work for its client, shielding the client from complexity by executing business tasks inside the server. At any given time, only one client has access to the bean instance. The state (i.e. the values of the instance variables) of the bean is not persistent, existing only for a short period of time. A session bean can be stateful or stateless. Stateful session Beans In a stateful session bean, the instance variables represent the state of a unique client-bean session. This state is often called the conversational state. The state is retained for the duration of the client-bean session. If the client removes the bean or terminates, the session ends and the state disappears. Stateless session Beans A stateless session bean does not maintain a conversational state for a particular client. When a client invokes the method of a stateless bean, the bean's instance variables may contain a state, but only for the duration of the invocation. When the method is finished, the state is no longer retained. Stateless vs. stateful session Beans All instances of a stateless bean are equivalent, allowing the EJB container to assign an instance to any client. => Stateless session beans can support multiple clients, and offer better scalability for applications that require large numbers of clients. Typically, an application requires fewer stateless session beans than stateful session beans to support the same number of clients. Stateless vs. stateful session Beans The EJB container can write a stateful session bean to secondary storage. However, stateless session beans are never written to secondary storage. Therefore, stateless beans may offer better performance than stateful beans. Stateful session Beans – examples of use •The bean needs to hold information about the client across method invocations. •The bean mediates between the client and the other components of the application, presenting a simplified view to the client. •Behind the scenes, the bean manages the work flow of several enterprise beans. Stateless session Beans – examples of use •In a single method invocation, the bean performs a generic task for all clients. For example, you might use a stateless session bean to send an e-mail that confirms an online order. •The bean fetches from a database a set of read-only data that is often used by clients. Stateless Example Stateless Example: Euro Converter Stateless example – The Component (Remote) Interface package statelessDemo; import java.rmi.*; import javax.ejb.*; public interface Converter extends EJBObject { public double convert(int lire); } Stateless example – The Home Interface package statelessDemo; import java.rmi.*; import javax.ejb.*; public interface ConverterHome extends EJBHome { public Converter create() throws RemoteException, CreateException; } Stateless example – The bean package statelessDemo; import java.rmi.*; import javax.ejb.*; public class ConverterBean implements SessionBean { private SessionContext sessionContext; public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; } private static double ratio=1936.27; public double convert(int lire) { return lire/ratio; } } Stateless example – The descriptor <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd"> <ejb-jar> <description>A simple demo of a stateless Bean</description> <display-name>An Euro Converter Bean</display-name> <enterprise-beans> <session> <description>A converter from Lire to Euro</description> <ejb-name>EuroConverter</ejb-name> <home>statelessDemo.ConverterHome</home> <remote>statelessDemo.Converter</remote> <ejb-class>statelessDemo.ConverterBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> Stateless example – The descriptor <assembly-descriptor> <container-transaction> <method> <ejb-name>EuroConverter</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> <ejb-client-jar /> </ejb-jar> Stateless example – The client import javax.ejb.*; import javax.naming.InitialContext; public class ConverterClient { public ConverterClient() { } Get naming context public static void main(String[] args) { and object reference try { InitialContext ctx=new InitialContext(); JNDI name Object objref=ctx.lookup("L2EConverter"); statelessDemo.ConverterHome home = Cast to correct type (statelessDemo.ConverterHome) javax.rmi.PortableRemoteObject.narrow( objref,statelessDemo.ConverterHome.class); statelessDemo.Converter bean=home.create(); Get a bean instance from container Stateless example – The client int lire=100000; Do your business System.out.println(lire+" Lire = "+ bean.convert(lire)+" Euro"); } catch (javax.naming.NamingException ex) { System.out.println("NamingException: "+ex); } catch (ClassCastException cc) { System.out.println(" ClassCastException : "+cc);} catch (javax.ejb.CreateException ce) { System.out.println("CreateException: "+ce); } catch (java.rmi.RemoteException re) { System.out.println("RemoteException: "+re); } } } Stateless example – execution RUN 100000 Lire = 51.64568990894865 Euro Stateful Example Stateful Example: Dollar Converter Stateful example – The Component (Remote) Interface import java.rmi.*; import javax.ejb.*; public interface DollarConverter extends EJBObject { public double convertInEuro(double dollar) throws java.rmi.RemoteException; public double convertInDollar(double euro) throws java.rmi.RemoteException; public void setRate(double euro_dollar_ratio) throws java.rmi.RemoteException; } Stateful example – The Home Interface import java.rmi.*; import javax.ejb.*; public interface DollarConverterHome extends EJBHome { public DollarConverter create() throws RemoteException, CreateException; } Stateful example – The bean import java.rmi.*; import javax.ejb.*; public class DollarConverterBean implements SessionBean { private SessionContext sessionContext; public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; } Stateful example – The bean private double euro_dollar_ratio=1; public double convertInEuro(double dollar) throws java.rmi.RemoteException{ return dollar/euro_dollar_ratio; } public double convertInDollar(double euro) throws java.rmi.RemoteException{ return euro*euro_dollar_ratio; } public void setRate(double euro_dollar_ratio) throws java.rmi.RemoteException{ this.euro_dollar_ratio=euro_dollar_ratio; } } Stateful example – The descriptor <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd"> <ejb-jar> <description>A simple demo of a stateful Bean</description> <display-name>An Euro to Dollar Converter Bean</display-name> <enterprise-beans> <session> <description>A converter from Euro to Dollar</description> <ejb-name>DollarConverter</ejb-name> <home>DollarConverterHome</home> <remote>DollarConverter</remote> <ejb-class>DollarConverterBean</ejb-class> <session-type>Stateful</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> Stateful example – The descriptor <assembly-descriptor> <container-transaction> <method> <ejb-name>DollarConverter</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> <ejb-client-jar /> </ejb-jar> Stateful example – The client import javax.ejb.*; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; public class ConverterClient { public ConverterClient() {} public static void main(String[] args) { Object objref=null; DollarConverterHome home=null; Get naming context DollarConverter bean=null; and object reference try { InitialContext ctx=new InitialContext(); objref=ctx.lookup("EDC"); Cast to correct type JNDI name home =(DollarConverterHome)PortableRemoteObject.narrow( objref,DollarConverterHome.class); bean=home.create(); Get a bean instance from container Stateful example – The client bean.setRate(0.978); Do your business double euro=1000; System.out.println(euro+" Euro = " +bean.convertInDollar(euro)+" Dollar"); double dollar=1000; System.out.println(dollar+" Dollar = " +bean.convertInEuro(dollar)+" Euro"); } catch (Exception e) { e.printStackTrace(); } } } Stateful example – execution RUN 1000.0 Euro = 978.0 Dollar 1000.0 Dollar = 1022.4948875255624 Euro Session Beans Lifecycle: client’s view Start Does not exist Is not referenced Crash, Timeout Exists Is not referenced Client releases reference home.create() Client obtains handle Client releases reference Client invokes method (NoSuchObject Exception) Does not exist Is referenced object.remove(), home.remove(), Crash, timeout Exists Is referenced Client invokes method Stateless session Beans Lifecycle Client invokes method (NoSuchObject Exception) Does not exist Client invokes create() Container executes: 1) newInstance() 2) setSessionContext(sc) 3) ejbCreate() Client invokes remove() Container executes: ejbRemoved() Exists In the pool Client invokes method Stateful session Beans Lifecycle Client invokes create() Container executes: 1) newInstance() 2) setSessionContext(sc) 3) ejbCreate() Does not exist Client invokes remove(), or timeout is reached Container executes: ejbRemoved() ejbPassivate() Client invokes non TX method Exists, method ready in the pool Client invokes TX method AfterBegin() Client invokes commit beforeCompletion() afterCompletion(true) Client invokes TX method Passive ejbActivate() Exists, method ready in TX Client invokes rollback afterCompletion(false)