You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by hkgot <ha...@gmail.com> on 2015/09/08 03:37:45 UTC

Re: How to reference connection factory and queues from code

Hi Romain,

Finally, I got a chance to try this. The below code is working:

System.setProperty("aConnectionFactory",
"connectionfactory:org.apache.activemq.ActiveMQConnectionFactory:tcp://localhost:11616");
System.setProperty("aQueue",
"queue:org.apache.activemq.command.ActiveMQQueue:LISTENER");
final ConnectionFactory connectionFactory = (ConnectionFactory)
context.lookup("java:aConnectionFactory");

However, I am not sure whether this is creating a new connection factory or
referencing the connection factory from tomee.xml. Because what I configured
in tomee.xml is MyJmsConnectionFactory, not connectionFactory. 

<Resource id="MyJmsConnectionFactory" type="javax.jms.ConnectionFactory">
        ResourceAdapter = MyJmsResourceAdapter
    </Resource>

In the sample you provided, aConnectionFactory is referencing
connectionfactory:org.apache.activemq.ActiveMQConnectionFactory:tcp://localhost:11616,
not MyJmsConnectionFactory. How do I reference to MyJmsConnectionFactory?

Thx.









--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676152.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: How to reference connection factory and queues from code

Posted by Romain Manni-Bucau <rm...@gmail.com>.
ServerUrl and BrokerXmlConfig are conflicting in your resource


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-09 20:31 GMT-07:00 hkgot <ha...@gmail.com>:

> I think so.
>
> My remote client has this:
> System.setProperty("aConnectionFactory",
>
> "connectionfactory:org.apache.activemq.ActiveMQConnectionFactory:tcp://localhost:61616");
> ConnectionFactory connectionFactory = (ConnectionFactory)
> initialContext.lookup("java:aConnectionFactory");
>
> tomee.xml has this;
>
> <Resource id="MyJmsResourceAdapter" type="ActiveMQResourceAdapter">
>         BrokerXmlConfig =  broker:(tcp://localhost:61616)
>         ServerUrl       = vm://localhost
>     </Resource>
>     <Resource id="MyJmsConnectionFactory"
> type="javax.jms.ConnectionFactory">
>         ResourceAdapter = MyJmsResourceAdapter
>     </Resource>
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676173.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: How to reference connection factory and queues from code

Posted by hkgot <ha...@gmail.com>.
I think so.

My remote client has this:
System.setProperty("aConnectionFactory",
"connectionfactory:org.apache.activemq.ActiveMQConnectionFactory:tcp://localhost:61616");
ConnectionFactory connectionFactory = (ConnectionFactory)
initialContext.lookup("java:aConnectionFactory");

tomee.xml has this;

<Resource id="MyJmsResourceAdapter" type="ActiveMQResourceAdapter">
        BrokerXmlConfig =  broker:(tcp://localhost:61616)
        ServerUrl       = vm://localhost
    </Resource>
    <Resource id="MyJmsConnectionFactory"
type="javax.jms.ConnectionFactory">
        ResourceAdapter = MyJmsResourceAdapter
    </Resource>



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676173.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: How to reference connection factory and queues from code

Posted by Romain Manni-Bucau <rm...@gmail.com>.
do both use the same broker? - ie do you have set up the connection factory
accordingly?


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-09 20:17 GMT-07:00 hkgot <ha...@gmail.com>:

> System.setProperty("aQueue",
> "queue:org.apache.activemq.command.ActiveMQQueue:LISTENER");
>
> final Queue destination = (Queue) context.lookup("java:aQueue"); ------>
> This seems to be working
>
> In ejb.jar.xml:
>  <enterprise-beans>
>
>     <message-driven>
>       <ejb-name>MyMessageBean</ejb-name>
>       <ejb-class>com.xyz.MyMessageBean</ejb-class>
>         <messaging-type>javax.jms.MessageListener</messaging-type>
>       <transaction-type>Container</transaction-type>
> <activation-config>
>           <activation-config-property>
>
>
> <activation-config-property-name>destination</activation-config-property-name>
>
> <activation-config-property-value>aQueue</activation-config-property-value>
>           </activation-config-property>
>           .
>           .
>           .
>
>
> The above configuration is not invoking MyMessageBean even after I send a
> message to aQueue.
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676171.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: How to reference connection factory and queues from code

Posted by hkgot <ha...@gmail.com>.
System.setProperty("aQueue",
"queue:org.apache.activemq.command.ActiveMQQueue:LISTENER");

final Queue destination = (Queue) context.lookup("java:aQueue"); ------>
This seems to be working

In ejb.jar.xml:
 <enterprise-beans>
    
    <message-driven>
      <ejb-name>MyMessageBean</ejb-name>
      <ejb-class>com.xyz.MyMessageBean</ejb-class>
        <messaging-type>javax.jms.MessageListener</messaging-type>
      <transaction-type>Container</transaction-type>
<activation-config>
          <activation-config-property>
             
<activation-config-property-name>destination</activation-config-property-name>
             
<activation-config-property-value>aQueue</activation-config-property-value>
          </activation-config-property>
          .
          .
          .


The above configuration is not invoking MyMessageBean even after I send a
message to aQueue. 



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676171.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: How to reference connection factory and queues from code

Posted by Romain Manni-Bucau <rm...@gmail.com>.
2015-09-09 18:35 GMT-07:00 hkgot <ha...@gmail.com>:

> OK, I am going with system properties as per your suggestion. This is what
> I
> set:
>
> System.setProperty("aQueue",
> "queue:org.apache.activemq.command.ActiveMQQueue:LISTENER");
>
> aQueue is the queue name or queue is the queue name?
>
>
aQueue


> With this I could create a message on the queue. How do I link my MDB
> listen
> to this queue? What queue name should I use in ejb-jar.xml for my MDB?
>
>
>
use destination activation config:
http://tomee.apache.org/jms-resources-and-mdb-container.html


>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676166.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: How to reference connection factory and queues from code

Posted by hkgot <ha...@gmail.com>.
OK, I am going with system properties as per your suggestion. This is what I
set:

System.setProperty("aQueue",
"queue:org.apache.activemq.command.ActiveMQQueue:LISTENER");

aQueue is the queue name or queue is the queue name? 

With this I could create a message on the queue. How do I link my MDB listen
to this queue? What queue name should I use in ejb-jar.xml for my MDB?



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676166.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: How to reference connection factory and queues from code

Posted by Romain Manni-Bucau <rm...@gmail.com>.
on server side http://tomee.apache.org/containers-and-resources.html
applies, on client side you need to either use the system properties or a
"custom" resource ie use class-name config, nothing more complicated, just
declare what you need.


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-08 6:52 GMT-07:00 hkgot <ha...@gmail.com>:

> I am confused.... Your sample creates connection factory and queues on fly
> instead of using from config files. If I create on fly, how do I manage
> connection factory with min/max connections etc.?
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676163.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: How to reference connection factory and queues from code

Posted by hkgot <ha...@gmail.com>.
I am confused.... Your sample creates connection factory and queues on fly
instead of using from config files. If I create on fly, how do I manage
connection factory with min/max connections etc.?



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676163.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: How to reference connection factory and queues from code

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Then the sample i referenced does exactly that
Le 7 sept. 2015 21:51, "hkgot" <ha...@gmail.com> a écrit :

> My remote client is not on the same JVM where EAR is. It's on a separate
> machine.
>
> Sorry, I couldn't understand your explanation. Apologies for my lack of
> understanding. Is there a sample code/configuration setting that will show
> how to reference same connection factory, queues from server and remote
> clients? Thx.
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676161.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: How to reference connection factory and queues from code

Posted by hkgot <ha...@gmail.com>.
My remote client is not on the same JVM where EAR is. It's on a separate
machine. 

Sorry, I couldn't understand your explanation. Apologies for my lack of
understanding. Is there a sample code/configuration setting that will show
how to reference same connection factory, queues from server and remote
clients? Thx.



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676161.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: How to reference connection factory and queues from code

Posted by Romain Manni-Bucau <rm...@gmail.com>.
still inline ;)

2015-09-07 20:14 GMT-07:00 hkgot <ha...@gmail.com>:

> "ConnectionFactory can be defined using a custom resource
> (with class-name), I guess for remote connections it can be the best
> solution but if parts of your ear are communicating together using local
> transport would make smooth to just reuse the same server resources."
>
> If I understand this correct, you are suggesting to reuse server resources
> both from server and remote client. Right? If yes, how do I reuse/reference
> the same server resources from remote client code?
>
>
this was my question, is "parts" of your ear are in the same JVM you just
reference them using the id as name in @Resource. If not this still works
for destinations but for connection factories you need to use next trick.


> Sorry, I couldn't understand custom resource (with class-name) - could you
> elobarate more on this? Thx.
>
>
>
In TomEE you can define a @Resource using: <Resource id="..."
class-name="xxxx">props as usual</Resource>.

This will do a new xxx(); and match properties using xxx setters. This
allows you to use any type as injection in your app. I would use it to
build a full client connection factory instead of relying on a server one
(see activemq doc for more details on the difference between the resource
adapter and the clients).


>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676158.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: How to reference connection factory and queues from code

Posted by hkgot <ha...@gmail.com>.
"ConnectionFactory can be defined using a custom resource 
(with class-name), I guess for remote connections it can be the best 
solution but if parts of your ear are communicating together using local 
transport would make smooth to just reuse the same server resources."

If I understand this correct, you are suggesting to reuse server resources
both from server and remote client. Right? If yes, how do I reuse/reference
the same server resources from remote client code?

Sorry, I couldn't understand custom resource (with class-name) - could you
elobarate more on this? Thx.





--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676158.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: How to reference connection factory and queues from code

Posted by Romain Manni-Bucau <rm...@gmail.com>.
2015-09-07 19:51 GMT-07:00 hkgot <ha...@gmail.com>:

> "if I read it right, activemq is using name + connection to identify a
> queue
> so referencing the same name and the right connection will do the job"
>
> What is the right connection from EAR client side? Your sample code takes
> care of remote client, what about EAR client code?
>
>
All server side code can reuse out of the box server resources, for
destinations (queues, topic) nothing different from using normal server
queues/topics. ConnectionFactory can be defined using a custom resource
(with class-name), I guess for remote connections it can be the best
solution but if parts of your ear are communicating together using local
transport would make smooth to just reuse the same server resources.


> If I call EAR client as "Server" and remote client as "Client", what is the
> simple way to reference same connection factory and queues both from Server
> and Client?
>
>
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676156.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: How to reference connection factory and queues from code

Posted by hkgot <ha...@gmail.com>.
"if I read it right, activemq is using name + connection to identify a queue 
so referencing the same name and the right connection will do the job"

What is the right connection from EAR client side? Your sample code takes
care of remote client, what about EAR client code?

If I call EAR client as "Server" and remote client as "Client", what is the
simple way to reference same connection factory and queues both from Server
and Client? 





--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676156.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: How to reference connection factory and queues from code

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

2015-09-07 19:02 GMT-07:00 hkgot <ha...@gmail.com>:

> Hi Romain,
>
> Thanks for the quick response. Appreacited!!!
>
> My problem is this:
>
> Active MQ runs as embedded inside Tomee.
>
> Remote client sends a message M1 to queue Q1 (Remote client)
> MDB part of ear on the same Tomee receives and processes message M1 from
> queue Q1
> MDB part of ear on the same Tomee sends another message M2 to queue Q2
> (Client part of EAR)
>
> Q1: How both clients can reference the same queues?
>
>
if I read it right, activemq is using name + connection to identify a queue
so referencing the same name and the right connection will do the job


> Q2: what is the point of creating connection factory and queues in
> Tomee.xml
> if I create both dynamically on the client side?
>
>
>
the server needs it ;). No point for the clients.


