title: Spring JMS configuration of a queue Consumer
body: <p>I am trying to run a very basic application to learn Spring JMS + ActiveMQ. I see my Producer creating the message (sysout), but nothing shows up in my Consumer and no exception is thrown. I think I am missing something simple here; would really appreciate any help.</p>
  
  <p>[EDITED, THE FOLLOWING CODE WORKS]</p>
  
  <p>Producer:</p>
  
  <pre><code>@Component
  public class JmsMessageProducer
  {
      @Autowired
      private JmsTemplate template;
  
      public void generateMessages() throws JMSException
      {
          template.send(new MessageCreator()
          {
              public Message createMessage(Session session) throws JMSException
              {
                  System.out.println("sending..");
                  TextMessage message = session.createTextMessage("this is a Producer created message!");
                  return message;
              }
          });
      }
  }
  </code></pre>
  
  <p>Consumer:</p>
  
  <pre><code>@Component
  public class JmsMessageConsumer implements MessageListener {
      @Override
      public void onMessage(Message message) {
          if (message instanceof TextMessage) {
              TextMessage tm = (TextMessage) message;
              try {
                  System.out.println("CONSUMER - received ["+tm.getText()+"]");
              }
              catch (Throwable th) {
                  th.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>Producer Configuration:</p>
  
  <pre><code>&lt;context:component-scan base-package="mrpomario.springcore.jms"/&gt; &lt;!-- finds the JmsMessageProducer --&gt;
  
  &lt;bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"&gt;
      &lt;property name="brokerURL" value="tcp://localhost:8082"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="pomarioQueue" class="org.apache.activemq.command.ActiveMQQueue"&gt;
      &lt;constructor-arg value="mrpomario.springcore.jms.queue"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"&gt;
      &lt;property name="connectionFactory" ref="connectionFactory"/&gt;
      &lt;property name="defaultDestination" ref="pomarioQueue"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Consumer Configuration:</p>
  
  <pre><code>&lt;jms:listener-container&gt;
      &lt;jms:listener ref="jmsMessageConsumer" method="onMessage" destination="mrpomario.springcore.jms.queue"/&gt;
  &lt;/jms:listener-container&gt;
  
  &lt;bean id="pomarioQueue" class="org.apache.activemq.command.ActiveMQQueue"&gt;
      &lt;constructor-arg value="mrpomario.springcore.jms.queue"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"&gt;
      &lt;property name="brokerURL" value="tcp://localhost:8082"/&gt;
  &lt;/bean&gt;
  
  &lt;amq:broker id="broker" useJmx="false" persistent="false"&gt;
      &lt;amq:transportConnectors&gt;
          &lt;amq:transportConnector uri="tcp://localhost:8082" /&gt;
      &lt;/amq:transportConnectors&gt;
  &lt;/amq:broker&gt;
  </code></pre>
  
  <p>Test Case:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration("classpath:mrpomario/springcore/jms/jms-config.xml")
  public class JmsTest
  {
      @Autowired
      JmsMessageProducer jmsMessageProducer;
  
      @Test
      public void test_Single_Queue_Producer_and_Consumer_Unidirectional() throws JMSException
      {
          try
          {
              jmsMessageProducer.generateMessages();
              assertTrue(true);
          }
          catch (Throwable th)
          {
              System.out.println("\n\nJmsTest: remote invocation failed. Ensure the web server is running.\n\n");
          }
      }
  }
  </code></pre>
  
  <p>I run the producer inside a Java EE container (mvn jetty:run) where a Spring MVC application also runs.</p>