【问题标题】:Receiver doesn't receive messages from topic接收者没有收到来自主题的消息
【发布时间】:2017-08-17 17:23:31
【问题描述】:

我有两个不同的应用程序用于发送者和接收者。

发件人:

@SpringBootApplication
public class RabbitJmsApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(RabbitJmsApplication.class, args);
    }

    @Autowired
    private JmsTemplate template;
    @Autowired
    private JmsTemplate topicTemplate;

    @Override
    public void run(String... arg0) throws Exception {
        for (int i = 0; i < 10; i++) {
            template.convertAndSend("my_queue", "msg_" + i);
            Thread.sleep(100);
        }
        for (int i = 0; i < 10; i++) {
            topicTemplate.convertAndSend("my_topic", "topic_msg_" + i);
            Thread.sleep(100);
        }
    }

    @Bean
    public RMQConnectionFactory connectionFactory() {
        return new RMQConnectionFactory();
    }

    @Bean
    public JmsTemplate template() {
        return new JmsTemplate(connectionFactory());
    }

    @Bean
    public JmsTemplate topicTemplate() {
        final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory());
        jmsTemplate.setPubSubDomain(true);
        return jmsTemplate;
    }
}

和接收者:

@Component
public class Listener {

    @JmsListener(destination = "my_queue")
    public void receive(String str){
        System.out.println(str);
    }
    @JmsListener(destination = "my_topic")
    public void receiveTopic(String str){
        System.out.println(str);
    }
}

我明白了

msg_1
msg_2
...

在接收器上,但我没有看到主题消息。

我做错了什么?

附言

管理控制台:

【问题讨论】:

    标签: java rabbitmq jms spring-jms


    【解决方案1】:

    默认情况下,主题订阅不是持久的 - 您可能在侦听器启动之前发送消息。

    在向主题发送消息之前尝试添加Thread.sleep()

    【讨论】:

    • Garry,正如我在主题中所写 - 它是 2 个不同的应用程序。此外,我不会停止听众。我只是重新启动发件人再次发送消息
    • 我已添加管理控制台屏幕。也许它可以帮助
    【解决方案2】:

    在将以下 bean 添加到上下文后,我的接收器开始接收消息:

        @Bean
        public JmsListenerContainerFactory<?> myFactory(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;
        }
    

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 2018-09-15
      • 2013-08-13
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 2019-04-21
      相关资源
      最近更新 更多