title: Starting ActiveMQ message listener in Tomcat?
body: <p>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?</p>
  
  <p>Here's what I have.  First, updated the web.xml to allow spring to configure itself on startup,</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/classes/spring/applicationContext.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>Then I created the applicationContext.xml to configure the ActiveMQ listener,</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
      &lt;context:annotation-config /&gt;
      &lt;context:component-scan base-package="com.somepackage"/&gt;
  
      &lt;context:property-placeholder location="classpath:env.properties"/&gt;
  
      &lt;bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory"&gt;
          &lt;property name="brokerURL" value="tcp://localhost:61616" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="documentListener" class="com.somepackage.SomeListener" /&gt;
  
      &lt;bean id="container"
          class="org.springframework.jms.listener.DefaultMessageListenerContainer"&gt;
          &lt;property name="connectionFactory" ref="cachingConnectionFactory"/&gt;
          &lt;property name="messageListener" ref="documentListener"/&gt;
          &lt;property name="destinationName" value="STOCKS.MSFT" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="cachingConnectionFactory"
          class="org.springframework.jms.connection.CachingConnectionFactory"&gt;
          &lt;property name="targetConnectionFactory" ref="jmsFactory" /&gt;
          &lt;property name="sessionCacheSize" value="1" /&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>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?</p>