@JmsListener用于发布 - 订阅主题的用法

时间:2015-05-17 19:15:41

标签: spring spring-jms

我正在尝试基于@JmsListener注释为发布 - 订阅创建示例: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) {
      //....
    }

}

问题是得到这种行为:

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

但是每个消息都应该由两个侦听器根据主题的定义来使用。我错过了什么?

2 个答案:

答案 0 :(得分:23)

使用@JmsListener时,它使用的DefaultMessageListenerContainer扩展JmsDestinationAccessor,默认情况下pubSubDomain设置为false。当此属性为false时,它将在队列上运行。如果要使用主题,则必须将此属性值设置为true

当您使用Spring Boot时,您可以通过将spring.jms.pub-sub-domain属性添加到application.properties并将其设置为true来轻松地将此属性设置为true。

spring.jms.pub-sub-domain=true

当使用@JmsListener时,它正在寻找一个jmsListenerContainerFactory命名bean,如果没有,则需要默认值。您还可以包含自己的bean,并以编程方式将此属性设置为true

@Bean
public DefaultMessageListenerContainer jmsListenerContainerFactory() {
    DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
    dmlc.setPubSubDomain(true);
    // Other configuration here
    return dmlc;
}

这当然也有效,但会有更多工作,有关这方面的更多信息可以在@EnableJms annotation的文档中找到。

答案 1 :(得分:5)

@JmsListener的默认目标类型从Queue切换到Topic可以在Java中完全完成,无需修改属性或使用XML。

Spring guide包含自定义DefaultMessageListenerContainer提供的默认设置的示例。

它需要定义一个自定义bean,如下所示:

@Bean
public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
                                                DefaultJmsListenerContainerFactoryConfigurer configurer) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    // This provides all boot's default to this factory, including the message converter
    configurer.configure(factory, connectionFactory);
    // You could still override some of Boot's default if necessary.
    factory.setPubSubDomain(true);
    return factory;
}

然后可以在@JmsListener带注释的方法中使用它:

@JmsListener(destination = "mailbox", containerFactory = "myFactory")
public void receiveMessage(Email email) {
    // implementation
}