What is EJB
EJB is an acronym for enterprise java bean. It is a specification provided by Sun Microsystems to develop secured, robust and scalable distributed applications.
To get information about distributed applications, visit RMI Tutorial first.
To run EJB application, you need an application server (EJB Container) such as Jboss, Glassfish, Weblogic, Websphere etc. It performs:
- life cycle management,
- transaction management, and
- object pooling.
EJB application is deployed on the server, so it is called server side component also.
EJB is like COM (Component Object Model) provided by Microsoft. But, it is different from Java Bean, RMI and Web Services.
# 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).
# Difference between RMI and EJB
Both RMI and EJB, provides services to access an object running in another JVM (known as remote object) from another JVM. The differences between RMI and EJB are given below:
|In RMI, middleware services such as security, transaction management, object pooling etc. need to be done by the java programmer.||In EJB, middleware services are provided by EJB Container automatically.|
|RMI is not a server-side component. It is not required to be deployed on the server.||EJB is a server-side component, it is required to be deployed on the server.|
|RMI is built on the top of socket programming.||EJB technology is built on the top of RMI.|
# EJB and Webservice
In EJB, bean component and bean client both must be written in java language.
If bean client need to be written in other language such as .net, php etc, we need to go with webservices (SOAP or REST). So EJB with web service will be better option.
# 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.