我正在尝试运行一个非常基本的应用程序来学习Spring JMS + ActiveMQ。我看到我的Producer创建了消息(sysout),但我的Consumer中没有显示任何内容,也没有抛出任何异常。我想我在这里缺少一些简单的东西;非常感谢任何帮助。
[已编辑,以下代码作品]
制片:
@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;
}
});
}
}
消费者:
@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();
}
}
}
}
制作人配置:
<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>
消费者配置:
<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>
测试用例:
@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");
}
}
}
我在Java EE容器(mvn jetty:run)中运行生产者,Spring MVC应用程序也运行在该容器中。
答案 0 :(得分:0)
我认为你最终会向两个完全不同的经纪人(以及随后不同的队列)发送和消费:
让你的经纪人只在一个地方定义,比如你的制作人配置:
<amq:broker id="broker" useJmx="false" persistent="false">
<amq:transportConnectors>
<amq:transportConnector uri="tcp://localhost:8082" />
<amq:transportConnector uri="vm://localhost" />
</amq:transportConnectors>
</amq:broker>
<amq:connectionFactory id="jmsFactory" brokerURL="vm://localhost" />
从您的使用者配置中删除代理配置并且只有connectionFactory: 要么(如果你在同一个VM中):
<amq:connectionFactory id="jmsFactory" brokerURL="vm://localhost" />
或(如果是远程):
<amq:connectionFactory id="jmsFactory" brokerURL="tcp://localhost:8082" />