Starting ActiveMQ message listener in Tomcat?

2019-07-15 09:15发布

问题:

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?

回答1:

This was actually working. I was debugging this using Eclipse and I had to do a couple refreshes, cleans and restarts before it suddenly decided to work.

To answer my own question, yes, this is all that is required in order to run a listener in a web application from within Tomcat. It can be totally configured with XML only.