@JmsListener用于发布-订阅主题 [英] @JmsListener usage for publish-subscribe topic
问题描述
我正在尝试基于@JmsListener注释创建用于发布-订阅的示例:
I am trying to create example for publish-subscribe based on @JmsListener annotation: https://github.com/lkrnac/book-eiws-code-samples/tree/master/05-jms/0515-publish-subscribe
相关代码段:
@Slf4j
@SpringBootApplication
@EnableScheduling
public class JmsPublishSubscribeApplication {
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(JmsPublishSubscribeApplication.class, args);
}
@Bean
public ActiveMQTopic simpleTopic() {
return new ActiveMQTopic("simpleTopic");
}
}
@Component
public class SimpleMessageListener1 {
@JmsListener(destination = "simpleTopic")
public void readMessage(String message) {
//....
}
}
@Component
public class SimpleMessageListener2 {
@JmsListener(destination = "simpleTopic")
public void readMessage(String message) {
//....
}
}
问题是要获得这种行为:
The problem is that is get this behaviour:
2015-05-17 20:07:04.985 INFO 22983 --- [pool-1-thread-1] n.l.b.e.chapter05.SimpleMessageSender : Sending message: simple message
2015-05-17 20:07:05.070 INFO 22983 --- [enerContainer-1] n.l.b.e.c.JmsPublishSubscribeApplication : Message Received: simple message via listener 2
2015-05-17 20:07:05.975 INFO 22983 --- [pool-1-thread-1] n.l.b.e.chapter05.SimpleMessageSender : Sending message: simple message
2015-05-17 20:07:05.986 INFO 22983 --- [enerContainer-1] n.l.b.e.c.JmsPublishSubscribeApplication : Message Received: simple message via listener 1
2015-05-17 20:07:06.975 INFO 22983 --- [pool-1-thread-1] n.l.b.e.chapter05.SimpleMessageSender : Sending message: simple message
2015-05-17 20:07:06.987 INFO 22983 --- [enerContainer-1] n.l.b.e.c.JmsPublishSubscribeApplication : Message Received: simple message via listener 2
2015-05-17 20:07:07.975 INFO 22983 --- [pool-1-thread-1] n.l.b.e.chapter05.SimpleMessageSender : Sending message: simple message
2015-05-17 20:07:07.994 INFO 22983 --- [enerContainer-1] n.l.b.e.c.JmsPublishSubscribeApplication : Message Received: simple message via listener 1
但是,根据主题的定义,每个消息都应由两个侦听器使用.我想念什么?
But each message should be consumed by both listeners by definition of topics. What am I missing?
推荐答案
使用@JmsListener
时,它使用的DefaultMessageListenerContainer
扩展了
When using a @JmsListener
it uses a DefaultMessageListenerContainer
which extends JmsDestinationAccessor
which by default has the pubSubDomain
set to false
. When this property is false it is operating on a queue. If you want to use topics you have to set this properties value to true
.
使用 Spring Boot ,您可以通过将spring.jms.pub-sub-domain
属性添加到application.properties
并将其设置为true
来轻松地将此属性设置为true.
As you are using Spring Boot you can quite easily set this property to true by adding the spring.jms.pub-sub-domain
property to the application.properties
and set it to true
.
spring.jms.pub-sub-domain=true
使用 @JmsListener
它正在寻找一个名为jmsListenerContainerFactory
的bean,如果该豆不可用,则应使用默认的bean.您还可以包括自己的bean,并以编程方式将此属性设置为true
.
When using a @JmsListener
it is looking for a jmsListenerContainerFactory
named bean, if that isn't available a default one is expected. You can also include your own bean and programmatically set this property yo true
.
@Bean
public DefaultMessageListenerContainer jmsListenerContainerFactory() {
DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
dmlc.setPubSubDomain(true);
// Other configuration here
return dmlc;
}
This would of course also work but would be more work, more information on this can be found in the documentation of the @EnableJms
annotation.
这篇关于@JmsListener用于发布-订阅主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!