The EJB component model

As a component model, EJB defines three bean types:

  • Session Bean: The session bean stores the data of a particular user's session. The lifetime of a session bean is limited to the duration of the interaction between the client and the bean. The client normally creates a session bean and invokes the functions. The EJB container removes the bean when it is no longer required. Session beans can be further classified into the following three types:
    • Stateless Bean: The state information between the client and bean is not maintained in a stateless bean. When a function of a stateless bean is invoked by a client, the instance variables of the beans may have some client-specific state information. This information exists only during the invocation of that function. When the function finishes its execution, the client-specific state will not be saved. A stateless session bean is shared between the different clients.
    • Stateful Bean: The EJB container maintains the state information of the bean and the client in a stateful bean. A stateful session bean is created only for a specific client and it is not shared among the clients.
      • Singleton Bean: A singleton bean is instantiated only once in the application and exists throughout its life cycle. A singleton bean is shared among the different clients and can be accessed simultaneously. Singleton beans are instantiated during the start of the application and are removed before the application goes down. Singleton classes are suitable for cases in which we need to initialize or clean up resources at an application level.
  • Entity Bean: Entity beans store the data in a data store, which is usually a relational database. Entity beans are often referred to as data-persistent units as they store the data that they carry. Entity beans have attributes and functions. Attributes are used to store data in a secondary store and functions perform operations on them. Entity beans are always shared among the clients. The EJB specifications define two entity bean models:
    • Bean-managed Persistence: In this model, a bean is responsible for managing the data persistence of the bean's state information. When creating the bean, we must implement details such as connecting to a database and interacting with the database to store the information.
    • Container-managed Persistence: In this model, the EJB container manages the data-persistence aspects. The container is responsible for connecting to the database and storing state interaction between the client and the bean in the database.
  • Message-driven Bean: A message-driven bean listens to messages that flow in an asynchronous fashion. When an EJB-based application needs to receive messages asynchronously from other systems, we can use message-driven beans. These message-driven beans are activated by the container when a message is received and it is not directly accessible to the clients. The clients subscribe to a topic or to a queue and interact with these EJBs by sending messages to the queues or topics.

Let's enhance the project that we created using servlets to include a stateless bean that will have business logic. We will write a stateless bean that accepts a name and returns a greeting message:

@Stateless
open class StatelessEjb {
fun hello(name:String):String{
return"Hello $name"
}
}

Let's modify AuthController to make it use the stateless bean that we created:

@WebServlet(name = "home", value = "/home")
class HomeController: HttpServlet() {
private var statelessEjb: StatelessEjb = StatelessEjb()
override fun doPost(request: HttpServletRequest, response: HttpServletResponse) {
statelessEjb.hello(request.getParameter("loginId"))
response.writer.write("<html><body><h2>${statelessEjb.hello(request.getParameter("loginId"))}</h2> </body></html>")
}
}

Deploy the WAR file and enter the details:

The output we get is as follows:

Here, we used a stateless EJB to display a greeting message in the controller.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset