Table of contents
1.
Introduction
2.
Architecture
3.
Application Server
4.
Container
5.
Beans
5.1.
1. Session Beans
5.2.
2. Message-Driven Beans
5.3.
3. Entity Beans (Deprecated)
6.
Difference between EJB and JB
7.
Frequently Asked Questions
7.1.
What is the difference between stateful & stateless session beans?
7.2.
Can EJBs be accessed remotely?
7.3.
Are entity beans still used in modern EJB applications?
8.
Conclusion
Last Updated: Aug 3, 2024
Easy

Enterprise Java Beans (EJB) Architecture

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Enterprise Java Beans (EJB) is a technology used to create reusable & scalable business logic components in Java. It is part of the Java Enterprise Edition (Java EE) platform. EJB helps developers focus on writing business logic without worrying about low-level details like transactions, security, concurrency & other system-level services. 

Enterprise Java Beans (EJB)  Architecture

In this article, we will learn about the EJB architecture, its components, and how it works. We will also discuss the differences between EJB and regular Java Beans.

Architecture

The EJB architecture is designed to support the development of distributed, transactional, secure & portable applications. It follows a container-based model where the EJB container provides system-level services to the EJB components. The main components of the EJB architecture are:

1. EJB Container: It is a runtime environment that manages the execution of EJBs. It provides services such as transaction management, security, concurrency control, and resource pooling. The container also handles the lifecycle of EJBs, including creation, destruction & passivation.
 

2. EJB Component: It is a server-side component that encapsulates the business logic. EJBs are deployed in the EJB container & can be accessed remotely by clients. There are three types of EJBs:

   - Session Beans: They represent a single client's interaction with the EJB container. They can be stateful or stateless.
 

   - Message-Driven Beans: They allow asynchronous processing of messages from a message queue or topic.
 

   - Entity Beans: They represent persistent data stored in a database. (Note: Entity Beans are deprecated in EJB 3.x & replaced by the Java Persistence API)
 

3. EJB Remote Interface: It defines the business methods that can be invoked remotely by clients. It is a Java interface that extends the javax.ejb.EJBObject interface.
 

4. EJB Home Interface: It provides methods for creating, finding & removing EJB instances. It is a Java interface that extends the javax.ejb.EJBHome interface.
 

5. Deployment Descriptor: It is an XML file (ejb-jar.xml) that specifies the EJB's configuration, such as transaction attributes, security roles & environment entries.


Let’s look at a simple example of an EJB component:

@Stateless
public class MyEJB {
    public void businessMethod() {
        // Business logic goes here
    }
}

Application Server

An application server is a software framework that provides an environment for developing & running enterprise applications, including EJBs. It handles the low-level details of managing the application, such as transaction management, security, and resource pooling. Some popular application servers that support EJB are:

1. Oracle WebLogic Server
 

2. IBM WebSphere Application Server
 

3. JBoss Enterprise Application Platform
 

4. GlassFish Server
 

5. Apache TomEE


To deploy an EJB application on an application server, you need to package the EJB components, along with the deployment descriptor, into an EJB JAR file. The EJB JAR file is then bundled with other application components, such as web modules & libraries, into an Enterprise Archive (EAR) file. The EAR file is then deployed on the application server.

Let’s look at an example of how to deploy an EJB application on JBoss EAP using the command line:

$ jboss-cli.sh --connect --command="deploy myapp.ear"


The application server provides a runtime environment for the EJB components & manages their lifecycle. It also provides a naming & directory service (JNDI) for locating & accessing EJB components.

Container

The EJB container is a runtime environment that manages the execution of EJBs. It provides a set of services to the EJB components, such as transaction management, security, concurrency control, and resource pooling. The container also handles the lifecycle of EJBs, including creation, destruction & passivation.

The EJB container provides the following services to the EJB components:

1. Transaction Management: The container manages transactions for the EJB components. It ensures that all the operations within a transaction are completed successfully or rolled back in case of a failure.
 

2. Security: The container provides security services to the EJB components, such as authentication & authorization. It ensures that only authorized clients can access the EJB methods.
 

