我是JMS消息传递的新手。我想向主题发布消息,订阅者应该接收它。这是我的spring bean定义。
<bean id="mySampleListener" class="com.my.sample.jms.MySampleListener" />
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${broker.url}" />
</bean>
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="amqConnectionFactory" />
</bean>
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="sampleTopic"/>
</bean>
<bean id="sampleMessageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destinationName" value="sampleTopic" />
<property name="messageListener" ref="mySampleListener" />
<property name="pubSubDomain" value="true" />
<property name="sessionAcknowledgeModeName" value="AUTO_ACKNOWLEDGE"/>
</bean>
<bean id="sampleJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="topicDestination" />
</bean>
我在JmsPublisher.class的构造函数中发布消息
<bean class="com.my.sample.jms.JmsPublisher">
<constructor-arg index="0" ref="sampleJmsTemplate" />
</bean>
但我的听众不会被处决。
public class MySampleListener implements MessageListener {
private static final Logger LOGGER = LoggerFactory.getLogger(MySampleListener.class);
@Override
public void onMessage(final Message message) {
LOGGER.info("Message received. " + message);
}
}
public class JmsPublisher {
private static final Logger LOGGER = LoggerFactory.getLogger(JmsPublisher.class);
public JmsPublisher(final JmsTemplate jmsTemplate) {
LOGGER.info("Trying to publish JMS message...");
jmsTemplate.send(new MessageCreator() {
@Override
public Message createMessage(final Session session) throws JMSException {
final Message message = session.createTextMessage("test123");
return message;
}
});
LOGGER.info("Published JMS message.");
}
}