EJB Stateful Session Bean Example

We already discussed the fundamental concepts of EJB and various types of EJBs with their features.In the last chapter we  discussed  the concept of Stateless Session Bean with an example. In this chapter we are looking how a Stateful Session Bean works with a suitable example.We are using EJB 3.0 specifications.So no need for ejb-jar.xml . Instead  EJB 3 .x is using annotations.

What is a Stateful Session Bean  ?

It a type of session bean which keeps a conversational state with an  invoking client.If we have two business methods in our bean , and if the client is trying to access two methods simultaneously . Then the state of the bean at the end of execution of first method and at the beginning of execution   of second method are the same.

A Stateful Session Bean can be identified  by a @Stateful annotation(in EJB 3.x).

Life Cycle of Stateful Session Bean

1)In case of  Stateful Session Bean , the life cycle starts when the client invokes.

2)If a method with @PostConstruct annotation is there , it will be invoked

3)Now the init() or ejbCreate() method will be invoked,if any. Now the bean is active.

4)Now the container can  move the bean to deactivate state or passive state. Passive state means , the bean instance will be moved to secondary storage.If there is a method exists with @PrePassivate annotation , it will be executed before moving the bean to passive state.

5)If a client invokes a bean in existing in passive state , it will go to  active state .If a method with  @PostActivate annotation is there  , it will be executed by the EJB container. The bean will stay in active state.

6)Now if the client invokes the method with @Remove annotation, the method with @PreDestroy will be invoked by EJB container .Now the bean is ready for garbage collection.

EJB Stateful Session Bean Example

Now let us discuss an EJB Stateful Session Bean. In the just previous chapter   we discussed a Stateless Session Bean example.There we used JBoss 6.0 as application server.The same set of tools and software  are using here also.The same project and package we are using here .The steps there we discussed   there till step 8  needs to be followed here also. In step 8 we need to give state type as Stateful . Also we need to give a different name for our bean.I case of Stateless Session Bean , we used ExampleBean as bean name. Here  we are using  the name as SampleBean. Select both remote and home options for interfaces.

statefulPress Finish button.

Now  create a method public String getMessage() in both the interfaces.

SampleBeanLocal.java

import javax.ejb.Local;

@Local

public interface SampleBeanLocal {

public String getMessage();

}

SampleBeanRemote.java

import javax.ejb.Remote;

@Remote

public interface SampleBeanRemote {

public String getMessage();

}

Now paste the following content to SampleBean.java.

import javax.ejb.Stateful;

/**
* Session Bean implementation class SampleBean
*/
@Stateful

public class SampleBean implements SampleBeanRemote, SampleBeanLocal {

/**
* Default constructor.
*/
public SampleBean() {
}

@Override
public String getMessage() {
return "This is a message from EJB";
}

}

Now  right click on the project and Run As —>Run on Server. Select JBoss server instance and run . When the stateful session bean is deployed , the following message will be displayed on the console.

[JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

    SampleBean/remote – EJB3.x Default Remote Business Interface

    SampleBean/remote-com.ejbs.SampleBeanRemote – EJB3.x Remote Business Interface

    SampleBean/local – EJB3.x Default Local Business Interface

    SampleBean/local-com.ejbs.SampleBeanLocal – EJB3.x Local Business Interface

Now let use create the client. In the client project create a class.

SampleBeanClient.java

import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.ejbs.SampleBeanRemote;

public class SampleBeanClient {

public static void main(String[] args) {
try {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.factory.url.pkgs",
"org.jboss.naming");
props.setProperty("java.naming.provider.url", "127.0.0.1:1099");

InitialContext ctx = new InitialContext(props);
SampleBeanRemote sampleBeanRemote = (SampleBeanRemote) ctx
.lookup("SampleBean/remote");
System.out.println("Message = " + sampleBeanRemote.getMessage());

} catch (NamingException e) {
e.printStackTrace();
}
}
}

Output

Now run the SampleBeanClient.java.The message will be displayed in the console.

Message = This is a message from EJB

See Related Topics

EJB Overview

EJB Stateless Session Bean Example

Stateless Session Bean vs Stateful Session Bean

EJB Message Driven Bean Example