Overview :
In this article we will learn what are Enterprise JavaBeans ( EJBs ), what are it's different types
and why to use EJBs in your application.
What are EJBs ?
EJB stands for "Enterprise JavaBeans" which are distributed network aware components for developing secure,
scalable, transactional and multi-user components in a J2EE environment.
Above definition actually describes EJBs from functional point of view i.e. what they do. A more
structural definition would be : "EJBs are collection of Java classes, interfaces and XML files
adhering to given rules".
Load up by having
managed hosting for your server needs.
What do EJBs provide ?
In J2EE all the components run inside their own containers. JSP, Servlets and JavaBeans have their
own web container. Similarly EJBs run inside EJB container. The container provides certain built-in
services to EJBs which the EJBs use to function. The services that EJB container provides are :
- Component Pooling
- Resource Management
- Transaction Management
- Security
- Persistence
- Handling of multiple clients
i. Component Pooling :
The EJB container handles the pooling of EJB components. If there are no requests for a particular
EJB then the container will probably contain zero or one instance of that component in memory. If need
arises then it will increase component instances to satisfy all incoming requests. Then again if number of requests
decrease, container will decrease the component instances in the pool. The best thing is that the client
is absolutely unaware of this component pooling and the container handles all this for you.
ii. Resource Management :
The container is also responsible for maintaining database connection pools. It provides you a
standard way of obtaining and returning database connections. The container also manages EJB environment
references and references to other EJBs. The container manages following types of resources and makes them
available to EJBs :
- JDBC 2.0 Data Sources
- JavaMail Sessions
- JMS Queues and Topics
- URL Resources
- Legacy Enterprise Systems via J2EE Connector Architecture
iii. Transaction Management :
This is probably the single most important factor of all. A transaction is a single unit of work,
composed of one or more steps. If all the steps succeed then the transaction is committed otherwise
it is rolled back. There are different types of transactions and it is absolutely unimaginable to not
to use transactions in today's business environments. We will learn more about transactions in a
separate article.
iv. Security.
The EJB container provides it's own authentication and authorization control, allowing only specific
clients to interact with the business process. There is no need for you to create a security architecture
of your own, you are provided with a built-in system, all you have to do is to use it.
v. Persistence :
The container if desired can also maintain persistent data of our application. The container is then
responsible for retrieving and saving the data for us while taking care of concurrent access from multiple
clients and not corrupting the data.
vi. Handling of multiple clients.
The EJB container handles multiple clients of different types. A JSP based thin client can interact with
EJBs with same ease as that of GUI based thick client. The container is smart enough to allow even non-Java
clients like COM based applications to interact with the EJB system. Like before the EJB container handles
it all for you.
On the next page we learn about different types of EJBs.
Types of EJBs :
There are three types of EJBs :
- Session Beans
- Entity Beans
- Message-Driven Beans
i. Session Beans :
Session beans are the brain of our EJB based application. Their methods should decide what should or
should not happen. They maintain the business logic. The client directly interacts with them, they are
of two types :
- Stateless Session Beans
They do not maintain state in class level variables. They are thus fastest and most efficient of all.
Since they do not save their state in class level variables, they are called as "Stateless".
- Stateful Session Beans
They do maintain state in class level variables and each client is provided a different and specific
Stateful Session bean. This is different from Stateless Session beans in which case the client can be
provided any Stateless Session bean between requests since the Stateless Session beans haven't managed their
state, to the client, all of them are equal. Due to this fact, Stateful Session beans are heavy beans,
while Stateless Session beans are light and efficient.
ii. Entity Beans :
Entity beans represent data in an EJB system. That data can reside in any data source including database.
Entity beans are also of two types :
- Container-Managed Persistence Entity Beans :
CMP bean's persistence is managed by the EJB container which is responsible for saving and retrieving the data
from the underlying database for us. CMP beans are easier to develop and work well with database
access.
- Bean-Managed Persistence Entity Beans :
BMP bean's persistence has to be managed by the EJB itself. While it gives more control to the EJB developer
to save and retrieve data, they are harder to build as more coding is required. One use of BMP
beans would be to retrieve and save data from non-database data sources like XML files, JMS resources
etc.
iii. Message-Driven Beans :
They are new in EJB 2.0 specification and provide a mechanism for EJBs to respond to asynchronous
JMS messages from different sources. Message-Driven beans and JMS have opened a new paradigm for J2EE
developers to create MOM ( Message Oriented Middleware ) based applications. We will learn more and more
about this in separate articles on Stardeveloper.
Why to use EJBs ?
Developers have shown that good online applications can be created using JSP, Servlets and
JavaBeans alone, without using EJBs. This has raised the question that why should we use EJB? People who ask this
question bring two important points in favor of not using EJBs :
- EJB Containers are expensive.
- EJB based systems turn out to more complex to develop and maintain.
I'll try to answer these questions now. First of all the reason EJB containers are expensive and their
license fees turn out to be thousands of dollars for a single instance of that container is simple, the whole
business process depends on that EJB container ( application server ) to run. Now if due to any reason there is a
bug or if the application server crashes all of a sudden then it is going to halt the business application, something
which business people wouldn't want to see happening even in dreams.
Due to the dependency of the whole business
process on these application server, the application server vendors spend lot of money on developing truly robust,
secure and scalable application servers which are fault-tolerant and work in clusters to provide fail-safe
operations. They also provide certain additional features like load balancing to prevent one server from
becoming bobbed down from load. Current application servers also act as transactional processing monitors and
work in the transactional system as heart in the body.