Starting ActiveMQ message listener in Tomcat?

Posted by Nick Swarr on Stack Overflow See other posts from Stack Overflow or by Nick Swarr
Published on 2011-01-13T15:50:49Z Indexed on 2011/01/13 15:53 UTC
Read the original article Hit count: 322

Filed under:
|
|

I've created an ActiveMQ MessageListener and configured it using Spring. I'm hosting the listener in Tomcat. When I start up the web application (that features only the listener), should the listener automatically start? Or do I need additional configuration?

Here's what I have. First, updated the web.xml to allow spring to configure itself on startup,

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/spring/applicationContext.xml</param-value>
    </context-param>

</web-app>

Then I created the applicationContext.xml to configure the ActiveMQ listener,

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:amq="http://activemq.apache.org/schema/core"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.somepackage"/>

    <context:property-placeholder location="classpath:env.properties"/>

    <bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616" />
    </bean>

    <bean id="documentListener" class="com.somepackage.SomeListener" />

    <bean id="container"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="cachingConnectionFactory"/>
        <property name="messageListener" ref="documentListener"/>
        <property name="destinationName" value="STOCKS.MSFT" />
    </bean>

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

And that's it. Based on what I've seen around the web, I can't tell if that's all I need? Maybe I need some other configuration in Tomcat to kick off the listener?

© Stack Overflow or respective owner

Related posts about tomcat6

Related posts about listener