0

I have two different apps for sender and receiver.

sender:

@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;
    }
}

and receiver:

@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);
    }
}

I see

msg_1
msg_2
...

on the receiver but I don't see the topic messages.

What am I doing wrong?

P.S.

management console:

enter image description here

enter image description here enter image description here

enter image description here

enter image description here

enter image description here

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

2 Answers2

0

Subscriptions to topics are not durable by default - you are probably sending the messages before the listener has started.

Try adding a Thread.sleep() before sending the messages to the topic.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
0

My receiver became to receive mesagges after adding following bean to the context:

    @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;
    }
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710