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.
[EDITED, THE FOLLOWING CODE WORKS]
Producer:
@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;
}
});
}
}
Consumer:
@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();
}
}
}
}
Producer Configuration:
<context:component-scan base-package="mrpomario.springcore.jms"/> <!-- finds the JmsMessageProducer -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:8082"/>
</bean>
<bean id="pomarioQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="mrpomario.springcore.jms.queue"/>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestination" ref="pomarioQueue"/>
</bean>
Consumer Configuration:
<jms:listener-container>
<jms:listener ref="jmsMessageConsumer" method="onMessage" destination="mrpomario.springcore.jms.queue"/>
</jms:listener-container>
<bean id="pomarioQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="mrpomario.springcore.jms.queue"/>
</bean>
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:8082"/>
</bean>
<amq:broker id="broker" useJmx="false" persistent="false">
<amq:transportConnectors>
<amq:transportConnector uri="tcp://localhost:8082" />
</amq:transportConnectors>
</amq:broker>
Test Case:
@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");
}
}
}
I run the producer inside a Java EE container (mvn jetty:run) where a Spring MVC application also runs.