Search code examples
ejbmessage-driven-beanjcastateful

Can a messagelistener or mdb be stateful?


related to my question on jca inbound transaction management. How to control XAResource in inbound jca resource adapter for conversational transaction?

For an inbound resource adapter I need to keep a transaction open and execute different actions in that transaction.

This could be solved by making the message endpoint @Stateful, but It's unclear to me wether that is allowed.

The 3.1 EJB spec states (section 5.1)

Message-driven bean instances have no conversational state. This means that all bean instances are equivalent when they are not involved in servicing a client message.

I found a post from David Blevins, founder of TomiTribe (the company behin TomEE), stating that it would be allowed. https://github.com/dblevins/jca-quickstart/wiki/Inbound-Connectors-MDBs-Today

Another approach would be trying to manage the XAResrouce myself, but no luck there yet...

edited: Note that the JCA spec states (section 13.5)

Note that the endpoint instance supplied by the createEndPoint method call is a proxy which implements the endpoint message listener type and the MessageEndpoint interface and it is not the actual endpoint.

So, afaik there is no guarantee that the actual listener is the same everytime you go through the proxy.


Solution

  • Let me answer my own question.

    There is nothing in the specification that would allow this. The sections referred to in the specification state that mdbs are not stateful.

    However, I was in contact with David Blevins, who answered: "Short answer is they are stateful for Wildfly and TomEE and for GlassFish, I can't quite remember."

    So it might work on your platform, but don't expect the behaviour to be portable across servers or even different versions of the same application server.