#
Stateful Session Bean
This tutorial explains to you what a Stateful Session Bean in Java is.
Starting with EJB 3, there are 2 main types of EJBs:
- Session Beans
- Message Driven Beans
We have 3 type of Session Beans in Java:
Stateless
Session Bean : does not maintain the conversational state with the client. After the invocation, the instance variables of the class (related to the client) are not retained. For more details and an example you can take a look at the article named "Stateless Session Bean in Java (with example)".Stateful
Session Bean : maintain the conversational state with the client. After the invocation, the instance variables of the class (related to the client) are retained.SINGLETON
Session Bean : A singleton session bean is instantiated once per application (per Java Virtual Machine) and exists for the whole lifecycle of the java application. The state and the functionalities of a singleton session bean are shared across the application. For the singleton session bean the concurrency could be managed at the container level or at the bean level. For more details and an example you can take a look at the article named "Singleton Session Bean in Java (with example)".
More information about the EJB you can get from the article named "What is an EJB in Java ?".
In this article I will show you how to create an Enterprise Java Bean (EJB) which will be deployed on an Oracle WebLogic 12c server.
This article is using:
- Eclipse Java EE IDE for Web Developers, Version: Neon.3 Release (4.6.3)
- Oracle WebLogic 12c
- Java 8
Here are the steps for creating a Stateful EJB:
- Create an EJB project (I use Eclipse for my example):
Choose File -> New Project and you will see :
Click on "Next" and the following screen will appear:
Choose the name of the project, the Target Runtime, the EJB module version (3.x), etc and click on "Finish". In my case the "Finish" button is disabled because another project exists with this name.
- Configure Maven
In pom.xml you must add the following dependencies:
Build the project with Maven to download the needed dependencies (the Goal must be "install").
Code the EJB3 classes
You have to create the Remote Interface (or Local Interface or both) for the Stateful Session Bean:
import javax.ejb.Remote;
@Remote
public interface AddIntegersRemoteInterface {
public void addValue(int a);
public int returnSum();
}
Create the interface implementation for the Stateful Session Bean:
import javax.ejb.Stateful;
@Stateful(mappedName="/AddIntegers")
public class AddIntegers implements AddIntegersRemoteInterface {
private int sum_var;
public AddIntegers() {
super();
sum_var = 0;
}
@Override
public void addValue(int a) {
// What the EJB3 will return
sum_var = sum_var + a;
}
@Override
public int returnSum() {
// What the EJB3 will return
return sum_var;
}
}
When the code is done the project looks like:
Run the project on Oracle WebLogic Server from Eclipse and you will see the EJB into the Oracle WebLogic Deployments page:
In this example I use the Admin Server for deployment, but on Production the EJB will be deployed on a Managed Server.