jakarta-eeapplication-serverstateful-session-beanejb-3.2

Understanding EJB Architecture and Implementation


I am a beginner in the Jakarta EE world. I am going through a Jakarta EE module at university and I am struggling to find information that properly answers my question. I feel like it is one of those things that is so simple, straight forward & expected to be understood that I am just not getting it.

My tutor has not been very helpful, telling me not to worry about anything else and just to accept that "its just encapsulated business logic" this is meaningless and useless, because if that is all it was why cant I just create my own class.

Now I have gone through this article https://docs.oracle.com/cd/E16439_01/doc.1013/e13981/undejbs002.htm and I get the basic overview.

I am aware of the following:

  1. EJB runs inside an EJB Container which runs inside an Application server,
  2. Two types of EJB: Session Bean, Message Driven Bean
  3. Three types of Session Bean: Stateless, Stateful, Singleton
  4. @Stateless does not maintain "conversational state" between session/method invocation? whereas Stateful beans do?
  5. The container has a pool of these EJB (Java classes) basically that are instantiated and randomly assigned to a client in the case of @stateless
  6. @Stateful beans are kept in a pool and the same client is assigned the same bean?

Please feel free to correct any of the above. Based on what I know so far the overall architecture still seems to confuse me,

I have the following 6 questions (all are linked) I am sorry if I have not organised them properly, I will try edit/improve the answer:

  1. How do non Java (generic clients) access EJB? Is this even possible?

As far as I am aware most clients are one of the following: a) Web Browser in laptop/tablet/mobile, b) Native Mobile App, c) Some IOT device or another kind of device (for example delivery driver saving something to a unique handset) etc...

Now! Say the clients mentioned above (mostly a & b) as they are the most common need to access the same "business logic", How would they go about this? I assume native mobile apps also use the HTTP/s protocol? Would this mean that it would only be through a Servlet? Which is sort of like an MVC Controller?

Based on this diagram from the official Java EE documentation it would appear as though Web Browsers cant access EJB direct:

enter image description here

  1. If the answer to (1) is no, that clients such as web/mobile apps cant directly access an EJB, is there any use for a @Stateful session bean? Say for example in a web app, HTTP protocol is stateless, state can be maintained with the help of session/cookie, Which would mean the state is maintained by the Web Container? Whats the point of having a stateful session bean?

I have read other questions on stack overflow which suggest its not a good idea to have @stateful session bean with Servlet.

I get the whole shopping cart example, but this does not explain how state is maintained, if I was accessing my shopping cart through a browser, the cookies would be responsible for state right? Does EJB have access to browser cookies?

  1. If an EJB can only be directly accessed by Java Clients:

Example of Java Clients as far as I know: Servlet, Another EJB running local or remote, Another Java Class?, Standalone Java app?

I assume these clients would access the EJB by RMI/JNDI/CDI/Dependency Injection (in case of servlet)? Would it be fair to say that an EJB is similar to a SOAP Web Service in the Java EE world?

  1. Based on all of the above is there much of a use case for EJB anymore? Especially as most apps are either through the web or mobile using HTTP/Rest end points, many use JWT's to manage state client side, and I cant see many examples of standalone Java apps except for maybe an Android app etc..

  2. Is it better to use MVC/Spring/Spring Boot which seems to have a more understandable architechture and create REST/SOAP Web Services

  3. Is RMI also using HTTP? Does EJB understand HTTP?

I think most of these questions surround the same point. I cant get a clear answer, I think It should be obvious but I just dont get it. Any advice would be greatly appreciated


