Your SlideShare is downloading. ×
Session 4 Tp4
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Session 4 Tp4

614
views

Published on

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
614
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
60
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Stateful Session Beans Session 4
  • 2. Session Objectives
    • Define a Stateful Session Bean
    • List the characteristics of a Stateful Session Bean.
    • Write Stateful session bean programs.
    • Differentiate between Stateless and Stateful Session beans.
  • 3. Review of Session 3 (1 of 3)
    • 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.
    • Bean class contains the implementation of the business logic methods.
    • EJB container performs important management functions when it intercepts client requests such as:
        • * Transaction logic
        • * Security logic
        • * Bean instance logic
    • The Remote interface duplicates the methods exposed by the bean class.
    • Responsibilities of the EJB home object:
        • * Creating EJB objects
        • * Searching for existing EJB Objects
        • * Removing EJB Objects
  • 4. Review of Session 3 (2 of 3)
    • The deployment descriptor: A file that tells the EJB server about the classes, the home interface, and the remote interface that form the bean.
    • 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.
    • Three classes are essential for deployment:
        • Home Interface
        • Remote Interface
        • Bean class
  • 5. Review of Session 3 (3 of 3)
    • 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.
    • An EJB client can be:
        • * An ordinary JavaBean
        • * Another EJB
        • * A JSP Page
        • * A servlet
        • * An applet
        • *A stand-alone application
  • 6. Characteristics of Stateful Session Beans
    • Conversional state occurs when a client calls a method on a bean.
    • Pooling has to be done to conserve resources and enhances scalability.
    • The container swaps out a bean and saves the conversational state to the hard disk or other storage devices. This process is called passivation.
    • 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.
    • To passivate a bean a container uses Least Recently Used method.
    • To activate a bean a container uses Just-in-Time (JIT) method.
  • 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. 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. 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. Requirements for Stateful session beans.
    • Remote Interface
    • The Bean Class
    • The Home Interface
    • The Client Code
    • Deployment Descriptor
  • 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. 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. The Home Interface import javax.ejb.*; import java.rmi.*;     public interface Numberhome extends EJBHome {   Number create( int answer ) throws RemoteException, CreateException ;     }
  • 14. Goals of the Client Code
    • It acquires a JNDI initial context and the naming context.
    • Home object is located using JNDI.
    • Client calls methods on the bean using the Home Object.
    • Conversations are created using home interface and used from the client code
    • Deployment descriptors limit the pool size.
    • Lastly, the EJB objects are removed.
  • 15. The Deployment Descriptor : ejb-jar.xml
    • ejb-jar.xml file contains the declarations of
    • Enterprise bean
    • Home interface
    • Remote interface
    • Bean class
    • Session Type
    • Transaction type
  • 16. jboss.xml
    • It contains the declaration of the actual ejb name and the JNDI name of the Session bean.
    • Client code maps the actual ejb name from this JNDI name.
    <jboss> <enterprise-beans> <session> <ejb-name>Number</ejb-name> <jndi-name>Number</jndi-name> </session> </enterprise-beans> </jboss>
  • 17. Creating the jar file
    • The jar file is created using java appropriate statement
    • Packages the remote interface, the bean class, the home interface and the XML files.
    • The jar file is copied into the deploy directory on the server and then deployed.
    • The creation and copying of the file in the deploy directory of the server marks the completion of the bean.
  • 18. Advantages and Disadvantages of Stateless Session bean
    • Advantages
      • Pooling is simple in stateless session bean as there is reuse of beans and this reduces overload
      • Loss of conversations is comparatively less in a stateless session bean
    • Disadvantages
      • Client-specific data needs to be given for every method invocation
      • Bandwidth could be reduced for other processes, as data has to be passed each time
      • Input/Output bottlenecks are more in stateless session beans
  • 19. Advantages and Disadvantages of Stateful Session Beans
    • Advantages
      • Data is not pushed to the bean for every method invocation
      • All resources are stored in a database when the bean is passive
    • Disadvantages
      • Conversation with the client maybe lost as there is caching of client conversation each time a bean is used.
      • Bean state has to be activated and passivated in between method invocations. Therefore there can be I/O bottlenecks.
  • 20. Practices used for writing Stateful Session Beans
    • Always try to keep the conversations short.
    • Try to use an EJB product that persists stateful conversations.
    • Always write client code that takes into account bean failures.
  • 21. Summary - 1
    • Removing a bean out of the container, and saving its resources to the database or file is known as passivation.
    • The process of bringing back the passivated bean into the container with all its resources is known as activation.
    • The strategy of passivating a bean that has been used the longest while ago is called the Least Recently Used method.
    • Rules for the conversational state are put forward by Java object serialization.
    • The container calls the ejbPassivate() method and tells the bean that it is going to passivate the bean.
    • 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.
  • 22. Summary - 2
    • This ejb-jar.xml file has to be present in the directory called META-INF .
    • A jar file is created in order to package the three files, namely, the remote interface, the bean class, and the home interface.
    • To deploy the bean, the newly created .jar file has to be copied into the deploy directory on the server.
    • 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.