Presentation is loading. Please wait.

Presentation is loading. Please wait.

Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.

Similar presentations


Presentation on theme: "Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that."— Presentation transcript:

1 Session Beans -) stateless -) stateful

2 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.

3 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.

4 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.

5 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.

6 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.

7 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.

8 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.

9 Stateless Example Stateless Example: Euro Converter

10 Stateless example – The Component (Remote) Interface package statelessDemo; import java.rmi.*; import javax.ejb.*; public interface Converter extends EJBObject { public double convert(int lire); }

11 Stateless example – The Home Interface package statelessDemo; import java.rmi.*; import javax.ejb.*; public interface ConverterHome extends EJBHome { public Converter create() throws RemoteException, CreateException; }

12 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; }

13 Stateless example – The descriptor A simple demo of a stateless Bean An Euro Converter Bean A converter from Lire to Euro EuroConverter statelessDemo.ConverterHome statelessDemo.Converter statelessDemo.ConverterBean Stateless Container

14 Stateless example – The descriptor EuroConverter * Required

15 Stateless example – The client import javax.ejb.*; import javax.naming.InitialContext; public class ConverterClient { public ConverterClient() { } public static void main(String[] args) { try { InitialContext ctx=new InitialContext(); Object objref=ctx.lookup("L2EConverter"); statelessDemo.ConverterHome home = (statelessDemo.ConverterHome) javax.rmi.PortableRemoteObject.narrow( objref,statelessDemo.ConverterHome.class); statelessDemo.Converter bean=home.create(); Get naming context and object reference Cast to correct type Get a bean instance from container JNDI name

16 Stateless example – The client int lire=100000; 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); } Do your business

17 Stateless example – execution RUN 100000 Lire = 51.64568990894865 Euro

18 Stateful Example Stateful Example: Dollar Converter

19 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; }

20 Stateful example – The Home Interface import java.rmi.*; import javax.ejb.*; public interface DollarConverterHome extends EJBHome { public DollarConverter create() throws RemoteException, CreateException; }

21 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; }

22 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; }

23 Stateful example – The descriptor A simple demo of a stateful Bean An Euro to Dollar Converter Bean A converter from Euro to Dollar DollarConverter DollarConverterHome DollarConverter DollarConverterBean Stateful Container

24 Stateful example – The descriptor DollarConverter * Required

25 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; DollarConverter bean=null; try { InitialContext ctx=new InitialContext(); objref=ctx.lookup("EDC"); home =(DollarConverterHome)PortableRemoteObject.narrow( objref,DollarConverterHome.class); bean=home.create(); Get naming context and object reference Cast to correct type Get a bean instance from container JNDI name

26 Stateful example – The client bean.setRate(0.978); 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(); } Do your business

27 Stateful example – execution RUN 1000.0 Euro = 978.0 Dollar 1000.0 Dollar = 1022.4948875255624 Euro

28 Session Beans Lifecycle: client’s view Does not exist Is not referenced Exists Is not referenced Exists Is referenced Does not exist Is referenced Start home.create() Client releases reference Crash, Timeout Client releases reference Client obtains handle object.remove(), home.remove(), Crash, timeout Client invokes method Client invokes method (NoSuchObject Exception)

29 Stateless session Beans Lifecycle Exists In the pool Does not exist ejbRemoved() Client invokes method (NoSuchObject Exception) 1)newInstance() 2)setSessionContext(sc) 3)ejbCreate() Client invokes create() Container executes: Client invokes remove() Container executes:

30 Stateful session Beans Lifecycle Passive ejbPassivate() ejbActivate() Exists, method ready in the pool Does not exist Client invokes remove(), or timeout is reached Container executes: ejbRemoved() Client invokes create() Container executes: 1)newInstance() 2)setSessionContext(sc) 3)ejbCreate() Client invokes non TX method Exists, method ready in TX Client invokes TX method AfterBegin() Client invokes commit beforeCompletion() afterCompletion(true) Client invokes rollback afterCompletion(false)


Download ppt "Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that."

Similar presentations


Ads by Google