jakarta-ee - Real world use cases for stateful session EJB - TagMerge
4Real world use cases for stateful session EJBReal world use cases for stateful session EJB

Real world use cases for stateful session EJB

Asked 1 years ago
2
4 answers

Stateful EJB can persist their state in the database but Stateful EJB do not necessarily need to persist their state in a database. A Stateful EJB is obliged to store and remember in memory the conversational state with the client only for the duration of the "conversation".

Below you may find some real world examples identified in some cases that are suitable for Stateful EJB usage according to the Java EE 7 Tutorial: Stateful session beans are appropriate if any of the following conditions are true.

  • The bean's state represents the interaction between the bean and a specific client. Real World Example: Stateful EJB implementation of an online transaction with login, action, logout.

  • The bean needs to hold information about the client across method invocations. Real World Example: Stateful EJB could be used to implement a shopping card. Data can be persisted or not, for example for an eshop that does not require a login, the shopping card could be discarded if the user does not finally checkout the items he has collected.

  • The bean mediates between the client and the other components of the application, presenting a simplified view to the client. Stateful EJB could be used to implement a persistence abstraction.

  • Behind the scenes, the bean manages the work flow of several enterprise beans. Real wold example: Stateful EJB could be used to implement an online vacation booking transaction where the EJB models an agent that books a plane ticket, a car and a hotel from different providers using other EJBs and then returns the results to the client.

The above examples demonstrate the applicability of the concept in some examples. Actually using Stateful EJBs in a real environment nowadays would result in a purer design however it would not be optimal if performance and complexity are taken into consideration. See also Stateful EJBs in web application?.

Source: link

1

We currently have a couple of Stateful EJBs in one of our applications running on production. So I suppose, it can be considered as a real world example.

This EJBs are used to provide large amounts of data to the clients by dividing this data in chunks and sending this chunks on demand. All this works as follows:

  • client prepares a request, specifying filters he wants data to be looked up against;
  • he submits this request using Stateful EJB method;
  • request is processed on a server and result set is prepared;
  • as a response to his request client gets a descriptor to the server-side result set;
  • having this descriptor he now can fetch chunks of data using Stateful bean methods.

Was a Stateful bean a necessity in solving such a problem? Not at all.

Described functionality could be achieved with a Stateless bean. But in this case we have only two ways of implementing it. Either we are forced to prepare result sets for requests each time a client needs next chunk of data (as we don't have any state), or we use some static storage and take care of security and concurrent access by our own. /By security here I mean preventing situations in which one client can gain access to the result set of another using his descriptor/

The first way is simply slower and less efficient comparing to implementation on Stateful beans. The second way is more complicated and less stable under load due to synchronization.

With Stateful bean we just get what we need in an efficient manner and without extra effort.

Source: link

0

Remote Interface
import javax.ejb.Remote;
 
@Remote
public interface LibraryStatefulSessionBeanRemote {
   //add business method declarations
}
Stateful EJB
@Stateful
public class LibraryStatefulSessionBean implements LibraryStatefulSessionBeanRemote {
   //implement business method 
}
LibraryStatefulSessionBeanRemote.java
package com.tutorialspoint.stateful;
 
import java.util.List;
import javax.ejb.Remote;
 
@Remote
public interface LibraryStatefulSessionBeanRemote {
   void addBook(String bookName);
   List getBooks();
}
LibraryStatefulSessionBean.java
package com.tutorialspoint.stateful;
 
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateful;
 
@Stateful
public class LibraryStatefulSessionBean implements LibraryStatefulSessionBeanRemote {
    
   List<String> bookShelf;    
 
   public LibraryStatefulSessionBean() {
      bookShelf = new ArrayList<String>();
   }
 
   public void addBook(String bookName) {
      bookShelf.add(bookName);
   }    
 
   public List<String> getBooks() {
      return bookShelf;
   }
}
JBoss Application Server Log Output
...
16:30:01,401 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
   LibraryStatefulSessionBean/remote - EJB3.x Default Remote Business Interface
   LibraryStatefulSessionBean/remote-com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote - EJB3.x Remote Business Interface
16:30:02,723 INFO  [SessionSpecContainer] Starting jboss.j2ee:jar=EjbComponent.jar,name=LibraryStatefulSessionBean,service=EJB3
16:30:02,723 INFO  [EJBContainer] STARTED EJB: com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote ejbName: LibraryStatefulSessionBean
16:30:02,731 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
 
   LibraryStatefulSessionBean/remote - EJB3.x Default Remote Business Interface
   LibraryStatefulSessionBean/remote-com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote - EJB3.x Remote Business Interface
...

Source: link

0

To define a session bean, you first need to define the service interface containing all its business methods. The session bean interface is just plain old Java interface without any annotation. The client uses this interface to retrieve a stub object of the session bean from the EJB 3.0 container
package demo.ejb3;

public interface SessionDemoItf {

   public String doQuery (String param);

}
We’ve also added the @PostConstruct and @PreDestroy notations, which are not compulsory but it’s helpful to understand the lifecycle of the EJB.
package demo.ejb3;

import javax.annotation.PostConstruct;
import javax.ejb.*;

@Stateless
public class SessionDemoBean implements SessionDemoItf {

    public String doQuery(String param) {
        return "Query with param " + param + " executed.";
    }

    @PostConstruct
    public void initialize () {
        // Initialize here objects which will be used
        // by the session bean
        System.out.println("SessionDemoBean initialized.");
    } 


    @PreDestory
    public void destroyBean() {
        // Free here resources acquired by the session bean
        System.out.println("SessionDemoBean initialized.");
    } 
}
In order to compile a project which uses EJBs, it is sufficient to include the jakarta.jakartaee-api dependency in your pom.xml:
<dependencies>
        <dependency>
          <groupId>jakarta.platform</groupId>
          <artifactId>jakarta.jakartaee-api</artifactId>
          <version>${jakartaee.version}</version>
          <scope>provided</scope>
        </dependency>
</dependencies>
<properties>
        <jakartaee.version>8.0.0</jakartaee.version>
</properties>
<properties>
        <jakartaee.version>9.0.0</jakartaee.version>
</properties>

Source: link

Recent Questions on jakarta-ee

    Programming Languages