[cdi-dev] JMS 2.1: Proposal to allow any CDI managed bean in a Java EE application to listen for JMS messages

arjan tijms arjan.tijms at gmail.com
Wed Aug 26 08:26:16 EDT 2015


On Wed, Aug 26, 2015 at 2:03 PM, Nigel Deakin <nigel.deakin at oracle.com> wrote:
> In the case of a "JMS listener bean" with @RequestScoped (or any scope,
> really) then the JMS consumer (the thread which receives a message from the
> JMS server and invokes the listener) is associated with the actual bean
> instance.

Indeed, so the call takes place in the context of the JMS consumer
thread and happens to the "bare" bean instance and does not go through
the proxy that's normally used to access that bean.

> However once you're inside the callback method then this request scope
> (which relates to a different thread) is not available.

So this would be an important detail for the user to take into
account. The callback method is NOT allowed to access any other normal
scoped beans, be it via injection, beanmanager lookup or whatever.

E.g. given:

public class MyCDIBean21 {

  private MyRequestScopedBean requestBean;

   public void processNewsItem(String newsItem) {

Then the callback processNewsItem() can not use requestBean, since
it's a proxy and it will try to delegate to a scope that does not
exist for that thread.

Alternative, if there's an other @RequestScope active (as suggested
below) it will end up calling a different bean instance than the user
may expect.

While it could work, I wonder if this aspect wouldn't be a bit confusing?

More information about the cdi-dev mailing list