Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to listen to topic using spring boot jms

I am trying to listen to topic using the below snippet. However its listening to queue by default. There is no xml config in this case. I am completely relying on annotations. Moreover I have relied completely on the AutoConfiguration provided by Spring boot. I am not sure how to set the destination type as topic, In JmsListener. Spring JMS gurus please help.

    @Component
    public class MyTopicListener {

        @JmsListener(destination = "${trans.alert.topic}")
        public void receiveMessage(TransactionAlert alert) {
            logger.info("AlertSubscriberEmail :: Sending Email => <" + alert + ">");
        }
    }
like image 900
zikzakjack Avatar asked Oct 20 '16 02:10

zikzakjack


People also ask

How can I send a message to JMS topic in Spring boot?

As we want to send a message to a topic we need to update our SenderConfig configuration. Use the setPubSubDomain() method on the JmsTemplate to set pubSubDomain to true . If you are using the autoconfigured JmsTemplate you can change the JMS domain by setting the spring. jms.

How does JMS listener work?

The JMS Listener adapter operates in an asynchronous mode. It establishes an asynchronous listener on the queue or topic destination specified by the JNDI name of Destination field. When a qualified message arrives at the destination, the adapter immediately processes the message.


1 Answers

The answer marked correct is ALMOST correct. It still wont work because:

factory.setPubSubDomain(true) 

must come AFTER:

configurer.configure(factory, connectionFactory);

Otherwise the pubSubDomain flag being set to true is lost when configuring the defaults and that factory instance will still work with queues and not topics.

like image 140
dleerob Avatar answered Sep 19 '22 13:09

dleerob