3. Concurrency Control: The container manages the concurrent access to the EJB components. It ensures that multiple clients can access the EJB methods simultaneously without causing data inconsistency.
 

4. Resource Pooling: The container manages the resource pooling for the EJB components. It creates & manages a pool of EJB instances to improve performance & scalability.
 

5. Lifecycle Management: The container manages the lifecycle of the EJB instances, including creation, destruction & passivation. It also provides callbacks to the EJB components during the lifecycle events.


Let’s look at an example of how the EJB container manages transactions:

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class MyEJB {
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void businessMethod() {
        // Business logic goes here
    }
}


In this example, the `@TransactionManagement` annotation specifies that the container will manage transactions for the EJB. The `@TransactionAttribute` annotation specifies that the `businessMethod()` requires a transaction. If a transaction is not already active, the container will create a new transaction before invoking the method.

Beans

EJB components, also known as beans, are server-side components that encapsulate the business logic of an application. There are three types of beans in EJB:

1. Session Beans

Session beans represent a single client's interaction with the EJB container. They can be stateful or stateless. Stateful session beans maintain the state of the client across multiple method invocations, while stateless session beans do not maintain any state. Session beans are typically used to perform business logic, such as processing orders or managing user sessions.

For example : 

@Stateless
public class MySessionBean {
    public void businessMethod() {
        // Business logic goes here
    }
}

2. Message-Driven Beans

Message-driven beans (MDBs) allow asynchronous processing of messages from a message queue or topic. They act as message listeners and are invoked by the EJB container when a message arrives. MDBs are typically used to process messages asynchronously, such as sending emails or generating reports.

For example : 

@MessageDriven(activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "jms/MyQueue")
})
public class MyMDB implements MessageListener {
    public void onMessage(Message message) {
        // Message processing logic goes here
    }
}

3. Entity Beans (Deprecated)

Entity beans represent persistent data stored in a database. They are used to access & modify data in a database. However, entity beans are deprecated in EJB 3.x & have been replaced by the Java Persistence API (JPA).

Beans are deployed in the EJB container & can be accessed remotely by clients using the EJB remote interface. The EJB home interface provides methods for creating, finding & removing bean instances.

Difference between EJB and JB

Feature

EJB

Java Beans

DeploymentEJBs are deployed in an EJB container on an application serverJava Beans are deployed in a Java application
Lifecycle ManagementEJB container manages the lifecycle of EJB instancesJava application manages the lifecycle of Java Beans
Transaction ManagementEJB container provides declarative transaction managementJava applications must handle transactions programmatically
SecurityEJB container provides declarative securityJava applications must handle security programmatically
ConcurrencyEJB container manages concurrent access to EJB instancesJava applications must handle concurrency programmatically
Resource PoolingEJB container manages resource pooling for EJB instancesJava applications must manage resource pooling programmatically
Remote AccessEJBs can be accessed remotely using remote interfacesJava Beans are typically accessed locally within the same JVM
MessagingEJBs can be message-driven & process messages asynchronouslyJava Beans do not have built-in support for messaging
PersistenceEJBs can use the Java Persistence API (JPA) for persistenceJava Beans can use JPA or other persistence frameworks
ComplexityEJBs have a steeper learning curve & are more complex to developJava Beans are simpler & easier to develop

 

Frequently Asked Questions

What is the difference between stateful & stateless session beans?

Stateful session beans maintain the state of the client across multiple method invocations, while stateless session beans do not maintain any state.

Can EJBs be accessed remotely?

Yes, EJBs can be accessed remotely using remote interfaces.

Are entity beans still used in modern EJB applications?

No, entity beans are deprecated in EJB 3.x & have been replaced by the Java Persistence API (JPA).

Conclusion

In this article, we learned about the EJB architecture & its components, like the EJB container, beans & interfaces. We also discussed the differences between EJB and regular Java Beans. EJB provides a powerful framework for developing enterprise applications with features such as transaction management, security, concurrency & messaging. 

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass