You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by cristisor <cr...@yahoo.com> on 2012/10/16 16:04:29 UTC

Basic ActiveMQ example help

Hello,

I need to do a simple application using JMS/ActiveMQ and after reading many
tutorials, where someone says something a little bit different that the guy
before him, I'm completely lost. Please help me with the following
questions:

1. How can I add destinations to be supported in Tomee+ so that I can lookup
for them later, a connection factory, a queue and a topic to begin with?
2. With Tomee+ started I should be able to see the destinations in the
web-console Queues and Topics sections, right?
3. Can I get the ActiveMQInitialContextFactory from outside Tomee+ and use
it to lookup for a ConnectionFactory, a Topic or a Queue in a basic java
application? I only found a tutorial on how to get
"RemoteInitialContextFactory" and lookup for a certain stub.

For example, the following links talk about configuring JMS resources in
Tomcat, but the explanations are very poor for a beginner with the
enterprise applications:
http://activemq.apache.org/setting-up-activemq-with-tomcat-559.html
http://rocksolutions.wordpress.com/2010/12/03/configure-apache-activemq-with-tomcat-6/
http://tomee.apache.org/jms-resources-and-mdb-container.html

Thank you very much.



--
View this message in context: http://activemq.2283324.n4.nabble.com/Basic-ActiveMQ-example-help-tp4657851.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: Basic ActiveMQ example help

Posted by cristisor <cr...@yahoo.com>.
After many hours of struggling to understand how to make a basic application
using Tomee as the destinations provider and Spring for the web application,
I want to post some code pieces for everyone interested.