>
>
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676154.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: How to reference connection factory and queues from code

Posted by hkgot <ha...@gmail.com>.
Hi Romain, 

Thanks for the quick response. Appreacited!!!

My problem is this:

Active MQ runs as embedded inside Tomee. 

Remote client sends a message M1 to queue Q1 (Remote client)
MDB part of ear on the same Tomee receives and processes message M1 from
queue Q1
MDB part of ear on the same Tomee sends another message M2 to queue Q2
(Client part of EAR)

Q1: How both clients can reference the same queues? 

Q2: what is the point of creating connection factory and queues in Tomee.xml
if I create both dynamically on the client side?







--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676154.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: How to reference connection factory and queues from code

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

Actually client is not using the server resource for 2 main reasons but you
can define multiple client resources using the id instead of the qualified
name IIRC.
Reasons are:

- way to make it working would be to
-- get a fully remote resource (would be slow and inefficient)
-- guess what you need on client side (would never be adapted)
-- allow you to define client resources on server side (what would be the
point?)
- each client can need a different config depending its needs and usage of
the resource





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-07 18:37 GMT-07:00 hkgot <ha...@gmail.com>:

> Hi Romain,
>
> Finally, I got a chance to try this. The below code is working:
>
> System.setProperty("aConnectionFactory",
>
> "connectionfactory:org.apache.activemq.ActiveMQConnectionFactory:tcp://localhost:11616");
> System.setProperty("aQueue",
> "queue:org.apache.activemq.command.ActiveMQQueue:LISTENER");
> final ConnectionFactory connectionFactory = (ConnectionFactory)
> context.lookup("java:aConnectionFactory");
>
> However, I am not sure whether this is creating a new connection factory or
> referencing the connection factory from tomee.xml. Because what I
> configured
> in tomee.xml is MyJmsConnectionFactory, not connectionFactory.
>
> <Resource id="MyJmsConnectionFactory" type="javax.jms.ConnectionFactory">
>         ResourceAdapter = MyJmsResourceAdapter
>     </Resource>
>
> In the sample you provided, aConnectionFactory is referencing
>
> connectionfactory:org.apache.activemq.ActiveMQConnectionFactory:tcp://localhost:11616,
> not MyJmsConnectionFactory. How do I reference to MyJmsConnectionFactory?
>
> Thx.
>
>
>
>
>
>
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/How-to-reference-connection-factory-and-queues-from-code-tp4675980p4676152.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>