Introduction
|
- Types of Enterprise Beans are
-
- Session Beans
- Stateless Session Beans
- Stateful Session Beans
- Entity Beans
- Bean Managed Persistent (BMP) Entity Beans
- Container Managed Persistent (CMP) Entity Beans
- Message-Driven Bean
- You need to define three Java class files in order to create a stateful session bean. The three Java class files of a stateful session bean are:
-
Stateful session bean home interface
-
Stateful session bean remote interface
-
Stateful session bean class
|
|
Creating the Stateful Session Bean Home Interface
|
The stateful session bean home interface extends the javax.ejb.EJBHome interface.
The stateful session bean home interface defines the create() method to create new stateful session bean instances. There can be multiple create() methods in the stateful session bean home interface. The return type of the create() method is a reference to the implementation of stateful session bean remote interface
In the stateful session bean home interface, the throws clause of the create() method includes the exceptions, javax.ejb.CreateException and javax.ejb.RemoteException.
- You can use the following code snippet to create a stateful session bean home interface:
-
import javax.ejb.*;
-
import java.rmi.RemoteException;
-
public interface StatefulSessionHome extends EJBHome
-
{
-
public StatefulSession create(parameter_datatype parameter_value) throws CreateException,RemoteException;
-
}
|
Creating the Stateful Session Bean Remote Interface
|
The stateful session bean remote interface extends the javax.ejb.EJBObject interface.
The signature of the business methods should be the same as the business methods implemented in the stateful session bean class. All business methods should throw the javax.ejb.RemoteException exception. You can use the following code snippet to create a stateful session bean remote interface:
-
import javax.ejb.*;
import java.rmi.RemoteException;-
public interface StatefulSession extends EJBObject
-
{
-
/*Declare the session bean business methods.*/
-
}
|
Creating a Stateful Session Bean Class
|
The stateful session bean class implements the javax.ejb.SessionBean interface
The stateful session bean class is declared as public. You can use the following code snippet to create a stateful session bean class:
-
import javax.ejb.*;
-
public class StatefulSessionBean implements SessionBean
-
{
/* Declare instance variables.*/
public int count()-
{
-
return ++counter;
-
}
-
public void ejbCreate(int val) throws CreateException
-
{
/* Initialize instance variables.*/
}-
public void ejbRemove()
-
{}
-
public void ejbActivate()
-
{}
-
public void ejbPassivate()
-
{}
-
public void setSessionContext(SessionContext ctx)
-
{}
-
}
|
Accessing a Stateful Session Bean
|
A client accesses a stateful session bean’s business methods using the references of the bean’s home and remote interfaces. Both, Web clients and Application clients, can access a stateful session bean. The steps to access stateful session bean business methods are same for both types of clients. To access a stateful session bean, a client performs the following steps:-
Locates a stateful session bean deployed in the J2EE 1.4 Application Server.
-
Retrieves references of the stateful session bean home and remote interfaces.
|
Locating a Stateful Session Bean
|
A client locates a stateful session bean in the J2EE 1.4 Application Server using Java Naming Directory Interface (JNDI). To locate a stateful session bean, a client needs to perform the following steps:-
A client should create an initial naming context by using the InitialContext interface of JNDI. You can use the following statement to create an initial naming context:
-
InitialContext ctx = new InitialContext();
-
A client should locate the deployed stateful session bean after creating an initial context by using the lookup() method. The lookup() method returns the reference of the stateful session bean home interface. You can use the following code snippet to locate a deployed stateful session bean home interface:
-
Object ref = ctx.lookup(“java:comp/env/bean_logical_name”);
|
Retrieving References of Stateful Session Bean Interfaces
|
- A client uses the narrow() method of the PortableRemoteObject interface to retrieve the reference to a stateful session bean home interface. You can use the following code snippet to retrieve a stateful session bean remote home interface reference:
-
StatefulSessionRemoteHome sremotehome = (StatefulSessionRemoteHome)PortableRemoteObject.narrow (ref,StatefulSessionRemoteHome.class);
- A local client should use the lookup() method of the InitialContext interface to retrieve a reference to stateful session bean local home interface. You can use the following code snippet to locate and retrieve reference to a stateful session bean local home interface:
-
StatefulSessionLocalHome slocalhome = (StatefulSessionLocalHome)ctx.lookup(“java:comp/env/bean_logical_name”);
- A client retrieves a reference to the remote or local interface of the stateful session bean by calling the create() method in the stateful session bean home interface. You can use the following code snippet to retrieve a reference to the remote or local interface of the stateful session bean:
-
StatefulSessionRemote sremote = sremotehome.create();
-
/* Retrieve reference of stateful session bean remote interface.*/
-
StatefulSessionLocal slocal = slocal.create();
|
|
Click for Next Topic
|
|