<div dir="ltr">Technically speaking, I can have a bean like this:<div><br></div><div>@ApplicationScoped</div><div>public class Foo {</div><div> public void onStart(@Observes @initialized(ApplicationScoped.class) Object obj) {</div><div> // do some work here</div><div> }</div><div>}</div><div><br></div><div>That bean will get instantiated by the container, even if it doesn't have any injection targets.</div><div><br></div><div>John</div><div><br><div class="gmail_quote"><div dir="ltr">On Mon, Aug 24, 2015 at 7:18 PM arjan tijms <<a href="mailto:arjan.tijms@gmail.com">arjan.tijms@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
On Mon, Aug 24, 2015 at 9:47 PM, Nigel Deakin <<a href="mailto:nigel.deakin@oracle.com" target="_blank" onclick="window.open('https://mail.google.com/mail/?view=cm&tf=1&to=nigel.deakin@oracle.com&cc=&bcc=&su=&body=','_blank');return false;">nigel.deakin@oracle.com</a>> wrote:<br>
> As I understand it, if you want to create a CDI managed bean (so that it is<br>
> managed by CDI) then you have to inject it into some code somewhere.<br>
<br>
This is not exclusively the case really;<br>
<br>
There are generally 3 options:<br>
<br>
1. Bean is injected<br>
2. Bean is named (using @Named annotation or getName() of a Bean<T><br>
returns non null) and referenced by EL<br>
3. Bean is programmatically created via BeanManager<br>
<br>
In saw that the proposal essentially has an example of 3. already;<br>
using the Instance injection. There are 2 variants on this where code<br>
either uses the BeanManager directly or uses the "shortcut"<br>
CDI.current().select(...);<br>
<br>
Option 3. is often used when something needs a bean of a specific type<br>
to do something. E.g. in JSF 2.3 a user would define a bean like this:<br>
<br>
@FacesDataModel(forClass = MyCollection.class)<br>
public class MyCollectionModel<E> extends DataModel<E> { ... }<br>
<br>
And it just sits there (the user doesn't have to create it).<br>
<br>
When a component needs this, it tries to obtain it programmatically,<br>
via something like:<br>
<br>
cdi.select(<br>
DataModel.class,<br>
new FacesDataModelAnnotationLiteral(MyCollection.class)<br>
).get();<br>
<br>
So the *expectation* may be that a user just defines a CDI based JMS<br>
listener bean, and that the container will then find those beans when<br>
it needs to deliver a message.<br>
<br>
Of course this is not entirely trivial in the JMS listener case. The<br>
JMS runtime can not easily ask CDI for all active instances of say the<br>
request scope, and then ask for beans to be created in all those<br>
scopes and then deliver the message to all of them.<br>
<br>
<br>
> If I understand things correctly, if this is a normal CDI bean then this<br>
> 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 MyListenerBean to trigger lazy initialisation.<br>
<br>
This is one way for sure, but one alternative worth mentioning is to<br>
eagerly instantiatie the scoped bean whenever its scope starts. For<br>
OmniFaces we have implemented a separate annotation for this for CDI<br>
1.0, see <a href="http://showcase.omnifaces.org/cdi/Eager" rel="noreferrer" target="_blank">http://showcase.omnifaces.org/cdi/Eager</a><br>
<br>
For CDI 1.1, there's the @Initialized and @Destroyed annotations that<br>
can be used to observe any scope where the implementing Context throws<br>
events for these (which are all Java EE provided scopes as far as I<br>
know). See <a href="https://rmannibucau.wordpress.com/2015/03/10/cdi-and-startup" rel="noreferrer" target="_blank">https://rmannibucau.wordpress.com/2015/03/10/cdi-and-startup</a><br>
and <a href="https://issues.jboss.org/browse/CDI-86" rel="noreferrer" target="_blank">https://issues.jboss.org/browse/CDI-86</a><br>
<br>
One CDI technicality is that if the listener method is called via the<br>
proxy, I think it will resolve to the current scope that thread is in.<br>
So this may mean the JMS endpoint that calls the JMS listener bean can<br>
only call the actual bean's method, not the proxied one. I'm sure the<br>
CDI experts here will have more knowledge about this.<br>
<br>
Hope this helps.<br>
<br>
Kind regards,<br>
Arjan Tijms<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
><br>
> I'm trying to avoid getting ahead of myself here. My current proposals<br>
> simply apply to normal CDI managed beans created in the normal way (via<br>
> injection and lazy initialisation). But I could certainly see a benefit in<br>
> extending this to allow JMS listener beans to be created in other ways. In<br>
> particular:<br>
><br>
> (a) Creating the bean instance as soon as the bean into which it is injected<br>
> enters a new scope rather than waiting for the application to call a method<br>
> on it (so-called eager initialisation). That sounds relatively<br>
> straightforward to me; this could either be a new standard CDI feature<br>
> available to all normal-scoped beans, or something specific to beans<br>
> annotated with @JMSListener.<br>
><br>
> (b) Creating the bean instance without the need to inject it anywhere. As<br>
> you suggest, in the case of ApplicationScoped beans this would make JMS<br>
> listener beans more like MDBs. But it might be equally useful to support<br>
> this for other normal scopes. For example, could we annotate a<br>
> @RequestScoped bean so that every time a new request scope started, an<br>
> instance of the bean for that scope was automatically created?<br>
><br>
> Or if we manage to get the @Startup annotation to Commons Annotation :<br>
><br>
> @Startup<br>
> public class MyListenerBean{<br>
><br>
><br>
> @JMSListener(lookup="java:global/java:global/Trades",type=JMSListener.Type.TOPIC<br>
> )<br>
> public void deliver(Message message) {<br>
> ...<br>
> }<br>
> }<br>
><br>
><br>
> That looks as if it's tied to ApplicationScoped. I'd rather look for<br>
> something more general.<br>
><br>
> Thanks for your comments so far. You're raising issues I have been thinking<br>
> about for some time.<br>
><br>
> Nigel<br>
><br>
><br>
><br>
><br>
> Just thinking here<br>
><br>
> Antonio<br>
><br>
> On Mon, Aug 24, 2015 at 6:30 PM, Nigel Deakin <<a href="mailto:nigel.deakin@oracle.com" target="_blank" onclick="window.open('https://mail.google.com/mail/?view=cm&tf=1&to=nigel.deakin@oracle.com&cc=&bcc=&su=&body=','_blank');return false;">nigel.deakin@oracle.com</a>><br>
> wrote:<br>
>><br>
>> Over in the JMS 2.1 expert group I've just published some proposals to<br>
>> allow any CDI managed bean in a Java EE<br>
>> application to listen for JMS messages. This would be implemented as a<br>
>> 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<br>
>> interested in helping, please take a look at<br>
>> <a 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<br>
>> <a href="mailto:users@jms-spec.java.net" target="_blank" onclick="window.open('https://mail.google.com/mail/?view=cm&tf=1&to=users@jms-spec.java.net&cc=&bcc=&su=&body=','_blank');return false;">users@jms-spec.java.net</a> alias.<br>
>><br>
>> Thanks,<br>
>><br>
>> Nigel<br>
>> JMS 2.1 specification lead<br>
>> _______________________________________________<br>
>> cdi-dev mailing list<br>
>> <a href="mailto:cdi-dev@lists.jboss.org" target="_blank" onclick="window.open('https://mail.google.com/mail/?view=cm&tf=1&to=cdi-dev@lists.jboss.org&cc=&bcc=&su=&body=','_blank');return false;">cdi-dev@lists.jboss.org</a><br>
>> <a 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<br>
>> code under the Apache License, Version 2<br>
>> (<a href="http://www.apache.org/licenses/LICENSE-2.0.html" rel="noreferrer" target="_blank">http://www.apache.org/licenses/LICENSE-2.0.html</a>). For all other ideas<br>
>> provided on this list, the provider waives all patent and other intellectual<br>
>> property rights inherent in such information.<br>
><br>
><br>
><br>
><br>
> --<br>
> Antonio Goncalves<br>
> Software architect, Java Champion and Pluralsight author<br>
><br>
> Web site | Twitter | LinkedIn | Pluralsight | Paris JUG | Devoxx France<br>
><br>
><br>
><br>
> _______________________________________________<br>
> cdi-dev mailing list<br>
> <a href="mailto:cdi-dev@lists.jboss.org" target="_blank" onclick="window.open('https://mail.google.com/mail/?view=cm&tf=1&to=cdi-dev@lists.jboss.org&cc=&bcc=&su=&body=','_blank');return false;">cdi-dev@lists.jboss.org</a><br>
> <a 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<br>
> under the Apache License, Version 2<br>
> (<a href="http://www.apache.org/licenses/LICENSE-2.0.html" rel="noreferrer" target="_blank">http://www.apache.org/licenses/LICENSE-2.0.html</a>). For all other ideas<br>
> provided on this list, the provider waives all patent and other intellectual<br>
> property rights inherent in such information.<br>
_______________________________________________<br>
cdi-dev mailing list<br>
<a href="mailto:cdi-dev@lists.jboss.org" target="_blank" onclick="window.open('https://mail.google.com/mail/?view=cm&tf=1&to=cdi-dev@lists.jboss.org&cc=&bcc=&su=&body=','_blank');return false;">cdi-dev@lists.jboss.org</a><br>
<a 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 href="http://www.apache.org/licenses/LICENSE-2.0.html" rel="noreferrer" target="_blank">http://www.apache.org/licenses/LICENSE-2.0.html</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></div></div>