Stateless Session Bean vs Stateful Session Bean

We already discussed the fundamentals of Enterprise Java Bean . Also we discussed EJB Stateless Session Bean and EJB Stateful Session Bean with examples.In this chapter we are discussing an example which illustrates the difference between both stateless and stateful session beans.

Stateless Session Bean vs Stateful Session Bean

There are two types of Session Beans. They are:

a)Stateless Session Bean,

b)Stateful session Bean

A stateless Session Bean , as the name implies , does not keep a conversational state with the calling client. In other words , if a stateless session bean has two business methods , and if a client is simultaneously accessing both the methods. Then we cannot guarantee that  the state of the bean remains the same. If another client is trying to access the bean , the container may give the existing instance to that client. So the state can be modified by the new client.

In case of a Stateful Session Bean , a conversational state is maintained between the client and the bean. In other words there will be separate bean instances for each client.So state for a client cannot be changed by a new client.

Now let us discuss the life cycles of each type of beans.

Statelful Session Bean Life Cycle

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

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.

Stateless Session Bean Life Cycle

There are only two states in a Stateless Session Bean life cycle .

1)Does not exist state

2)Ready for invocation from client.

The EJB container creates a pool of beans and does the necessary dependency injection. Then , if a method with @PostConstruct annotation is there ,it will be invoked.Now the bean is ready for client invocation.If a method annotated with@PreDestroy is there , then that method will be invoked at the end of client invocation. Now the bean is ready for garbage collection.

Stateless  Session Bean vs Stateful Session Bean example

Now we can discuss an example  which compares both types of beans. Each bean has two methods. First method sets a String attribute to bean .Second method Retrieves the value of attribute.The  client application has two bean references. Client invokes the bean methods simultaneously . In  case of Stateless Session Bean , the bean is not  keeping a conversational  state with client. In case of Stateful session Bean , bean keeps a conversational state with Client. In other words , in case of a Stateful Session Bean, the  two bean references from the client are unique. In case of Stateless Session Bean,  the same reference can be reused by the EJB container .

Tools And Software Required

1)Java EE SDK 1.6  – Download link

2)Eclipse Indigo - Download link

3)JBoss-6.0.0.M1 – Download link

Steps

1)Install Java EE SDK. Download and extract JBoss and Eclipse to appropriate locations.Open eclipse in a suitable workspace.

2)In eclipse , start a new EJB Project by  File–>New–>EJB Project. Give suitable name to the project. In our case , it is given as SessionBeanSample. Select EJB version as 3.x and run time as JBoss 6.x.

1

3)Now right click on the ejbModules . Then select New –>Package.Give suitable name as package . In this example , we are using com.beans as package name.

4)Right click on the package . The select New–>Session Bean.Select State Type as Stateless.Give the name as StatelessBean.Select both local and remote options for interfaces.

3-1

Press Finish.Now the interfaces and class will be created. paste the contents as given.

StatelessBeanRemote.java

 Java |  copy code |? 
1
import javax.ejb.Remote;
2
 
3
@Remote
4
public interface StatelessBeanRemote {
5
    public void setMessage(String message);
6
 
7
    public String getMessage();
8
}

StatelessBeanLocal.java

 Java |  copy code |? 
1
import javax.ejb.Local;
2
 
3
@Local
4
public interface StatelessBeanLocal {
5
    public void setMessage(String message);
6
 
7
    public String getMessage();
8
}

StatelessBean.java

 Java |  copy code |? 
01
import javax.ejb.Stateless;
02
 
03
/**
04
 * Session Bean implementation class StatelessBean
05
 */
06
@Stateless
07
public class StatelessBean implements StatelessBeanRemote, StatelessBeanLocal {
08
    private String message;
09
 
10
    /**
11
     * Default constructor.
12
     */
13
    public StatelessBean() {
14
 
15
    }
16
 
17
    @Override
18
    public void setMessage(String message) {
19
        this.message = message;
20
    }
21
 
22
    @Override
23
    public String getMessage() {
24
        String reply;
25
        if(null == message){
26
            reply = "Empty message";
27
        }else{
28
            reply = message;
29
        }
30
        return reply;
31
    }
32
 
33
}

5)Right click on the package name and New–>SessionBean.This time create a Stateful SessionBean.

3-2

Press Finish . Interfaces and class will be generated. Now paste the contents as given.

StatefulBeanRemote.java

 Java |  copy code |? 
1
import javax.ejb.Remote;
2
 
3
@Remote
4
public interface StatefulBeanRemote {
5
    public void setMessage(String message);
6
 
7
    public String getMessage();
8
}
9

StatefulBeanLocal.java

 Java |  copy code |? 
1
import javax.ejb.Local;
2
 
3
@Local
4
public interface StatefulBeanLocal {
5
    public void setMessage(String message);
6
 
7
    public String getMessage();
8
}

StatefulBean.java

 Java |  copy code |? 
01
import javax.ejb.Stateful;
02
 
03
/**
04
 * Session Bean implementation class StatefulBean
05
 */
06
@Stateful
07
public class StatefulBean implements StatefulBeanRemote, StatefulBeanLocal {
08
    private String message;
09
 
10
    /**
11
     * Default constructor.
12
     */
13
    public StatefulBean() {
14
 
15
    }
16
 
17
    @Override
18
    public void setMessage(String message) {
19
        this.message = message;
20
    }
21
 
22
    @Override
23
    public String getMessage() {
24
        String reply;
25
        if (null == message) {
26
            reply = "Empty message";
27
        } else {
28
            reply = message;
29
        }
30
        return reply;
31
    }
32
 
33
}

