# EJB explanation

In 
Published 2022-12-03

This tutorial explains to you what an EJB is in Java.

EJB is an acronym for Enterprise Java Bean. The Enterprise Java Bean (EJB) is a development architecture for building highly scalable and robust enterprise level applications to be deployed on J2EE compliant Application Server such as WebSphere, JBOSS, WebLogic, etc.

An EJB is a server-side component that contains business logic for an application. Here is an EJB overview :

As you can see, the Enterprise Java Bean (EJB) is a java code that run on the EJB container. All the business logic is done on the EJB container by the usage of Enterprise Java Beans. Prior to the EJB3, the Persistence was done by the EJB as well. Since the release of Java 5, the use of EJB entity beans has been discouraged and essentially replaced by the Java Persistence API (JPA).

An EJB can be called by a desktop application or by a class from the Web Container. A servlet, a JSP or a JSF can call an Enterprise Java Bean (EJB).

The usage of EJB technology enables a rapid and simplified development of distributed, transactional, secure and portable Java EE Web applications and Java desktop applications.

Starting with EJB3, there are 2 main types of EJBs:

1 - Session Beans 2 - Message Driven Beans

Session Beans

A session bean implements a process or a functionality (business logic) that can be invoked programmatically by a client over local, remote or by a client web service.

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

Message Driven Beans

Message Driven Beans (MDBs) also known as Message Beans. Message Driven Beans are business objects whose execution is triggered by messages instead of by method calls. For more details and examples you can take a look at the page related to the Java Message Service (JMS).