What is Enterprise Beans
A Java EE
component is called an Enterprise Beans which implements the EJB technology and
runs inside an EJB container. Enterprise Beans are the server side component
where the application's business logic is encapsulated. The business logic can
be explained as, it is line of statement called code which is written for
completing the requirements of application.
What is EJB Container
EJB container is a
specification within the Application Server that runs the Enterprise Beans. Services
provided by the EJB container is System-level services. In such type of
services it provides the transaction and security to its enterprise beans. In
addition they also provides for the Resource and life cycle management, remote
accessibility, concurrency control, collision etc.
Benefits of Enterprise Beans
Generally, Enterprise beans is used for
creating large, distributed applications. This is because of various reasons
these are as follows :
- The
first reason is the services that are provided by the EJB container to
enterprise beans i.e. System-level services that helps the developer to
focus on solving the business problems.
- The
second reason is elaborated from first reason that the bean is responsible
for application's business logic not for the client. So, a client
developer can focus on only its presentation.
- The
third reason is enterprise bean's portability feature i.e. the enterprise
beans components can be easily or conveniently portable.
When
use Enterprise Java Bean?
- Application
needs Remote Access. In other words, it
is distributed.
- Application
needs to be scalable. EJB applications
supports load balancing, clustering and fail-over.
- Application
needs encapsulated business logic.
EJB application is separated from presentation and persistent layer.
Types
of Enterprise Java Bean
There are 3 types of enterprise bean in
java.
Session Bean
Session bean contains business logic that
can be invoked by local, remote or webservice client.
Message Driven Bean
Like Session Bean, it contains the business
logic but it is invoked by passing message.
Entity Bean
It encapsulates the state that can be
persisted in the database. It is deprecated. Now, it is replaced with JPA (Java
Persistent API).
Session
Bean
These types of beans directly interact with
the client and contains business logic of the business application.
Session bean
encapsulates business logic only, it can be invoked by local, remote and
webservice client.
It can be used for
calculations, database access etc.
The life cycle of
session bean is maintained by the application server (EJB Container).
Types
of Session Bean
There are 3 types
of session bean.
1)
Stateless Session Bean: It doesn't maintain
state of a client between multiple method calls.
2)
Stateful Session Bean: It maintains state of a
client across multiple requests.
3)
Singleton Session Bean: One instance per
application, it is shared between clients and supports concurrent access.
Stateless
Session Bean
Stateless
Session bean is a business object that
represents business logic only. It doesn't have state (data).
In other
words, conversational state between multiple method calls is
not maintained by the container in case of stateless session bean.
The stateless bean
objects are pooled by the EJB container to service the request on demand.
It can be accessed
by one client at a time. In case of concurrent access, EJB container routes
each request to different instance.
Stateful
Session Bean
Stateful
Session bean is a business object that
represents business logic like stateless session bean. But, it
maintains state (data).
In other
words, conversational state between multiple method calls is
maintained by the container in stateful session bean.
Message-Driven Bean :
Message-Driven
Bean permits the Java Enterprise Edition applications for processing messages
asynchronously. Message-Driven beans likewise the JMS message listener, that
receives the JMS messages rather than events.
Disadvantages
of EJB
- Requires
application server
- Requires
only java client. For other language client, you need to go for
webservice.
- Complex
to understand and develop ejb applications.