# Stateless Session Bean

In 
Published 2022-12-03

This tutorial explains ou what a Stateless Session Bean in Java is.

Starting with EJB3, 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.

  • 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. For more details and an example you can take a look at the article named "Stateful Session Bean in Java (with example)".

  • 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)".

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 an EJB:

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

  1. Configure Maven

In pom.xml you must add the following dependencies:

  1. Build the project with Maven to download the needed dependencies (the Goal must be "install").

  2. Code the EJB3 classes

You have to create the Remote Interface (or Local Interface or both):

AddIntegersRemoteInterface.java
package ejb;
import javax.ejb.Remote; 
 
@Remote
public interface AddIntegersRemoteInterface {
 
    public int returnSuma(int a, int b);
}

Create the interface implementation:

package ejb;
 
import javax.ejb.Stateless;

@Stateless(mappedName="/AddIntegers")
public class AddIntegers implements AddIntegersRemoteInterface {
 
    @Override
    public int returnSuma(int a, int b) {
        // What the EJB3 will return
        return a+b;
    }
}

When the code is done the project looks like:

![](../pictures/ejb/stateless-session-bean/4-create-java-ejb3-example-project-look.png)

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.