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.
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.
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:
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:
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:
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.
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:
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.
Difference b/w EJB and JB
These are the following differences between EJB and JB: