title: JMS Standalone consumer with ActiveMQ and Spring
body: <p>Historically I've deployed my JMS consumer applications as Spring webapps deployed under Tomcat (Windows box).  These consumers will then run alongside my other webapps under the same Tomcat instance.  As the number of consumers that I use has grown, however, I've realized that this is turning into a bit of a maintenance nightmare.</p>
  
  <p>My solution was to convert these webapps to "main method" standalone apps deployed as jars.  In fact, I was able to successfully package them all together in an attempt to reuse as many resources (DAO, dependencies, etc) as possible.</p>
  
  <p>Here's what my main method looks like:</p>
  
  <pre><code>public static void main(String[] args) {
  
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  
      try {
          FooListener fooListener = (FooListener) context.getBean("fooListener");
  
          fooListener.start();
      } catch (Exception e) {
          logger.error(e.getMessage(), e);
      }
  
      try {
          BarListener barListener = (BarListener) context.getBean("barListener");
  
          barListener.start();
      } catch (Exception e) {
          logger.error(e.getMessage(), e);
      }
  }
  </code></pre>
  
  <p>My question(s):</p>
  
  <ol>
  <li>Do I need to do anything special to shut down my JMS connections in my main method application, or will they shut down just fine as the application terminates?</li>
  <li>Does anyone have any personal preferences or otherwise regarding whether to deploy jms consumers using tomcat or as standalone apps?</li>
  </ol>
  
  <p>Edit:</p>
  
  <p>A bit more information: FooListener and BarListener extend the following abstract class.  They inherit their values from the corresponding beans in the applicationContext.xml file, and they both override the onMessage() method to asynchronously consume messages.</p>
  
  <pre><code>public abstract class TextMessageListener implements MessageListener {
  
      protected ConnectionFactory connectionFactory;
  
      protected String queueName;
  
      protected String selectors;
  
      public void start() throws JMSException {
          Connection connection = connectionFactory.createConnection();
  
          Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
  
          MessageConsumer consumer = session.createConsumer(session.createQueue(queueName), selectors);
  
          consumer.setMessageListener(this);
  
          connection.start();
      }
  
      public abstract void onMessage(Message message);
  }
  </code></pre>