6)Now right click on the server tab and create new JBoss 6.x server instance.Add our SessionBeanSample project to server run time.

4Then start the server.Once the deployment is done.The JNDI details will be displayed in the console.

INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

    StatefulBean/remote – EJB3.x Default Remote Business Interface

    StatefulBean/remote-com.beans.StatefulBeanRemote – EJB3.x Remote Business Interface

    StatefulBean/local – EJB3.x Default Local Business Interface

    StatefulBean/local-com.beans.StatefulBeanLocal – EJB3.x Local Business Interface

 [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

    StatelessBean/remote – EJB3.x Default Remote Business Interface

    StatelessBean/remote-com.beans.StatelessBeanRemote – EJB3.x Remote Business Interface

    StatelessBean/local – EJB3.x Default Local Business Interface

    StatelessBean/local-com.beans.StatelessBeanLocal – EJB3.x Local Business Interface

7)Now let us create a client project .Create a dynamic web project with JBoss 6.x as target run time.Create the following classes in it.

Client for Stateless Session Bean

StatelessClient.java

 Java |  copy code |? 
01
import java.util.Properties;
02
import javax.naming.InitialContext;
03
import javax.naming.NamingException;
04
import com.beans.StatelessBeanRemote;
05
 
06
public class StatelessClient {
07
 
08
    /**
09
     * @param args
10
     */
11
    public static void main(String[] args) {
12
        Properties props = new Properties();
13
        props.setProperty("java.naming.factory.initial",
14
                "org.jnp.interfaces.NamingContextFactory");
15
        props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
16
        props.setProperty("java.naming.provider.url", "127.0.0.1:1099");
17
        try {
18
            InitialContext ctx = new InitialContext(props);
19
            StatelessBeanRemote exampleBean = (StatelessBeanRemote) ctx
20
                    .lookup("StatelessBean/remote");
21
            exampleBean.setMessage("This is from main");
22
            StatelessClient client = new StatelessClient();
23
            client.doLookup();
24
            System.out.println("Message from Bean in main:"
25
                    + exampleBean.getMessage());
26
        } catch (NamingException e) {
27
            e.printStackTrace();
28
        }
29
 
30
    }
31
 
32
    public void doLookup() {
33
        Properties props = new Properties();
34
        props.setProperty("java.naming.factory.initial",
35
                "org.jnp.interfaces.NamingContextFactory");
36
        props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
37
        props.setProperty("java.naming.provider.url", "127.0.0.1:1099");
38
        try {
39
            InitialContext ctx = new InitialContext(props);
40
            StatelessBeanRemote exampleBean = (StatelessBeanRemote) ctx
41
                    .lookup("StatelessBean/remote");
42
            exampleBean.setMessage("This is from lookup");
43
            System.out.println("Message from Bean in doLookup:"
44
                    + exampleBean.getMessage());
45
        } catch (NamingException e) {
46
            e.printStackTrace();
47
        }
48
 
49
    }
50
 
51
}
52

Client for Stateful Session Bean

StatefulClient.java

 Java |  copy code |? 
01
import java.util.Properties;
02
import javax.naming.InitialContext;
03
import javax.naming.NamingException;
04
import com.beans.StatefulBeanRemote;
05
 
06
public class StatefulClient {
07
 
08
    public static void main(String[] args) {
09
        Properties props = new Properties();
10
        props.setProperty("java.naming.factory.initial",
11
                "org.jnp.interfaces.NamingContextFactory");
12
        props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
13
        props.setProperty("java.naming.provider.url", "127.0.0.1:1099");
14
        try {
15
            InitialContext ctx = new InitialContext(props);
16
            StatefulBeanRemote exampleBean = (StatefulBeanRemote) ctx
17
                    .lookup("StatefulBean/remote");
18
            exampleBean.setMessage("This is from main");
19
            StatelessClient client = new StatelessClient();
20
            client.doLookup();
21
            System.out.println("Message from Bean in main:"
22
                    + exampleBean.getMessage());
23
        } catch (NamingException e) {
24
            e.printStackTrace();
25
        }
26
 
27
    }
28
 
29
    public void doLookup() {
30
        Properties props = new Properties();
31
        props.setProperty("java.naming.factory.initial",
32
                "org.jnp.interfaces.NamingContextFactory");
33
        props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
34
        props.setProperty("java.naming.provider.url", "127.0.0.1:1099");
35
        try {
36
            InitialContext ctx = new InitialContext(props);
37
            StatefulBeanRemote exampleBean = (StatefulBeanRemote) ctx
38
                    .lookup("StatefulBean/remote");
39
            exampleBean.setMessage("This is from lookup");
40
            System.out.println("Message from Bean in doLookup:"
41
                    + exampleBean.getMessage());
42
        } catch (NamingException e) {
43
            e.printStackTrace();
44
        }
45
 
46
    }
47
 
48
}

Output

Output of Stateless Session Bean

Compile and run StatelessClient.java.The output is:

Message from Bean in doLookup:This is from lookup

Message from Bean in main:This is from lookup

The output indicates that the same bean reference is reused by the container. For the second access , the existing instance is reused.

Output of Stateful Session Bean

Compile and run StatefulClient.java.The output is:

Message from Bean in doLookup:This is from lookup

Message from Bean in main:This is from main

The output indicates that different instances were created for each access from client.

See Related Topics

EJB Overview

Stateless Session Bean Example

Stateful Session Bean example

EJB Message Driven Bean example

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>