session 4 tp4
DESCRIPTION
TRANSCRIPT
ACCP2005/EJB 2.0/Session 4/1 of 22
Stateful Session Beans
Session 4
ACCP2005/EJB 2.0/Session 4/2 of 22
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.
ACCP2005/EJB 2.0/Session 4/3 of 22
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
ACCP2005/EJB 2.0/Session 4/4 of 22
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
ACCP2005/EJB 2.0/Session 4/5 of 22
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
ACCP2005/EJB 2.0/Session 4/6 of 22
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.
ACCP2005/EJB 2.0/Session 4/7 of 22
Rules for Conversational State
Storage
Converted to
Memory freed
Bit - blobConversational 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
ACCP2005/EJB 2.0/Session 4/8 of 22
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
ACCP2005/EJB 2.0/Session 4/9 of 22
Activation of a Stateful Bean
Enterprise Beans
EJB Object
Other EnterpriseBeans
Storage
ClientEJB Container/Server
3. Reconstruct bean
4. Call ejbActivate()
5. Invoke business method1. Invoke business methods
2. Retrieve the passivated state ofbean
ACCP2005/EJB 2.0/Session 4/10 of 22
Requirements for Stateful session beans.
Remote Interface The Bean Class The Home Interface The Client Code Deployment Descriptor
ACCP2005/EJB 2.0/Session 4/11 of 22
The Remote Interface
Stateful Session Bean Class
1. number(){/
Implementation
}
Remote Interface(Defines business methods of bean)
1. number()
Number extends EJBObject
Client
ACCP2005/EJB 2.0/Session 4/12 of 22
The Bean ClassStateful Session Bean Class
public int number(){ /* Actual Implementation*/ }
public class Numberbean implements SessionBeanprivate SessionContext ctx;
public int answer;
public void ejbCreate(int answer) throws CreateExceptionpublic void ejbRemove()
public void ejbActivate()
public void ejbPassivate()public void setSessionContext(SessionContext ctx)
ACCP2005/EJB 2.0/Session 4/13 of 22
The Home Interface
import javax.ejb.*;import java.rmi.*; public interface Numberhome extends EJBHome{
Number create( int answer ) throws RemoteException, CreateException ; }
ACCP2005/EJB 2.0/Session 4/14 of 22
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.
ACCP2005/EJB 2.0/Session 4/15 of 22
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
ACCP2005/EJB 2.0/Session 4/16 of 22
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>
ACCP2005/EJB 2.0/Session 4/17 of 22
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.
ACCP2005/EJB 2.0/Session 4/18 of 22
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
ACCP2005/EJB 2.0/Session 4/19 of 22
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.
ACCP2005/EJB 2.0/Session 4/20 of 22
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.
ACCP2005/EJB 2.0/Session 4/21 of 22
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.
ACCP2005/EJB 2.0/Session 4/22 of 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.