<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    On 24/08/2015 18:42, Antonio Goncalves wrote:<br>
    <blockquote
cite="mid:CA+ZZq9_Brvtks=jxQ1TbtXMNm+ab3w3GZfG-2sGQYeMK2mu2jA@mail.gmail.com"
      type="cite">
      <div dir="ltr">Nigel, what do you mean by : 
        <div><br>
        </div>
        <div>"The application must obtain an instance of the JMS
          listener bean using dependency injection"</div>
        <div><br>
        </div>
        <div>In your examples, it looks like a CDI bean cannot start
          listening if not injected : </div>
        <div><br>
        </div>
        <div>
          <div><font face="monospace, monospace">@WebServlet("/myjmsservlet1")</font></div>
          <div><font face="monospace, monospace"> public class
              MyJMSServlet1 extends HttpServlet {</font></div>
          <div><font face="monospace, monospace"> </font></div>
          <div><font face="monospace, monospace">   <b>@Inject
                MyDepScopeListenerBean</b> myDepScopeListenerBean;</font></div>
          <div><font face="monospace, monospace">    </font></div>
          <div><font face="monospace, monospace">   public void
              service(ServletRequest req, ServletResponse res) throws
              IOException, ServletException {</font></div>
          <div><font face="monospace, monospace">      ...</font></div>
          <div><font face="monospace, monospace">   }</font></div>
          <div><font face="monospace, monospace"> }</font></div>
        </div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div>Did I understand well ? Is this the case ? Just like a MDB,
          I would like a CDI bean to start listening to a message
          without being injected, as soon as the application starts. </div>
      </div>
    </blockquote>
    <br>
    My starting point is that these are normal CDI beans which are
    created just like any other CDI bean. The bean's postCreate
    behaviour (inserted by the extension) would connect to the JMS
    provider and start listening for messages. The bean's preDestroy
    behaviour would stop listening for messages and disconnect from the
    JMS provider. <br>
    <br>
    As I understand it, if you want to create a CDI managed bean (so
    that it is managed by CDI) then you have to inject it into some code
    somewhere.  <br>
    <br>
    Additionally, if the bean is normal scoped, you have to trigger lazy
    instantiation by calling a method on the injected bean proxy (e.g.
    toString()). <br>
    <br>
    <blockquote
cite="mid:CA+ZZq9_Brvtks=jxQ1TbtXMNm+ab3w3GZfG-2sGQYeMK2mu2jA@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div>Something like :  </div>
        <div><br>
        </div>
        <div>
          <div><font face="monospace, monospace"><b>@ApplicationScoped</b></font></div>
          <div><font face="monospace, monospace">public class
              MyListenerBean{</font></div>
          <div><font face="monospace, monospace"> </font></div>
          <div><font face="monospace, monospace"> 
               @JMSListener(lookup="java:global/java:global/Trades",type=JMSListener.Type.TOPIC
              )</font></div>
          <div><font face="monospace, monospace">   public void
              deliver(Message message) {</font></div>
          <div><font face="monospace, monospace">     ...</font></div>
          <div><font face="monospace, monospace">   }</font></div>
          <div><font face="monospace, monospace">}</font></div>
        </div>
        <div><br>
        </div>
      </div>
    </blockquote>
    <br>
    If I understand things correctly, if this is a normal CDI bean then
    this alone is not sufficient to cause an instance of the bean to be
    created. <br>
    <br>
    You also need to <br>
    (1) inject it into some other bean <br>
    (2) create that other bean and<br>
    (3) call a method on the <font face="monospace, monospace">MyListenerBean
    </font>to trigger lazy initialisation.<br>
    <br>
    I'm trying to avoid getting ahead of myself here. My current
    proposals simply apply to normal CDI managed beans created in the
    normal way (via injection and lazy initialisation). But I could
    certainly see a benefit in extending this to allow JMS listener
    beans to be created in other ways. In particular:<br>
    <br>
    (a) Creating the bean instance as soon as the bean into which it is
    injected enters a new scope rather than waiting for the application
    to call a method on it (so-called eager initialisation). That sounds
    relatively straightforward to me; this could either be a new
    standard CDI feature available to all normal-scoped beans, or
    something specific to beans annotated with @JMSListener.<br>
    <br>
    (b) Creating the bean instance without the need to inject it
    anywhere.  As you suggest, in the case of ApplicationScoped beans
    this would make JMS listener beans more like MDBs. But it might be
    equally useful to support this for other normal scopes. For example,
    could we annotate a @RequestScoped bean so that every time a new
    request scope started, an instance of the bean for that scope was
    automatically created? <br>
    <br>
    <blockquote
