Session 4 Tp4


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Session 4 Tp4

  1. 1. Stateful Session Beans Session 4
  2. 2. Session Objectives <ul><li>Define a Stateful Session Bean </li></ul><ul><li>List the characteristics of a Stateful Session Bean. </li></ul><ul><li>Write Stateful session bean programs. </li></ul><ul><li>Differentiate between Stateless and Stateful Session beans. </li></ul>
  3. 3. Review of Session 3 (1 of 3) <ul><li>The bean class, the EJB object, the remote interface, the home interface, the home object, the deployment descriptors, and the jar files constitute the enterprise bean. </li></ul><ul><li>Bean class contains the implementation of the business logic methods. </li></ul><ul><li>EJB container performs important management functions when it intercepts client requests such as: </li></ul><ul><ul><ul><li>* Transaction logic </li></ul></ul></ul><ul><ul><ul><li>* Security logic </li></ul></ul></ul><ul><ul><ul><li>* Bean instance logic </li></ul></ul></ul><ul><li>The Remote interface duplicates the methods exposed by the bean class. </li></ul><ul><li>Responsibilities of the EJB home object: </li></ul><ul><ul><ul><li>* Creating EJB objects </li></ul></ul></ul><ul><ul><ul><li>* Searching for existing EJB Objects </li></ul></ul></ul><ul><ul><ul><li>* Removing EJB Objects </li></ul></ul></ul>
  4. 4. Review of Session 3 (2 of 3) <ul><li>The deployment descriptor: A file that tells the EJB server about the classes, the home interface, and the remote interface that form the bean. </li></ul><ul><li>The lifetime of a session bean may last till such time as that of a client session. It could be as long as a window is open or as long as an application is open. Session beans do not, therefore, survive application server crashes or machine crashes. </li></ul><ul><li>Three classes are essential for deployment: </li></ul><ul><ul><ul><li>Home Interface </li></ul></ul></ul><ul><ul><ul><li>Remote Interface </li></ul></ul></ul><ul><ul><ul><li>Bean class </li></ul></ul></ul>
  5. 5. Review of Session 3 (3 of 3) <ul><li>The ejb-jar.xml file is a compressed file that contains the declarations of the enterprise bean class, the remote interface and the home interface. </li></ul><ul><li>An EJB client can be: </li></ul><ul><ul><ul><li>* An ordinary JavaBean </li></ul></ul></ul><ul><ul><ul><li>* Another EJB </li></ul></ul></ul><ul><ul><ul><li>* A JSP Page </li></ul></ul></ul><ul><ul><ul><li>* A servlet </li></ul></ul></ul><ul><ul><ul><li>* An applet </li></ul></ul></ul><ul><ul><ul><li>*A stand-alone application </li></ul></ul></ul>
  6. 6. Characteristics of Stateful Session Beans <ul><li>Conversional state occurs when a client calls a method on a bean. </li></ul><ul><li>Pooling has to be done to conserve resources and enhances scalability. </li></ul><ul><li>The container swaps out a bean and saves the conversational state to the hard disk or other storage devices. This process is called passivation. </li></ul><ul><li>When the client requests for a method, the passivated conversational state is returned to the bean. The bean is again ready to meet the request. This process is called activation. </li></ul><ul><li>To passivate a bean a container uses Least Recently Used method. </li></ul><ul><li>To activate a bean a container uses Just-in-Time (JIT) method. </li></ul>
  7. 7. Rules for Conversational State Storage Converted to Memory freed Bit - blob Conversational state of a bean Written to storage in case of passivation Read from storage into memory in case of activation Converted into data from bit-blob
  8. 8. Passivation of a Stateful Bean Client EJB Object Enterprise bean Other Enterprise beans Storage 1. Invoke business methods 5. Store passivated bean state The EJB Container/Server 2. Take the least recently used bean 3. Call ejbPassivate() 4. Serialize the bean state
  9. 9. Activation of a Stateful Bean Enterprise Beans EJB Object Other Enterprise Beans Storage Client EJB Container/Server 3. Reconstruct bean 4. Call ejbActivate() 5. Invoke business method 1. Invoke business methods 2. Retrieve the passivated state of bean
  10. 10. Requirements for Stateful session beans. <ul><li>Remote Interface </li></ul><ul><li>The Bean Class </li></ul><ul><li>The Home Interface </li></ul><ul><li>The Client Code </li></ul><ul><li>Deployment Descriptor </li></ul>
  11. 11. The Remote Interface Client Stateful Session Bean Class 1. number() { / Implementation } Remote Interface (Defines business methods of bean) 1. number() Number extends EJBObject
  12. 12. The Bean Class Stateful Session Bean Class public int number(){ /* Actual Implementation*/ } public class Numberbean implements SessionBean private SessionContext ctx; public int answer; public void ejbCreate(int answer) throws CreateException public void ejbRemove() public void ejbActivate() public void ejbPassivate() public void setSessionContext(SessionContext ctx)
  13. 13. The Home Interface import javax.ejb.*; import java.rmi.*;     public interface Numberhome extends EJBHome {   Number create( int answer ) throws RemoteException, CreateException ;     }
  14. 14. Goals of the Client Code <ul><li>It acquires a JNDI initial context and the naming context. </li></ul><ul><li>Home object is located using JNDI. </li></ul><ul><li>Client calls methods on the bean using the Home Object. </li></ul><ul><li>Conversations are created using home interface and used from the client code </li></ul><ul><li>Deployment descriptors limit the pool size. </li></ul><ul><li>Lastly, the EJB objects are removed. </li></ul>
  15. 15. The Deployment Descriptor : ejb-jar.xml <ul><li>ejb-jar.xml file contains the declarations of </li></ul><ul><li>Enterprise bean </li></ul><ul><li>Home interface </li></ul><ul><li>Remote interface </li></ul><ul><li>Bean class </li></ul><ul><li>Session Type </li></ul><ul><li>Transaction type </li></ul>
  16. 16. jboss.xml <ul><li>It contains the declaration of the actual ejb name and the JNDI name of the Session bean. </li></ul><ul><li>Client code maps the actual ejb name from this JNDI name. </li></ul><jboss> <enterprise-beans> <session> <ejb-name>Number</ejb-name> <jndi-name>Number</jndi-name> </session> </enterprise-beans> </jboss>
  17. 17. Creating the jar file <ul><li>The jar file is created using java appropriate statement </li></ul><ul><li>Packages the remote interface, the bean class, the home interface and the XML files. </li></ul><ul><li>The jar file is copied into the deploy directory on the server and then deployed. </li></ul><ul><li>The creation and copying of the file in the deploy directory of the server marks the completion of the bean. </li></ul>
  18. 18. Advantages and Disadvantages of Stateless Session bean <ul><li>Advantages </li></ul><ul><ul><li>Pooling is simple in stateless session bean as there is reuse of beans and this reduces overload </li></ul></ul><ul><ul><li>Loss of conversations is comparatively less in a stateless session bean </li></ul></ul><ul><li>Disadvantages </li></ul><ul><ul><li>Client-specific data needs to be given for every method invocation </li></ul></ul><ul><ul><li>Bandwidth could be reduced for other processes, as data has to be passed each time </li></ul></ul><ul><ul><li>Input/Output bottlenecks are more in stateless session beans </li></ul></ul>
  19. 19. Advantages and Disadvantages of Stateful Session Beans <ul><li>Advantages </li></ul><ul><ul><li>Data is not pushed to the bean for every method invocation </li></ul></ul><ul><ul><li>All resources are stored in a database when the bean is passive </li></ul></ul><ul><li>Disadvantages </li></ul><ul><ul><li>Conversation with the client maybe lost as there is caching of client conversation each time a bean is used. </li></ul></ul><ul><ul><li>Bean state has to be activated and passivated in between method invocations. Therefore there can be I/O bottlenecks. </li></ul></ul>
  20. 20. Practices used for writing Stateful Session Beans <ul><li>Always try to keep the conversations short. </li></ul><ul><li>Try to use an EJB product that persists stateful conversations. </li></ul><ul><li>Always write client code that takes into account bean failures. </li></ul>
  21. 21. Summary - 1 <ul><li>Removing a bean out of the container, and saving its resources to the database or file is known as passivation. </li></ul><ul><li>The process of bringing back the passivated bean into the container with all its resources is known as activation. </li></ul><ul><li>The strategy of passivating a bean that has been used the longest while ago is called the Least Recently Used method. </li></ul><ul><li>Rules for the conversational state are put forward by Java object serialization. </li></ul><ul><li>The container calls the ejbPassivate() method and tells the bean that it is going to passivate the bean. </li></ul><ul><li>During activation, the ejbActivate() method is called, and the conversational state is read back into the memory. The container then re-constructs the memory-state using object serialization. </li></ul>
  22. 22. Summary - 2 <ul><li>This ejb-jar.xml file has to be present in the directory called META-INF . </li></ul><ul><li>A jar file is created in order to package the three files, namely, the remote interface, the bean class, and the home interface. </li></ul><ul><li>To deploy the bean, the newly created .jar file has to be copied into the deploy directory on the server. </li></ul><ul><li>If the business process requires multiple invocations, a stateful session bean has to be used. However, if the business process is only for a single method call, the stateless session bean will suffice. </li></ul>