PDA

View Full Version : JMS / ActiveMQ / Tomcat / Spring Integration



whyBish
Feb 24th, 2008, 02:58 PM
I'm trying to prototype the integration part of an upcoming project. (I've read the message patterns, and know roughly what is going on at a high level with asynch messaging, but haven't done anything hands onwith it before). What I want to end up with is:

1) Client Webapp that sends messages to a queue and reads responses from a response queue.
2) Either another Webapp that host the queue, or set up the server to host the queue
3) Server Webapp that processes the client messages and provides a response.

What I have at the moment is:
applicationContext-integration.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">

<annotation-driven />
<message-bus error-channel="errorChannel" />

<channel id="errorChannel" publish-subscribe="true" capacity="500" />

<channel id="testChannel"/>
<endpoint input-channel="testChannel" handler-ref="testSender">
<!-- NOTE: A.B. 22/2/8 concurrency has some reasonable defaults but we probably want to explicitly define them because we will have some knowledge about the load and performance profiles we expect -->
<concurrency core="2" max="4" queue-capacity="100" keep-alive="20" />
</endpoint>
<jms-source connection-factory="connectionFactory" destination="testInputQueue" channel="testChannel"/>
<beans:bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<beans:property name="brokerURL"><beans:value>tcp://localhost:61616</beans:value></beans:property>
</beans:bean>
<!-- <jms-target connection-factory="connectionFactory" destination="outputQueue" channel="outputChannel1"/> -->

<beans:bean id="testSender"
class="xxx.TestHandler">
</beans:bean>
</beans:beans>


(buggy) TestHandler.java


public class TestHandler implements MessageHandler {

public Message<?> handle(Message<?> message) {
System.out.println("I handled a message");
return null;
}

public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
"/main/webapp/WEB-INF/applicationContext-integration.xml", TestHandler.class);
context.start();
ChannelRegistry channelRegistry = (ChannelRegistry) context.getBean(MessageBusParser.MESSAGE_BUS_BEAN_ NAME);
MessageChannel inputChannel = channelRegistry.lookupChannel("testChannel");
MessageChannel outputChannel = channelRegistry.lookupChannel("testChannel");
inputChannel.send(new StringMessage("World"));
System.out.println(outputChannel.receive().getPayl oad());
context.stop();
}
}


META-INF/context.xml


<Context>
<Resource name="jms/ConnectionFactory" auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory" brokerURL="vm://localhost" brokerName="LocalActiveMQBroker"/>
<!-- Publish - subscribe example -->
<!--Resource name="jms/someTopic" auth="Container" type="org.apache.activemq.command.ActiveMQTopic" description="my Topic"
factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="FOO.BAR"/-->

<Resource name="testInputQueue" auth="Container" type="org.apache.activemq.command.ActiveMQQueue" description="my Queue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="FOO.BAR"/>
</Context>


When I start the TestHandler it attempts to load applicationContext-integration.xml but fails with "No bean named 'testInputQueue' is defined"
What am I supposed to do to get the connectionFactory pointing to the deployed jms/connectionFactory rather than a locally created instance?

Tomcat 6.0
Spring Integration 1.0.0.m1
ActiveMQ 5.0.0

Mark Fisher
Feb 24th, 2008, 04:16 PM
You can use something like the following (in your spring configuration):


<jee:jndi-lookup id="connectionFactory" jndi-name="jms/ConnectionFactory" resource-ref="true"/>

<jee:jndi-lookup id="testInputQueue" jndi-name="testInputQueue" resource-ref="true"/>

whyBish
Feb 24th, 2008, 04:54 PM
That results in a 4999 line stack trace with most lines complaining about:
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

Mark Fisher
Feb 24th, 2008, 07:11 PM
You need to provide the JNDI environment properties. Typically that is done with a "jndi.properties" file on the classpath. Otherwise, you can set the properties with a <jee:jndi-environment/> sub-element of the <jee:jndi-lookup/>.

Have a look at the jndi-lookup examples in the reference (especially the inclusion of the jndi environment properties): http://static.springframework.org/spring/docs/2.5.x/reference/xsd-config.html#xsd-config-body-schemas-jee-jndi-lookup

You can consult your JNDI provider's documentation for the required property values (e.g. java.naming.factory.initial=???)

-Mark

whyBish
Feb 24th, 2008, 07:22 PM
Thanks for the help. I've managed to get something going :) where the messages are handled, but have used a local ActiveMQ deployment rather than using Tomcat. Will try using Tomcat again after I get some more sleep.