Session Bean

 Session Bean

 

 A session bean usually exists for the lifetime of a single client session. The methods of a session bean perform a set of tasks or processes for the client that uses the bean. Session beans persist only for the life of the connection with the client. Unless you need to work with persistent data that exists in a data store, you are usually working with session beans.

 

Types of session beans

 

There are two types of session beans: those that can maintain state information between method calls , which are called stateful beans, and those that can’t , which are called stateless beans.

 

Stateful session bean

 

Stateful session beans are objects used by a single client and they maintain state on behalf of that client. For example, consider a shopping cart session bean. As the shopper in an online store selects items in a list within the shopping cart session bean object. When the shopper is ready to purchase the items , the list is used to calculate the total cost.

 

Stateless session bean

 

Stateless session beans don’t maintain state for any specific client. Therefore, they can be used by many clients. For example, consider a sort session bean that contains a sortList() business method. The client would invoke sortList(), passing it an unsorted list of items. sortList() would then pass back to the client a sorted list.

 

Writing the session bean class

 

To create a session bean class,

v Create a class that implements the javax.ejb.SessionBean interface.

v Implement one or more ejbcreate() methods. If you are creating a stateless session bean, the class implement just one parameterless ejbcreate() method. If you have already created the remote home or local home interface for the bean , the bean must have an ejbcreate() method with the same signature for each create() method in the remote home/local home interface.

v Define and implements the business methods you want your bean to have. If you have already created the remote or local interface for the bean, the methods must be defined exactly as they are in the remote/local interface.

 

Implementing the SessionBean interface

 

The SessionBean interface defines the methods all session beans must implements. It extends the EnterpriseBean Interface.

 

Package javax.ejb;

Public interface SessionBean extends EnterpriseBean{

Void setSessionContext(SessionContext sessionContext) throws EJBException, RemoteException;

Void ejbRemove() throws EJBException, RemoteException;

Void ejbActivate() throws EJBException, RemoteException;

Void ejbPassivate() throws EJBException, RemoteException;

}

 

The methods of the SessionBean interface are closely associated with the life cycle of a session bean. This table explains their purpose:

 

Method Description

 

Method

Description

setSessionContext()

Sets a session context. The bean’s container calls this method to associate a session bean instance with its context. The session context interface provides methods to access the runtime properties of the context in which a session runs. Usually a session bean retains its context in a data field.

ejbRemove()

Notifies a session object that it is about to be removed. The container calls this method whenever it removes a stateful session bean a result of the client calling a remove() method of the remote/local or remote home/local home interface.

ejbActivated()

Notifies a stateful session object that is has been activated.

ejbPassivate()

Notifies a stateful session object that it is about to be deactivated by the container.

 

The ejbActivated() and ejbPassivate() methods allow a stateful session bean to manage resources.

 

Writing the business methods

Within your enterprise bean class, write full implementations of the business methods your bean needs. To make these methods available to a client, you must also declare them in the bean’s remote interface exactly as they are declared in the bean class.

 

Adding one or more ejbCreate() methods

 

You can add additional ejbCreate() methods that do include parameters. While stateless session beans never need more than a parameterless ejbCrete() method because they don’t retain any state, stateful session beans often need one or more ejbCreate() methods that have parameters. As you write additional ejbCreate() methods with parameters keep these rules in mind:

 

v Each ejbCreate() must be declared as public.

v Each must return void.

v The parameter of an ejbCreate() method must be of the same number and type as those in the corresponding create() method in the bean’s remote interface. For stateless session beans, there can be only one parameterless ejbCreate().

v This is the signature for all ejbCreate() methods of a session bean:

 

Public void ejbCreate(<zero or more parameters>)

{

//implementation

}

 

The ejbCreate() method need not throw an exception, although it can throw application specific exceptions and other exceptions, such javax.ejb.CreateException.

No comments:

Post a Comment