Session Beans stateless stateful Session Beans A session
Session Beans -) stateless -) stateful
Session Beans A session bean represents a single client inside the J 2 EE 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: Euro Converter
Stateless example – The Component (Remote) Interface package stateless. Demo; import java. rmi. *; import javax. ejb. *; public interface Converter extends EJBObject { public double convert(int lire); }
Stateless example – The Home Interface package stateless. Demo; import java. rmi. *; import javax. ejb. *; public interface Converter. Home extends EJBHome { public Converter create() throws Remote. Exception, Create. Exception; }
Stateless example – The bean package stateless. Demo; import java. rmi. *; import javax. ejb. *; public class Converter. Bean implements Session. Bean { private Session. Context session. Context; public void ejb. Create() {} public void ejb. Remove() {} public void ejb. Activate() {} public void ejb. Passivate() {} public void set. Session. Context(Session. Context session. Context) { this. session. Context = session. Context; } 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 Java. Beans 1. 1//EN" "http: //java. sun. com/j 2 ee/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>Euro. Converter</ejb-name> <home>stateless. Demo. Converter. Home</home> <remote>stateless. Demo. Converter</remote> <ejb-class>stateless. Demo. Converter. Bean</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>Euro. Converter</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. Initial. Context; public class Converter. Client { public Converter. Client() { } Get naming context public static void main(String[] args) { and object reference try { Initial. Context ctx=new Initial. Context(); JNDI name Object objref=ctx. lookup("L 2 EConverter"); stateless. Demo. Converter. Home home = Cast to correct type (stateless. Demo. Converter. Home) javax. rmi. Portable. Remote. Object. narrow( objref, stateless. Demo. Converter. Home. class); stateless. Demo. 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. Naming. Exception ex) { System. out. println("Naming. Exception: "+ex); } catch (Class. Cast. Exception cc) { System. out. println(" Class. Cast. Exception : "+cc); } catch (javax. ejb. Create. Exception ce) { System. out. println("Create. Exception: "+ce); } catch (java. rmi. Remote. Exception re) { System. out. println("Remote. Exception: "+re); } } }
Stateless example – execution RUN 100000 Lire = 51. 64568990894865 Euro
Stateful Example: Dollar Converter
Stateful example – The Component (Remote) Interface import java. rmi. *; import javax. ejb. *; public interface Dollar. Converter extends EJBObject { public double convert. In. Euro(double dollar) throws java. rmi. Remote. Exception; public double convert. In. Dollar(double euro) throws java. rmi. Remote. Exception; public void set. Rate(double euro_dollar_ratio) throws java. rmi. Remote. Exception; }
Stateful example – The Home Interface import java. rmi. *; import javax. ejb. *; public interface Dollar. Converter. Home extends EJBHome { public Dollar. Converter create() throws Remote. Exception, Create. Exception; }
Stateful example – The bean import java. rmi. *; import javax. ejb. *; public class Dollar. Converter. Bean implements Session. Bean { private Session. Context session. Context; public void ejb. Create() {} public void ejb. Remove() {} public void ejb. Activate() {} public void ejb. Passivate() {} public void set. Session. Context(Session. Context session. Context) { this. session. Context = session. Context; }
Stateful example – The bean private double euro_dollar_ratio=1; public double convert. In. Euro(double dollar) throws java. rmi. Remote. Exception{ return dollar/euro_dollar_ratio; } public double convert. In. Dollar(double euro) throws java. rmi. Remote. Exception{ return euro*euro_dollar_ratio; } public void set. Rate(double euro_dollar_ratio) throws java. rmi. Remote. Exception{ 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 Java. Beans 1. 1//EN" "http: //java. sun. com/j 2 ee/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>Dollar. Converter</ejb-name> <home>Dollar. Converter. Home</home> <remote>Dollar. Converter</remote> <ejb-class>Dollar. Converter. Bean</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>Dollar. Converter</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. Initial. Context; import javax. rmi. Portable. Remote. Object; public class Converter. Client { public Converter. Client() {} public static void main(String[] args) { Object objref=null; Dollar. Converter. Home home=null; Get naming context Dollar. Converter bean=null; and object reference try { Initial. Context ctx=new Initial. Context(); objref=ctx. lookup("EDC"); Cast to correct type JNDI name home =(Dollar. Converter. Home)Portable. Remote. Object. narrow( objref, Dollar. Converter. Home. class); bean=home. create(); Get a bean instance from container
Stateful example – The client bean. set. Rate(0. 978); Do your business double euro=1000; System. out. println(euro+" Euro = " +bean. convert. In. Dollar(euro)+" Dollar"); double dollar=1000; System. out. println(dollar+" Dollar = " +bean. convert. In. Euro(dollar)+" Euro"); } catch (Exception e) { e. print. Stack. Trace(); } } }
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 (No. Such. Object 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 (No. Such. Object Exception) Does not exist Client invokes create() Container executes: 1) new. Instance() 2) set. Session. Context(sc) 3) ejb. Create() Client invokes remove() Container executes: ejb. Removed() Exists In the pool Client invokes method
Stateful session Beans Lifecycle Client invokes create() Container executes: 1) new. Instance() 2) set. Session. Context(sc) 3) ejb. Create() Client invokes non TX method Client invokes TX method After. Begin() Client invokes commit before. Completion() after. Completion(true) Client invokes TX method Does not exist Client invokes remove(), or timeout is reached Container executes: ejb. Removed() ejb. Passivate() Exists, method ready in the pool Passive ejb. Activate() Exists, method ready in TX Client invokes rollback after. Completion(false)
- Slides: 30