You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Michael Südkamp <Mi...@docware.de> on 2015/09/01 17:10:42 UTC

Using JMS with Resource annotation

Hello,

We are using JMS with Tomcat/ActiveMQ as well with IBM Websphere/IBM MQ.
We have resource-ref entries in our web.xml which are referencing the JMS resources configured in the container.

Now we would like to get rid of the web.xml resource-ref entries. In my understanding they can be replaced by @Resource annotations as described here: http://tomee.apache.org/tomcat-jms.html

But whether I use the @Resource annotation for the ConnectionFactory without name attribute as in your example or whether I specify the name configured in the container, I always get a NullPointerException when using the factory instance, i.e. the factory doesn't get injected.

What am I missing here?

mit freundlichen Grüßen
with kind regards

Michael Südkamp
Docware GmbH


Re: Using JMS with Resource annotation

Posted by Andy Gumbrecht <ag...@tomitribe.com>.
The area you marked as code, is that code in, for example @Singleton?

Andy.

http://www.tomitribe.com - @AndyGeeDe - On a small screen device.
On 2 Sep 2015 13:31, "Romain Manni-Bucau" <rm...@gmail.com> wrote:

> Why not using tomee.xml?
>
> Ps: we still dont know if you class getting the injection is managed (is it
> a cdi bean or an ejb?)
> Le 2 sept. 2015 13:03, "msdocware" <ms...@docware.de> a écrit :
>
> > This is the current situation
> > -----------------------------
> >
> > Code
> >
> >     InitialContext ctx = new javax.naming.InitialContext();
> >     factory = (ConnectionFactory) ctx.lookup("java:comp/env/jms/MyCF");
> >
> >
> > web.xml
> >
> >     <resource-ref>
> >         <description>Resource reference to JMS Factory</description>
> >         <res-ref-name>jms/MyCF</res-ref-name>
> >         <res-type>javax.jms.ConnectionFactory</res-type>
> >         <res-auth>Container</res-auth>
> >         <res-sharing-scope>Shareable</res-sharing-scope>
> >     </resource-ref>
> >
> >
> > Tomcat context.xml
> >
> > <Resource name="jms/MyCF" auth="Container"
> > type="org.apache.activemq.ActiveMQConnectionFactory" description="JMS
> > Connection Factory"
> >         factory="org.apache.activemq.jndi.JNDIReferenceFactory"
> > brokerURL="tcp://localhost:61616" brokerName="RemoteActiveMQBroker"/>
> >
> >
> > This is what I am trying
> > ------------------------
> >
> > Code
> >
> >     @Resource(name = "jms/MyCF")
> >     private ConnectionFactory factory;
> >
> > web.xml: no JMS related entries
> >
> > Tomcat context.xml: see above
> >
> >
> >
> >
> >
> > --
> > View this message in context:
> >
> http://tomee-openejb.979440.n4.nabble.com/Using-JMS-with-Resource-annotation-tp4676042p4676056.html
> > Sent from the TomEE Users mailing list archive at Nabble.com.
> >
>

Re: Using JMS with Resource annotation

Posted by msdocware <ms...@docware.de>.
After reading this https://docs.oracle.com/javaee/6/tutorial/doc/bnaeo.html,
I now have a better understanding what is possible in web components. 
It's stated there that resource injections by annotations should work in
Servlets. For simplicity I had done my tests in a JSP. And that was the
problem. Once I put my code into a servlet, Websphere during deployment
recognized the resource requirements and offered the mapping to the
available resources. Testing worked.

Now I am much further.



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Using-JMS-with-Resource-annotation-tp4676042p4676115.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: Using JMS with Resource annotation

Posted by Romain Manni-Bucau <rm...@gmail.com>.
2015-09-03 8:53 GMT+02:00 msdocware <ms...@docware.de>:

> >> Now it works in Tomcat. I still need to test Websphere deployment
>
> What I did doesn't work in Websphere. When I had the resource-ref entries
> in
> web.xml, Websphere during deployment recognized them and offered to map
> them
> to the available resources. My understanding was that this would work also
> with annotations, probably by some byte code analyse. But this didn't
> happen
> and thus I got errors when accessing the resources.
>
> > If it matches a tomee.xml resource id
>
> What I need is a way compatible with JEE, so that it works in different JEE
> servers. Something that works in Tomcat or TomEE is not enough for me.
>
>
There is no way to keep the definition portable in EE 6 (JMS 1.x) but the
code should stay the same. Dont mix definition of the resource and usage
(injection).


> In a previous reply there was the question whether my resource is managed
> or
> not. Actually I don't know. Above I described what I did in Tomcat and
> Websphere. Maybe "managed" is a thing what is available in true EAR apps.
> We
> have only WAR apps.
>
>
if your code is not managed then you'll get NO service from the container
so this is really the question you need to answer.

Let suppose your bean with injection is MyService, is this class a CDI bean
or an EJB or do you do a "new MyService()" or use spring to get an
instance. If not one of the first 2 answers then injections are not
intended to work (a bit like if you ask me to be at a meeting in 2h without
saying it to me ;)).


> More ideas?
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/Using-JMS-with-Resource-annotation-tp4676042p4676073.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: Using JMS with Resource annotation

Posted by msdocware <ms...@docware.de>.
>> Now it works in Tomcat. I still need to test Websphere deployment