Resources configured in tomee.xml with embeded ActiveMQ support:

	<Resource id="JMSAdapter" type="ActiveMQResourceAdapter">
		BrokerXmlConfig = (tcp://localhost:61616)?useJmx=true;
	</Resource>

	<Resource id="jms/connectionFactory" type="javax.jms.ConnectionFactory">
		ResourceAdapter = JMSAdapter
	</Resource>
	
	<Resource id="jms/queue" type="javax.jms.Queue">
		destination = Queue1
	</Resource>
	
	<Resource id="jms/topic" type="javax.jms.Topic">
		destination = Topic1
	</Resource>

Spring web application configuration:
web.xml
    <resource-ref>
        <description>JMS Connection Factory</description>
        <res-ref-name>jms/connectionFactory</res-ref-name>
        <res-type>javax.jms.ConnectionFactory</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    
    <resource-env-ref>
	<description>JMS Queue</description>
	<resource-env-ref-name>jms/queue</resource-env-ref-name>
	<resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>
    </resource-env-ref>

web-server-servlet.xml
    <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">
            <props>
                <prop
key="java.naming.factory.initial">org.apache.openejb.client.LocalInitialContextFactory</prop>
            </props>
        </property>
    </bean>

	<bean id="connectionFactory"
class="org.springframework.jndi.JndiObjectFactoryBean">
	    <property name="jndiTemplate" ref="jndiTemplate"/>
	    <property name="jndiName" value="jms/connectionFactory"/>
	    <property name="resourceRef" value="true" />
	</bean>

	<bean id="cachingConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
	    <property name="targetConnectionFactory" ref="connectionFactory"/>
	    <property name="sessionCacheSize" value="3"/>
	</bean>

	<bean id="destinationResolver"
class="org.springframework.jms.support.destination.JndiDestinationResolver">
	    <property name="jndiTemplate" ref="jndiTemplate"/>
	    <property name="cache" value="true"/>
	    <property name="fallbackToDynamicDestination" value="false"/>
	</bean>

	<bean id="messageListener" class="com.listener.QueueMessageListener"/>

	<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
	    <property name="connectionFactory" ref="cachingConnectionFactory"/>
	    <property name="destinationResolver" ref="destinationResolver"/>
	    <property name="concurrentConsumers" value="3" />
	    <property name="destinationName" value="java:comp/env/jms/queue"/>
	    <property name="messageListener" ref="messageListener" />
	    <property name="sessionAcknowledgeModeName" value="AUTO_ACKNOWLEDGE"/>
	</bean>

	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="cachingConnectionFactory"/>
		<property name="destinationResolver" ref="destinationResolver"/>
	</bean>
	
	<bean id="sender" class="com.sender.JMSSender">
		<property name="jmsTemplate" ref="jmsTemplate"></property>
	</bean>

I want to mention some things:
a. because the application was deployed in Tomee also, I set
"java.naming.factory.initial" to
"org.apache.openejb.client.LocalInitialContextFactory" and nothing else
b. for the ConnectionFactory I used only "jms/connectionFactory" but for the
Queue, where the "JndiDestinationResolver" is used I had to give the entire
name: "java:comp/env/jms/queue"
c. if the application is outside of the Tomme container, the remote access
settings are:
java.naming.factory.initial =
org.apache.openejb.client.RemoteInitialContextFactory
java.naming.provider.url = http://localhost:8080/tomee/ejb
java.naming.security.principal = tomee
java.naming.security.credentials = tomee
Also, don't forget to add the following property to the remote client in
order to get the remote connection factory:
-DResource/javax.jms.ConnectionFactory=connectionfactory:org.apache.activemq.ActiveMQConnectionFactory:tcp://localhost:61616




--
View this message in context: http://activemq.2283324.n4.nabble.com/Basic-ActiveMQ-example-help-tp4657851p4657939.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: Basic ActiveMQ example help

Posted by cristisor <cr...@yahoo.com>.
Good morning,

In tomee.xml I have the following resources configured:

        <Resource id="JMSAdapter" type="ActiveMQResourceAdapter">
		BrokerXmlConfig = (tcp://localhost:61616)?useJmx=true;
	</Resource>

	<Resource id="JMSConnectionFactory" type="javax.jms.ConnectionFactory">
		ResourceAdapter = JMSAdapter
	</Resource>
	
	<Resource id="JMSQueue" type="javax.jms.Queue">
		destination = Queue1
	</Resource>
	
	<Resource id="Foo" type="javax.jms.Topic">
		destination = Topic1
	</Resource>

I also deployed the activemq web-console and I access it at:
http://localhost:8080/activemq-web-console-5.7.0/index.jsp
<http://localhost:8080/activemq-web-console-5.7.0/index.jsp>   
but the Queues, Topics and Connections tabs don't show any resource.

I suppose that I can only get RemoteInitialContextFactory and not
ActiveMQInitialContextFactory because the resources are deployed in Tomee+
and I can only have access to the server context, right?

Here is my code, please tell me if it's the correct approach:
	final Hashtable<String, String> ctxProps = new Hashtable<String,
String>(4);
	ctxProps.put("java.naming.factory.initial",
"org.apache.openejb.client.RemoteInitialContextFactory");
	ctxProps.put("java.naming.provider.url",
"http://localhost:8080/tomee/ejb");
	ctxProps.put("java.naming.security.principal", "tomee");
	ctxProps.put("java.naming.security.credentials", "tomee");
	InitialContext ctx = new InitialContext(ctxProps);
        ConnectionFactory cf = (ConnectionFactory)
context.lookup("JMSConnectionFactory");

This approach doesn't work:
	final Hashtable<String, String> ctxProps = new Hashtable<String,
String>(2);
	ctxProps.put("java.naming.factory.initial",
"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
	ctxProps.put("java.naming.provider.url", "http://localhost:61616");
	InitialContext ctx = new InitialContext(ctxProps);
        ConnectionFactory cf = (ConnectionFactory)
context.lookup("JMSConnectionFactory");

Is it because the resources are located in tomee.xml?
ActiveMQInitialContextFactory doesn't exist at all in the tomee container, I
can't use it even from an application deployed on tomee?

One last thing, why do I need the parameter below in order to get the
ActiveMQConnectionFactory?
-DResource/javax.jms.ConnectionFactory=connectionfactory:org.apache.activemq.ActiveMQConnectionFactory:tcp://localhost:61616

Thank you very much for your help.



--
View this message in context: http://activemq.2283324.n4.nabble.com/Basic-ActiveMQ-example-help-tp4657851p4657877.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Basic ActiveMQ example help

Posted by cristisor <cr...@yahoo.com>.
Hello,

I configured an ActiveMQResourceAdapter, a ConnectionFactory, a Queue and a
Topic in tomee.xml, like in your URI, but from an application running
outside of Tomcat I could only get "RemoteInitialContextFactory" and then
make a successful look-up for the ConnectionFactory, it didn't work when I
was trying to get the "ActiveMQInitialContextFactory". I'm not at work
anymore, I'll get back tomorrow morning with a code snippet.



--
View this message in context: http://activemq.2283324.n4.nabble.com/Basic-ActiveMQ-example-help-tp4657851p4657861.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: Basic ActiveMQ example help

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

did you try to configure it in tomee.xml?
http://tomee.apache.org/containers-and-resources.html (a bit before the end)

*Romain Manni-Bucau*
*Twitter: @rmannibucau <https://twitter.com/rmannibucau>*
*Blog: **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
*LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
*Github: https://github.com/rmannibucau*




2012/10/16 cristisor <cr...@yahoo.com>

> Hello,
>
> I need to do a simple application using JMS/ActiveMQ and after reading many
> tutorials, where someone says something a little bit different that the guy
> before him, I'm completely lost. Please help me with the following
> questions:
>
> 1. How can I add destinations to be supported in Tomee+ so that I can
> lookup
> for them later, a connection factory, a queue and a topic to begin with?
> 2. With Tomee+ started I should be able to see the destinations in the
> web-console Queues and Topics sections, right?
> 3. Can I get the ActiveMQInitialContextFactory from outside Tomee+ and use
> it to lookup for a ConnectionFactory, a Topic or a Queue in a basic java
> application? I only found a tutorial on how to get
> "RemoteInitialContextFactory" and lookup for a certain stub.
>
> For example, the following links talk about configuring JMS resources in
> Tomcat, but the explanations are very poor for a beginner with the
> enterprise applications:
> http://activemq.apache.org/setting-up-activemq-with-tomcat-559.html
>
> http://rocksolutions.wordpress.com/2010/12/03/configure-apache-activemq-with-tomcat-6/
> http://tomee.apache.org/jms-resources-and-mdb-container.html
>
> Thank you very much.
>
>
>
> --
> View this message in context:
> http://activemq.2283324.n4.nabble.com/Basic-ActiveMQ-example-help-tp4657851.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>