What is Enterprise JavaBeans (EJB)? – Architecture, Types, Advantages, Disadvantages

EJB stands for Enterprise JavaBeans. EJB is a server-side component written in the Java programming language. Enterprise JavaBeans encapsulates the business logic of applications. Enterprise JavaBeans are typically deployed in EJB containers and run on the EJB servers.

EJB Architecture and its Components

EJB Architecture has three main components. They are

  • Enterprise Beans
  • EJB Container
  • Java Application Server

In general, Enterprise Beans run inside an EJB Container whereas EJB Container runs in a Java Application Server. This is how EJB Architecture works.

EJB container has the environment in which one or more Enterprise JavaBeans run. It provides the following control and management services.

  • Life Cycle Management.
  • Naming services.
  • Security checks.
  • Resource pooling.
  • persistence management.
  • Transactions coordinations.
  • Bean runtime context information(meta data).

Types of Enterprise JavaBeans

There are three types of Enterprise JavaBeans – Session beans, Entity beans, and Message-driven beans.

1. Session Bean: It stores the information of a particular client for a single session. Ecan Session bean is associated with one EJB client. It is created and destroyed by the particular client that is associated with it. It either can have stateless or stateful. A session bean can be destroyed if at all the EJB server crashes. Session bean does not represent data that must be stored in a database.

2. Entity Bean: It represents persistent data storage. In a persistent storage mechanism, an Entity bean denotes the business object. The Application Server has a relational database which is the persistent storage mechanism. An entity bean can be shared by multiple EJB clients. An entity bean can share access from many users. These are persistent and can survive when the EJB server crashes.

3. Message-Driven Bean: It is the third form of EJB, which you can initiate by sending a request from Java Message Service (JMS). Message-Driven bean includes the business logic same as in the session bean but it is invoked by passing the message. It can consume JMS messages from external entities.

When to Use Enterprise JavaBeans?

  • If the Application needs remote access i.e. if it is distributed.
  • If you need the Application to be Scalable you can go with Enterprise JavaBeans as it supports load balancing, clustering, and failover.
  • If the Application needs encapsulated business logic you can go with Enterprise JavaBeans. The application can be differentiated from the persistent and demonstration layer.

Advantages of Enterprise JavaBeans (EJB)

  • Enterprise JavaBeans can simplify the development of large, and distributed applications.
  • The EJB container – and not the Bean Developer – provides system-level services, such as transaction management and security authorization. So the developer has to focus only on the business logic of the applications.
  • EJB is of portable components, so the developer can build new applications by using the existing beans.

Disadvantages of Enterprise JavaBeans (EJB)

  • To run the EJB Applications they need an application server.
  • Developing the EJB applications requires only Java Developer.
  • Complex to develop EJB applications.