activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cristisor <cristisor...@yahoo.com>
Subject Re: Basic ActiveMQ example help
Date Thu, 18 Oct 2012 14:23:25 GMT
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.

Mime
View raw message