Writing Session bean - Session Bean Example with Source Code

n this lesson you will learn how to develop Hello World Session Bean. We will use ant to build the application. Our application will be deployed on the Web Logic Application for testing.

Writing Session bean - Session Bean Example with Source Code

Writing Session bean

In this lesson you will learn how to develop Hello World Session Bean. We will use ant to build the application. Our application will be deployed on the Web Logic Application for testing.

What is Session Bean?
Session is one of  the EJBs and it  represents a single client inside the Application Server. Stateless session is easy to develop and its efficient. As compare to entity beans session beans require few server resources.

A session bean is similar to an interactive session and is not shared; it can have only one client, in the same way that an interactive session can have only one user. A session bean is not persistent and it is destroyed once the session terminates. 

Session Bean Types
Session Beans are of two types, Stateful Session Bean and Stateless Session Bean. 

Stateless Session Beans

A stateless session bean does not maintain a conversational state for the client. When a client invokes the method of a stateless bean, the bean's instance variables may contain a state, but only for the duration of the invocation.

Because 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.

Stateful Session Beans

The state of an object consists of the values of its instance variables. In a stateful session bean, the instance variables represent the state of a unique client-bean session. Because the client interacts ("talks") with its bean, this state is often called the conversational state.

Writing Stateful Session Bean
A Session Bean is composed of the following parts:

  • Remote Interface: The Remote Interface is the client view of the bean.
  • Home Interface: The Home Interface contains all the methods for the bean life cycle (creation, suppression) used by the client application.
  • Bean Class: The bean implementation class implements the business methods.
  • Deployment Descriptor: The deployment descriptor contains the bean properties that can be edited at assembly or deployment time.

Steps involved in developing the Stateful Session Bean can be summarized in following steps:

  1. Define Home Interface
  2. Define Remote Interface
  3. Develop EJB class
  4. Write deployment descriptors
  5. Package, deploy and test the application

Define Home Interface
The Session bean's home interface defines one or more create(...) methods and each create method must be named create and must match one of the ejbCreate methods defined in the enterprise Bean class. The return type of a create method is the Bean's remote interface type.

In case of Stateless Session Bean there is one create method with no arguments. A remote home interface extends the javax.ejb.EJBHome interface, while a local home interface extends the javax.ejb.EJBLocalHome interface.

Here is the source code of our home interface:

/*
 * TestSessionBeanHome.java
 */
package examples;

/**
 * Home interface for TestSessionBean.
 @author Deepak Kumar
 * @Web http://www.roseindia.net
 * @Email [email protected]
 */

public interface TestSessionBeanHome
 extends javax.ejb.EJBHome
{
 public examples.TestSessionBean create()
  throws javax.ejb.CreateException,
  java.rmi.RemoteException;


}

Define Remote Interface
As mentioned earlier, Remote Interface is the client view of the bean and the functions defined in the remote interface is visible to the client. There should be an implementation in the Bean class for the all the functions defined in the remote interface.

Here is the source code of our Remote Interface:

/*
* TestSessionBean.java
*
*/ 
package examples;

/**
* Remote interface for TestSessionBean.
@author Deepak Kumar
* @Web http://www.roseindia.net
* @Email [email protected]
*/

public interface TestSessionBean
 extends javax.ejb.EJBObject
{
 /**
  * The method that returns Hello Message
  */
 public java.lang.String SayHello(  )
  throws java.rmi.RemoteException;

}

Define Enterprise Bean Class
In the EJB class we write all the business methods along with required that is necessary to implement. In the EJB class methods are defined public. The Session Bean interface methods that the EJB provider must must be defined in the Session bean class are:

  • public void setSessionContext(SessionContext ic);
    This method is used by the container to pass a reference to the SessionContext to the bean instance.
  • public void ejbRemove();
    This method is invoked by the container when the instance is in the process of being removed by the container.
  • public void ejbPassivate();
    This method is invoked by the container when it wants to passivate the instance.
  • public voidejbActivate();
    This method is invoked by the container when the instance has just been reactivated.

A stateful session Bean with container-managed transaction demarcation can optionally also  implements  the javax.ejb.SessionSynchronization interface.

The Session Synchronization interface methods that must be developed are:

  • public void afterBegin();
    This method notifies a session Bean instance that a new transaction has started.
  • public void afterCompletion(boolean committed);
    This method notifies a session Bean instance that a transaction commit protocol has completed and tells the instance whether the transaction has been committed or rolled back.
  • public void beforeCompletion();
    This method notifies a session Bean instance that a transaction is about to be committed.

 Here is the source code of our Session Bean class:

/*
* SessionBean.java
*
*/ 
package examples;

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

/**
@author Deepak Kumar
* @Web http://www.roseindia.net
* @Email [email protected]
*/

public class MyTestSessionBean implements SessionBean{

  public void ejbCreate() throws CreateException {

  }

  public void setSessionContext(
   SessionContext aContext 
throws EJBException {

  }

  public void ejbActivate() throws EJBException {

  }

  public void ejbPassivate() throws EJBException {

  }

  public void ejbRemove() throws EJBException {

  }

  /**
  *  The method that returns Hello Message
  *
  */
  public String SayHello(){
  String msg="Hello! I am Session Bean";
  System.out.println(msg);
  return msg;
  }

}

In the next lesson we will write the deployment descriptors and the servlet to test the Hello World Session bean.