EJB Architecture Java
Java beans incorporate a set of objects into one accessible object that can be accessed easily from any application. This single accessible object is maintainable, customizable, and reusable. The setter/getter method and the single public constructor are used to govern that single accessible object. We can update and read the value of any variable of any object by using the setter and getter, respectively.
The EJB stands for Enterprise Java beans that is a server-based architecture that follows the specifications and requirements of the enterprise environment. EJB is conceptually based on the Java RMI(Remote Method Invocation) specification. In EJB, the beans are run in a container having four-tier architecture. This architecture consists of four layers, i.e., Client layer, Web layer, Application layer, and Data layer.
Architecture
The EJB architecture has two main layers, i.e., Application Server and EJB Container, based on which the EJB architecture exist. The graphical representation of the EJB architecture is given below.
In the above diagram, the logical representation of how EJBs are invoked and deployed by using RMI(Remote Method Invocation) is defined. The containers of the EJB cannot be self-deployed. In order to deploy the containers, it requires the Application server.
Application Server
In the EJB architecture, the Application server is the outermost layer that holds or contains the Container to be deployed. The application layer plays an important role in executing the application developed using the beans. It provides the necessary environment to execute those applications. Some most popular application servers are Web-logic, Tomcat, JBoss, Web-sphere, Wildfly, and Glass-finish. The main tasks of the application server are:
- Manage Interfaces
- Execution of the processes
- Connecting to the database
- Manage other resources.
Container
In EJB architecture, the Container is the second outermost layer. It is a very important layer for enterprise beans that are contained in it. For the enterprise bean, the Container provides various supporting services, which are as follows:
- It provides support for transactional services such as registering the objects, assign remote interfaces, purge the instances.
- It provides support for monitoring the object’s activities and coordinating distributed components.
- It provides support for security services.
- It provides support for the pooling of resources.
- It provides support for managing the Life-cycle of beans and their concurrency.
- It provides support to concentrate on business logic.
Beans
Java beans of the enterprise are installed in the Container in the same way as a Plain old java object (POJO) is installed and registered to the Container. For developing secured, large scale and robust business applications, beans provide business logic.
Types of EJB
There are three types of Enterprise Java Beans or EJB available, which are as follows:
- Stateless Enterprise Java Beans
- Stateful Enterprise Java Beans
- Message-driven Enterprise Java Beans
Stateless EJB
In order to implement the stateless business logic, the stateless EJBs are primarily used. Storing a user’s physical address into an inventory system’s database is an example of the stateless EJB. In addition, a stateless bean would be perfect for this type of business logic because it is not necessary to have more than two forms on the user interface at all stages of the transaction.
StatelessBeanExample.java
Stateful EJB
The Stateful EJB is just opposite to the Stateless EJB. The Stateful EJBs are used when we have to maintain the state of the application on the backend during the user session. The shopping cart of an online shopping application is an example of the Stateful EJB. In order to achieve such application, we will use the following steps:
- We will create the stateful session bean.
- After that, for temporarily storing the selected products that are within the user session on the backend, we have to create a global variable collection of type products.
- Next, we will create a method through which we will add all the selected products to the collection that we create in the previous step.
- We also create a method through which we will remove the product from the collection.
- In the end, we will create a checkout method for the selected products to process.
StatelessBeanExample.java
Message-driven EJB
Another special type of EJB that is used for sending and receiving messages from message brokers implements the JMS specification. The systems based on the broker are loosely coupled. The components that communicate through the broker have the advantage of not waiting for one request to finish before submitting another request because the broker by nature is asynchronous.
MessageDrivenBeanExample.java
Difference b/w EJB and JB
These are the following differences between EJB and JB:
S.No. | EJB | JB |
---|---|---|
1. | EJB is not visible because it runs as a remote. | JB is visible. |
2. | EJB is executed on the server-side. | EJB can execute on both the client-side and the server-side. |
3. | EJB works with an external builder tool or its IDE. | For interpreting the bean’s functionality, EJB uses its external interface. |
4. | EJB uses the technology of components but cannot build or extend it over beans. | By using generic components created by Java beans, it is able to build applet and application. |
5. | It has no property editor, customizer, and bean information classes. It has only information about what is provided by the deployment descriptor. | It has property editors, customizers, and bean information classes. |
6. | It supports transactions. | It doesn’t support transactions. |
7. | Three types of EJB possible. | No types. |