SET AS HOME PAGE

ADD TO FAVORITES / BOOKMARK THIS WEBSITE (More Details)

Introduction

Servlet

Jsp

Security

Enterprise Beans

Contact Us


Creating Stateless Session Beans (Continue ...)

 Introduction

Stateless session beans can support multiple clients, they can offer better scalability for applications that require large numbers of clients. Typically, an application requires fewer stateless session beans than stateful session beans to support the same number of clients.
Types of Enterprise Beans are
  • Session Beans
    1.  Stateless Session Beans
    2. Stateful Session Beans
  • Entity Beans
    1. Bean Managed Persistent (BMP) Entity Beans
    2. Container Managed Persistent (CMP) Entity Beans
  • Message-Driven Bean

 Creating a Stateless Session Bean Home Interface

You can create two types of stateless session bean home interfaces:
  • Remote home interface
  • Local home interface

Following are the remote home interface of a session bean needs to fulfill:
  • It needs to extend the javax.ejb.EJBHome interface.
  • It needs to declare one or more create() methods. In case of stateless session beans, only one create() method is required.
  • It needs to declare create() methods that correspond to the ejbCreate() methods in the bean class and return type of the create() methods should be of remote interface type.
  • It needs to declare the throws clause of create() method as javax.ejb.CreateException and java.rmi.RemoteException.

You can use the following code snippet to create a remote home interface of a stateless session bean:
import java.rmi.RemoteException;
import javax.ejb.CreateException;
public interface StatelessSessionHome extends javax.ejb.EJBHome
{
public StatelessSessionRemote create() throws RemoteException, CreateException;
}

You need to extend the EJBLocalHome interface that the javax.ejb package provides to create a stateless session bean local home interface.

Following are the local home interface of a session bean needs to fulfill:
  • It should not declare methods, which throw exceptions of the RemoteException type.
  • It can have super interfaces.
  • It should declare one or more create() methods that correspond to ejbCreate() methods in the session bean class. The return type of each create() method should be local interface.
  • For each create() method, it should declare the same exceptions that are declared in throws clause of corresponding ejbCreate() method in the bean class.
  • It should declare the javax.ejb.CreateException in throws clause of each create() method.

You can use the following code to create the local home interface of a stateless session bean:
import javax.ejb.CreateException;
import javax.ejb.EJBException;
public interface StatelessSessionLocalHome extends javax.ejb.EJBLocalHome
{
public StatelessSessionLocalRemote create() throws EJBException, CreateException;
}
 Creating a Stateless Session Bean Remote Interface

Following are the additional requirements that the remote interface of a session bean needs to fulfill:
  • It needs to extend the javax.ejb.EJBHome interface.
  • It should not use the local interface types, local home interface types as method arguments and return types.
  • It can have super interfaces that fulfill the RMI/IIOP requirements.

You can use the following code snippet to create a stateless session bean remote interface:
import java.rmi.RemoteException;
public interface StatelessSessionRemote extends javax.ejb.EJBObject
{
/* Declare the session bean business methods. */
}

Following are the requirements that the local interface of a stateless session bean needs to fulfill:
  • It needs to extend the javax.ejb.EJBLocalHome interface.
  • It should not declare the business methods with exception, java.rmi.RemoteException, in their throws clause.
  • It should only declare the business methods, which are implemented in the bean class of the stateless session bean. Each method in the local interface should have a corresponding method with the same signature in the bean class.
  • It can have super interfaces.

You can use the following code to create a local interface of a stateless session bean:
public interface StatelessSessionLocal extends javax.ejb.EJBLocalObject
{
/* Declare the session bean business methods. */
}
 Creating Stateless Session Bean Class

Following are the additional requirements that the bean class of a session bean needs to fulfill:
  • It should implement the javax.ejb.SessionBean interface.
  • It should be declared as public and not as final or abstract.
  • It should declare a public constructor that accepts no arguments.
  • It should not define finalize() method.
  • It should implement business methods that need to be declared as public and not as final or static. These business methods can throw application exceptions.
  • It can have super classes and super interfaces.
  • It should implement one or more ejbCreate() methods that fulfill the following criteria:
    1. Should be declared as public and not as final or static.
    2. Should have void return type.
    3. Should have RMI/IIOP compliant arguments and return values.

You can use the following code snippet to create a stateless session bean class:
public class StatelessSession implements javax.ejb.SessionBean
{
/* Define the stateless session bean life cycle methods. */

public void ejbCreate () {}
public void ejbRemove () {}
public void ejbActivate () {}
public void ejbPassivate () {}
public void setSessionContext(javax.ejb.SessionContext actx) {}

/* Implement the stateless session bean business methods.*/
}


 Accessing a Stateless Session Bean

To access a stateless session bean, a client performs the following steps:
  1. Locates a stateless session bean in the J2EE 1.4 Application Server.
  2. Retrieves references of a stateless session bean’s home and remote interfaces.
 Locating a Stateless Session Bean
A client locates a stateless session bean in the J2EE 1.4 Application Server using the Java Naming Directory Interface (JNDI). To locate a stateless session bean, a client performs the following steps:
  1. Creates an initial naming context using the InitialContext interface of JNDI. You can use the following statement to create the initial naming context:
    InitialContext ctx = new InitialContext();
  2. Locates the home object of the deployed stateless session bean using the lookup() method. The lookup() method locates a stateless session bean home object using the name assigned to the bean at the time of deployment. This method returns the reference of the home object, which is an implementation of the stateless session bean home interface. You can use the following code snippet to obtain a reference to the deployed stateless session bean home interface:
    Object ref = ctx.lookup(“java:comp/env/bean_logical_name”);

 Retrieving References of Stateless Session Bean Interfaces
After locating the stateless session bean’s home object, a client retrieves the reference to the EJB object that is created by the EJB container. Clients do not have direct access to a stateless session bean class. They can invoke a bean’s business methods using the reference of the EJB object which is an implementation of the stateless session bean remote interface. Clients use the narrow() method of the PortableRemoteObject interface to retrieve the reference of the EJB object. You can use the following code snippet to retrieve the EJB object of a stateless session bean:
StatelessSessionRemoteHome sremotehome = (StatelessSessionRemoteHome)PortableRemoteObject.narrow (ref,StatelessSessionRemoteHome.class);

Local clients retrieve the stateless session bean local home interface reference using the lookup() method of the InitialContext interface. You can use the following code snippet to locate and retrieve the stateless session bean local home interface reference:
StatelessSessionLocalHome slocalhome = (StatelessSessionLocalHome)ctx.lookup(“java:comp/env/bean_logical_name”);

A client calls the create() method in the stateless session bean home interface to retrieve the reference of stateless session bean remote interface. You can use the following code snippet to retrieve a reference of stateless session bean remote or local interface:
/* Retrieve reference of stateless session bean remote interface.*/
StatelessSessionRemote sremote = sremotehome.create();

/* Retrieve reference of stateless session bean local interface.*/
StatelessSessionLocal slocal = slocal.create();

 Click for Next Topic
<- PREVIOUSNEXT ->