What I did doesn't work in Websphere. When I had the resource-ref entries in
web.xml, Websphere during deployment recognized them and offered to map them
to the available resources. My understanding was that this would work also
with annotations, probably by some byte code analyse. But this didn't happen
and thus I got errors when accessing the resources.

> If it matches a tomee.xml resource id

What I need is a way compatible with JEE, so that it works in different JEE
servers. Something that works in Tomcat or TomEE is not enough for me.

In a previous reply there was the question whether my resource is managed or
not. Actually I don't know. Above I described what I did in Tomcat and
Websphere. Maybe "managed" is a thing what is available in true EAR apps. We
have only WAR apps.

More ideas?



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Using-JMS-with-Resource-annotation-tp4676042p4676073.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: Using JMS with Resource annotation

Posted by Romain Manni-Bucau <rm...@gmail.com>.
2015-09-02 15:30 GMT+02:00 msdocware <ms...@docware.de>:

> I think I found my mistake. The Resource annotation is just a replacement
> for
> the web.xml reource-ref entry.
> I still need to obtain the instance from the initial context.
>
>
not really, at least not if set on a field. If it matches a tomee.xml
resource id then it will get injected in all managed beans without more
config.


> Now it works in Tomcat. I still need to test Websphere deployment, whether
> during deployment the resource mapping will be obtained from the annotation
> instead from the web.xml.
>
> Michael
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/Using-JMS-with-Resource-annotation-tp4676042p4676060.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: Using JMS with Resource annotation

Posted by msdocware <ms...@docware.de>.
I think I found my mistake. The Resource annotation is just a replacement for
the web.xml reource-ref entry.
I still need to obtain the instance from the initial context.

Now it works in Tomcat. I still need to test Websphere deployment, whether
during deployment the resource mapping will be obtained from the annotation
instead from the web.xml.

Michael



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Using-JMS-with-Resource-annotation-tp4676042p4676060.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: Using JMS with Resource annotation

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Why not using tomee.xml?

Ps: we still dont know if you class getting the injection is managed (is it
a cdi bean or an ejb?)
Le 2 sept. 2015 13:03, "msdocware" <ms...@docware.de> a écrit :

> This is the current situation
> -----------------------------
>
> Code
>
>     InitialContext ctx = new javax.naming.InitialContext();
>     factory = (ConnectionFactory) ctx.lookup("java:comp/env/jms/MyCF");
>
>
> web.xml
>
>     <resource-ref>
>         <description>Resource reference to JMS Factory</description>
>         <res-ref-name>jms/MyCF</res-ref-name>
>         <res-type>javax.jms.ConnectionFactory</res-type>
>         <res-auth>Container</res-auth>
>         <res-sharing-scope>Shareable</res-sharing-scope>
>     </resource-ref>
>
>
> Tomcat context.xml
>
> <Resource name="jms/MyCF" auth="Container"
> type="org.apache.activemq.ActiveMQConnectionFactory" description="JMS
> Connection Factory"
>         factory="org.apache.activemq.jndi.JNDIReferenceFactory"
> brokerURL="tcp://localhost:61616" brokerName="RemoteActiveMQBroker"/>
>
>
> This is what I am trying
> ------------------------
>
> Code
>
>     @Resource(name = "jms/MyCF")
>     private ConnectionFactory factory;
>
> web.xml: no JMS related entries
>
> Tomcat context.xml: see above
>
>
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/Using-JMS-with-Resource-annotation-tp4676042p4676056.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: Using JMS with Resource annotation

Posted by msdocware <ms...@docware.de>.
This is the current situation
-----------------------------

Code

    InitialContext ctx = new javax.naming.InitialContext();
    factory = (ConnectionFactory) ctx.lookup("java:comp/env/jms/MyCF");


web.xml

    <resource-ref>
        <description>Resource reference to JMS Factory</description>
        <res-ref-name>jms/MyCF</res-ref-name>
        <res-type>javax.jms.ConnectionFactory</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>


Tomcat context.xml

<Resource name="jms/MyCF" auth="Container"
type="org.apache.activemq.ActiveMQConnectionFactory" description="JMS
Connection Factory"
        factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="tcp://localhost:61616" brokerName="RemoteActiveMQBroker"/>


This is what I am trying
------------------------

Code

    @Resource(name = "jms/MyCF")
    private ConnectionFactory factory;

web.xml: no JMS related entries

Tomcat context.xml: see above





--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Using-JMS-with-Resource-annotation-tp4676042p4676056.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: Using JMS with Resource annotation

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi

is the bean with the @Resource managed - or do you instantiate it yourself?


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com>

2015-09-01 17:10 GMT+02:00 Michael Südkamp <Mi...@docware.de>:

> Hello,
>
> We are using JMS with Tomcat/ActiveMQ as well with IBM Websphere/IBM MQ.
> We have resource-ref entries in our web.xml which are referencing the JMS
> resources configured in the container.
>
> Now we would like to get rid of the web.xml resource-ref entries. In my
> understanding they can be replaced by @Resource annotations as described
> here: http://tomee.apache.org/tomcat-jms.html
>
> But whether I use the @Resource annotation for the ConnectionFactory
> without name attribute as in your example or whether I specify the name
> configured in the container, I always get a NullPointerException when using
> the factory instance, i.e. the factory doesn't get injected.
>
> What am I missing here?
>
> mit freundlichen Grüßen
> with kind regards
>
> Michael Südkamp
> Docware GmbH
>
>