Solution

  • Jakarta EE is a bundle of coexisting APIs, EJB is just one of them.

    We can say that an EJB is a plain old java object with aspects. You get for example: transaction managing, security, monitoring, pooling, etc.

    @Stateless does maintain state just during a method invocation. @Stateful does maintain state between method invocation during a session. @Singelton does maintain state during the whole life cycle of the running application.

    @Stateful beans are kept in a pool and the same client is assigned the same bean?

    No, each client creates a new instance of a bean, and eventually removes it. Instances may be passivated to disk if the cache fills up.

    "How do non Java (generic clients) access EJB? Is this even possible?"

    Java EE web services

    Via Rest integrating JAX-RS with EJB Technology

    @Stateless
    @Path("/employee/{id}")
    public class Employee {
        public Employee(@PathParam("id") String id) {...}
    }
    

    the HTTP/s protocol? Would this mean that it would only be through a Servlet? Which is sort of like an MVC Controller?

    JAX-RS och Spring MVC uses Servlet.

    For a web application as example you can use JSF or JSP integrating with CDI and EJBs.

    If the answer to (1) is no, that clients such as web/mobile apps cant directly access an EJB, is there any use for a @Stateful session bean? Say for example in a web app, HTTP protocol is stateless, state can be maintained with the help of session/cookie, Which would mean the state is maintained by the Web Container? Whats the point of having a stateful session bean?

    As an example: where will your shopping cart and products go between calls? We need to save the state somewhere, you can use a @SessionScoped bean or a @Stateful bean or the database, etc. Any ways it's your choice

    "I have read other questions on stack overflow which suggest its not a good idea to have @Stateful session bean with Servlet."

    I don´t know what they mean, but if you want to hold your state for example in a shopping cart @Stateful beans is the way to go. as an alternative you can use CDI beans with @SessionScoped instead of EJB @Stateful.

    I get the whole shopping cart example, but this does not explain how state is maintained, if I was accessing my shopping cart through a browser, the cookies would be responsible for state right? Does EJB have access to browser cookies?

    The Jakarta EE runtime is responsible for those details, but you can access the SecurityContext, SessionContext if is needed.

    on an EJB can you annotate with @Resource SessionContext ctx; to get the context of a Caller.

    Example of Java Clients as far as I know: Servlet, Another EJB running local or remote, Another Java Class?, Standalone Java app?

    I assume these clients would access the EJB by RMI/JNDI/CDI/Dependency Injection (in case of servlet)?

    There is more than the Servlet API, but as i said the most of Java web technologies build upon or integrates with Servlet technology.

    Would it be fair to say that an EJB is similar to a SOAP Web Service in the Java EE world?

    No, for SOAP Web Services we use JAX-WS and yes you can integrate JAX-WS with EJBs

    @Stateless
    @WebService
    public class HelloService {
    
        public String sayHello(String name) {
            return("Hello "+name +" from first EJB3.0 Web Service");
        }
    }
    

    Based on all of the above is there much of a use case for EJB anymore? Especially as most apps are either through the web or mobile using HTTP/Rest end points, many use JWT's to manage state client side, and I cant see many examples of standalone Java apps except for maybe an Android app etc..

    EJB is just a part or tool of Jakarta EE you have to use the right tool for the case.

    Is it better to use MVC/Spring/Spring Boot which seems to have a more understandable architecture and create REST/SOAP Web Services

    No, you are mixing apples and oranges. EJB is just an API and Spring is a complete platform.

    "While some consider Java EE and Spring to be in competition, Spring is, in fact, complementary to Java EE."

    The term "Spring" means different things in different contexts. It can be used to refer to the Spring Framework project itself, which is where it all started.

    Spring Boot is an extension of Spring Framework. Spring Boot embeds Apache Tomcat as a runtime. Apache Tomcat implements four Jakarta EE specifications — Jakarta Servlet, Jakarta Standard Tag Library, Jakarta WebSocket, and Jakarta Authentication. (is almost the same for Undertow, Jetty).

    Is RMI also using HTTP?

    RMI protocol is not relevant anymore and RMI is not required for using EJB.

    Does EJB understand HTTP?

    as i said you can integrate JAX-RS with EJB.

    I recommend some online workshops if you are interested

    Java EE Bootstrap

    Effective Java EE

    Java EE Testing

    Java EE microservices

    Apps With Microprofile