Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does adding the @Stateful or @Stateless annotations actually do?

Tags:

jakarta-ee

ejb

I'm just getting to grips with Java EE. I know that adding @Stateful or @Stateless annotations to a class will make it an EJB bean. But what is actually happening in the background once I do that? I see the following listed on Wikipedia in relation to EJBs.

  • Transaction processing
  • Integration with the persistence services offered by the Java Persistence API (JPA)
  • Concurrency control
  • Eventing using Java Message Service and Java EE Connector Architecture
  • Asynchronous method invocation

 

  1. When I mark a class as an EJB do items listed above get 'taken care of' in the background? An entirely different code path is followed that goes through each of the above once I make a class an EJB, is that what's happening?
  2. I see that using CDI I have the option of injecting EJB beans as oppposed to CDI beans. In that case should I always be using EJB beans instead of CDI beans as EJB beans are more powerful than CDI beans?
like image 384
sonicboom Avatar asked Dec 17 '12 10:12

sonicboom


People also ask

What is @stateless annotation in Java?

Use @Stateless annotation to signify it a stateless bean. EJB Container automatically creates the relevant configurations or interfaces required by reading this annotation during deployment.

How does EJB annotation work?

In EJB 3.0, annotations are used to describe configuration meta-data in EJB classes. By this way, EJB 3.0 eliminates the need to describe configuration data in configuration XML files. EJB container uses compiler tool to generate required artifacts like interfaces, deployment descriptors by reading those annotations.

What is stateful and stateless in Java?

An instance of a stateful session bean has a unique identity that is assigned by the container at create time. Stateless: A stateless session bean does not maintain conversational state. Instances of a stateless session bean have no conversational state.


1 Answers

See this answer for some insight on both questions.

The highlights to focus on in that answer are that:

  • EJBs and CDI beans are proxied components, the object you get is a fake, the real object is hidden and this is how services are added: caller->proxy->services->realObject
  • CDI and EJB are effectively the same as such, mix them freely. Which you use depends on what you're attempting to do. I tend to use CDI unless I need one of the items listed in that answer. Then I just upgrade or add a new bean.

Note, one thing I did miss in that answer was the entire @MessageDriven concept.

MessageDriven Beans

It's very interesting you put JMS / Connector on the same line as that is exactly how they are implemented. Message-Driven Beans (MDBs) should actually be called "Connector-Driven Beans" as all communication and lifecycle of an MDB is actually tied to the Connector Architecture specification and has nothing to do with JMS directly -- JMS is just the only Connector people ever see. There's a great deal of potential there. Hopefully we'll see some improvements in Java EE 7.

like image 171
David Blevins Avatar answered Sep 17 '22 20:09

David Blevins