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

Nigel Deakin nigel.deakin at oracle.com
Tue Aug 25 13:07:12 EDT 2015


On 25/08/2015 12:38, arjan tijms wrote:
> On Tue, Aug 25, 2015 at 1:19 PM, John D. Ament <john.d.ament at gmail.com> wrote:
>> Technically speaking, I can have a bean like this:
>>
>> @ApplicationScoped
>> public class Foo {
>>      public void onStart(@Observes @initialized(ApplicationScoped.class)
>> Object obj) {
>>         // do some work here
>>      }
>> }
>>
>> That bean will get instantiated by the container, even if it doesn't have
>> any injection targets.
>
> Indeed, that's what I meant above with the reference to the
> @Initialized and @Destroyed annotations. With that construct a JMS
> Listener bean can be "started" without it having to be referenced from
> other code.

Arjan, John,

This looks very interesting. Does this work with any normal scope?

I was thinking of a hypothetical feature in which a application defines a @RequestScoped bean...

@RequestScoped
public class MyCDIBean21 {

    @JMSListener(lookup="java:global/java:global/Trades",type=JMSListener.Type.TOPIC )
    @JMSConnectionFactory("java:global/MyCF")
    @MessageSelector("ticker='ORCL'")
    public void processNewsItem(String newsItem) {
      ...
    }
  }

...but we want to avoid the need for the application to inject and lazily create an instance of the bean in each request.

Are you suggesting that if the user added:

    public void onStart(@Observes @initialized(RequestScoped.class) Object obj) {
       // do nothing here
    }

then every time a new request starts, this event is fired which causes an instance of the bean to be created for that 
request?

Nigel



More information about the cdi-dev mailing list