Introduction to EJB Component in Java EE Architecture by MyExamCloud

Brief introduction about Enterprise Java Bean (EJB) usage in Java EE application architecture.

This article deeply discusses the usage of EJB component in Java EE architecture. Before start reading about EJB, it is advisable to understand the history behind component-container architecture.

The WWW technology is moved from 2 tier to 3 tier, 3 tier to n-tier and now n-tier to SOA based solutions for easy integration and maintenance.

Single Tier

Mainframe based systems where presentation, business logic and database are on same node.


Client-Server model where presentation logic is moved to client machine.


Introducing browser based applications where web server will handle all requests. Presentation, Business Logic and Database are separated.


Introducing component-container approach with application server to run business logic components. Presentation, Business Logic, Integration (Data/Legacy) and Database.


Service Oriented Architecture to interact with other systems. Both WSDL based SOAP or JSON based web services are getting more popular in current trend.

Java EE Containers:

Normally, in N-Tiered applications we need to handle transaction and state management, multi-threading, resource pooling, and other complex low-level services. The component-based and platform-independent Java EE architecture simplifies these services with the help of containers. We just need to write business logic and it is organized into reusable components.

The Java EE server provides underlying services in the form of a container for every component type. Because we do not have to develop these services, we are free to concentrate on solving the business problem at hand.

Introducing EJB Component:

EJB is a component to write business logic in an enterprise application and it runs inside a container named EJB container. The typical Java EE application server architecture is shown below:

Java EE Application Server

The run-time portion of a Java EE product. A Java EE application server provides EJB and Web containers.

Enterprise JavaBeans (EJB) container

Manages the execution of EJBs for Java EE applications. Enterprise beans and their container run on the Java EE application server.

Web container

Manages the execution of JSP page and servlet components for Java EE applications. Web components and their container run on the Java EE application server.

Application client container

Manages the execution of application client components. Application clients and their container run on the client.

Applet container

Manages the execution of applets. Consists of a Web browser and Java Plug-in running on the client together.

Java EE 6 provides the following EJB components.

  1. Stateful Session Bean
  2. Stateless Session Bean
  3. Singleton Session Bean
  4. Message Driven Bean

Note that Singleton bean is under session type introduced in Java EE 6.

EJB Design decision
Using Stateless Session Beans
You are developing a business component where it does not contain the data for a specific client. Your search component will be a best example for this scenario. Note that search requires only one method invocation with search query among all the clients to perform the generic task (executing search query on server side).
Example: Google search, Yahoo search and MSN search.

The following contents are taken from Oracle:
When to use Stateful Session Bean:

  • If the bean does not contain the data for a specific client.
  • If there is only one method invocation among all the clients to perform the generic task.

Using Stateful Session Beans
You are developing a business component where it wants to hold information about the client across method invocation. The most common example for this type of implementation would be “Shopping Cart”, where it needs to hold client specific information.
Example: Gmail, Yahoo Mail and Facebook.

The following contents are taken from Oracle:
When to use Stateful Session Bean:

  • What the bean wants to holds information about the client across method invocation.
  • When the bean works as the mediator between the client and the other component of the application.
  • When the bean have to manage the workflow of several other enterprise beans.

Using Singleton Session Beans
You can easily decide by the name itself, when you want to have exactly one instance of a session bean go for Singleton session bean. You are developing an application where you want to have some configuration parameters needs to be used across different modules and they needs to be available during your server startup. You can easily implement this scenario by using Singleton Session bean.

The following contents are taken from Oracle:
When to use Singleton Session Bean:

  • State needs to be shared across the application.
  • A single enterprise bean needs to be accessed by multiple threads concurrently.
  • The application needs an enterprise bean to perform tasks upon application startup and shutdown.
  • The bean implements a web service.

Using Message Driven Beans
Unless like regular Java method calls MDBs are called asynchronously without blocking. Note that Java EE 6. Note that Session beans allow you to send JMS messages and to receive them synchronously but not asynchronously. To avoid tying up server resources, do not to use blocking synchronous receives in a server-side component; in general, JMS messages should not be sent or received synchronously. To receive messages asynchronously, we should use a message-driven bean.

One better way to study EJB is by taking Oracle’s EJB Certification. The certification tests coding/design knowledge in the following areas:

  • Java EE design choices about using different EJB components.
  • Implementing Session Beans with annotations.
  • How to access Session Beans from other components or clients.
  • Implementing Singleton Session Bean
  • The exam requires knowledge in developing Java EE Applications Using Messaging and Message-Driven Beans.
  • Using Timer Services in EJB components.
  • The other topics you must have good knowledge are Interceptor Classes and Methods, Transactions and Security.

You can check the details at MyExamCloud’s EJB Certification Study Plan at

Leave a Reply