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:
- EJB runs inside an EJB Container which runs inside an Application server,
- Two types of EJB: Session Bean, Message Driven Bean
- Three types of Session Bean: Stateless, Stateful, Singleton
- @Stateless does not maintain "conversational state" between session/method invocation? whereas Stateful beans do?
- 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
- @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:
- 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:
- 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?
- 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?
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..
Is it better to use MVC/Spring/Spring Boot which seems to have a more understandable architechture and create REST/SOAP Web Services
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