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:

  1. life cycle management,
  2. security,
  3. transaction management, and
  4. 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?

  1. Application needs Remote Access. In other words, it is distributed.
  2. Application needs to be scalable. EJB applications supports load balancing, clustering and fail-over.
  3. 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).

Types of Enterprise Bean

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 .netphp etc, we need to go with webservices (SOAP or REST). So EJB with web service will be better option.

Disadvantages of EJB

  1. Requires application server
  2. Requires only java client. For other language client, you need to go for webservice.
  3. Complex to understand and develop ejb applications.