cite="mid:CA+ZZq9_Brvtks=jxQ1TbtXMNm+ab3w3GZfG-2sGQYeMK2mu2jA@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div>Or if we manage to get the <font face="monospace,
            monospace">@Startup</font> annotation to Commons Annotation
          : </div>
        <div><br>
        </div>
        <div>
          <div>
            <div><font face="monospace, monospace"><b>@Startup</b></font></div>
            <div><font face="monospace, monospace">public class
                MyListenerBean{</font></div>
            <div><font face="monospace, monospace"> </font></div>
            <div><font face="monospace, monospace"> 
                 @JMSListener(lookup="java:global/java:global/Trades",type=JMSListener.Type.TOPIC
                )</font></div>
            <div><font face="monospace, monospace">   public void
                deliver(Message message) {</font></div>
            <div><font face="monospace, monospace">     ...</font></div>
            <div><font face="monospace, monospace">   }</font></div>
            <div><font face="monospace, monospace">}</font></div>
          </div>
        </div>
        <div><font face="monospace, monospace"><br>
          </font></div>
      </div>
    </blockquote>
    <br>
    That looks as if it's tied to ApplicationScoped. I'd rather look for
    something more general.<br>
    <br>
    Thanks for your comments so far. You're raising issues I have been
    thinking about for some time.<br>
    <br>
    Nigel<br>
    <br>
     <br>
    <br>
    <blockquote
cite="mid:CA+ZZq9_Brvtks=jxQ1TbtXMNm+ab3w3GZfG-2sGQYeMK2mu2jA@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div><font face="monospace, monospace"><br>
          </font></div>
        <div><font face="arial, helvetica, sans-serif">Just thinking
            here</font></div>
        <div><font face="arial, helvetica, sans-serif"><br>
          </font></div>
        <div><font face="arial, helvetica, sans-serif">Antonio</font></div>
      </div>
      <div class="gmail_extra"><br>
        <div class="gmail_quote">On Mon, Aug 24, 2015 at 6:30 PM, Nigel
          Deakin <span dir="ltr">&lt;<a moz-do-not-send="true"
              href="mailto:nigel.deakin@oracle.com" target="_blank">nigel.deakin@oracle.com</a>&gt;</span>
          wrote:<br>
          <blockquote class="gmail_quote" style="margin:0 0 0
            .8ex;border-left:1px #ccc solid;padding-left:1ex">Over in
            the JMS 2.1 expert group I've just published some proposals
            to allow any CDI managed bean in a Java EE<br>
            application to listen for JMS messages. This would be
            implemented as a standard CDI portable extension and would
            become<br>
            a mandatory part of a full Java EE 8 application server.<br>
            <br>
            I would welcome any comments from the CDI spec experts here.
            If you're interested in helping, please take a look at<br>
            <a moz-do-not-send="true"
              href="https://java.net/projects/jms-spec/pages/CDIBeansAsJMSListeners"
              rel="noreferrer" target="_blank">https://java.net/projects/jms-spec/pages/CDIBeansAsJMSListeners</a><br>
            and send comments or questions to me or to the public <a
              moz-do-not-send="true"
              href="mailto:users@jms-spec.java.net"><a class="moz-txt-link-abbreviated" href="mailto:users@jms-spec.java.net">users@jms-spec.java.net</a></a>
            alias.<br>
            <br>
            Thanks,<br>
            <br>
            Nigel<br>
            JMS 2.1 specification lead<br>
            _______________________________________________<br>
            cdi-dev mailing list<br>
            <a moz-do-not-send="true"
              href="mailto:cdi-dev@lists.jboss.org">cdi-dev@lists.jboss.org</a><br>
            <a moz-do-not-send="true"
              href="https://lists.jboss.org/mailman/listinfo/cdi-dev"
              rel="noreferrer" target="_blank">https://lists.jboss.org/mailman/listinfo/cdi-dev</a><br>
            <br>
            Note that for all code provided on this list, the provider
            licenses the code under the Apache License, Version 2 (<a
              moz-do-not-send="true"
              href="http://www.apache.org/licenses/LICENSE-2.0.html"
              rel="noreferrer" target="_blank"><a class="moz-txt-link-freetext" href="http://www.apache.org/licenses/LICENSE-2.0.html">http://www.apache.org/licenses/LICENSE-2.0.html</a></a>).
            For all other ideas provided on this list, the provider
            waives all patent and other intellectual property rights
            inherent in such information.<br>
          </blockquote>
        </div>
        <br>
        <br clear="all">
        <div><br>
        </div>
        -- <br>
        <div class="gmail_signature">
          <div dir="ltr">Antonio Goncalves <br>
            Software architect, Java Champion and Pluralsight author<br>
            <br>
            <a moz-do-not-send="true"
              href="http://www.antoniogoncalves.org" target="_blank">Web
              site</a> | <a moz-do-not-send="true"
              href="http://twitter.com/agoncal" target="_blank">Twitter</a>
            | <a moz-do-not-send="true"
              href="http://www.linkedin.com/in/agoncal" target="_blank">LinkedIn</a> |
            <a moz-do-not-send="true"
              href="http://pluralsight.com/training/Authors/Details/antonio-goncalves"
              target="_blank">Pluralsight</a> | <a
              moz-do-not-send="true" href="http://www.parisjug.org"
              target="_blank">Paris JUG</a> | <a moz-do-not-send="true"
              href="http://www.devoxx.fr" target="_blank">Devoxx France</a></div>
        </div>
      </div>
    </blockquote>
    <br>
  </body>
</html>