Try PubSub+
If you haven't already, check out our new Developer Portal! You'll find useful information about Solace PubSub+ as well as handy resources to get you started.

subscribe to multiple topics inside same session?

alinaqvialinaqvi Member Posts: 17
edited October 23 in PubSub+ Software

TopicSubscriber.java (https://github.com/SolaceSamples/solace-samples-jms/blob/master/src/main/java/com/solace/samples/TopicSubscriber.java) gives a good intro to subscribing to a single topic.

What if we want to subscribe to multiple topics ideally with a listener attached to each of those subscriptions. Do we have example code for that some where please?

Thanks,

Best Answer

Answers

  • jeremyjeremy Administrator Posts: 26 admin

    What I am getting from support around this @alinaqvi , is that you would create a second consumer in the same way that we show how the first one is created.

  • marcmarc Member, Moderator, Employee Posts: 33 mod

    There are a few options here.
    1. As @jeremy mentioned, from one session you can create multiple MessageConsumers that each consume from their own endpoint.
    2. You can use wildcards to subscribe to multiple topics, e.g: foo/*/bar or foo/>
    3. Last, and maybe your best option if wildcarding can't give you what you want is to leverage Solace's topic to queue mapping capability. Essentially you would create a queue that attracts messages from a bunch of different topics and then in your code you can just listen to that single queue. See more on that here: https://solace.com/samples/solace-samples-jms/topic-to-queue-mapping/

  • alinaqvialinaqvi Member Posts: 17
    edited October 23

    The documentation for session.getMessageConsumer() says:

    "
    NOTE: For a given JCSMPSession, this method returns a new XMLMessageConsumer object on each call, closing the existing consumer if it exists.
    "
    So basically what happens is that at the last consumer that you start will be the active one. This will receive msgs for all registered topics so far. Right now I have a privately held list of listeners that I map to the topic name/s. When I receive a new message from solace I check what topic it was sent on and then call the appropriate listener/s.

    What I would ideally like to do is use solace API similar to pseudo-code below:

    session.registerForTopicUpdates("abc/topic1" , XMLMsgListener1 )
    session.registerForTopicUpdates("abc/topic2" , XMLMsgListener2 )
    ....

  • amackenzieamackenzie Member, Employee Posts: 4

    @alinaqvi the feature you are looking for is Topic Dispatch. This allows you to set up multiple listeners with topic filters that will dispatch messages to listeners with matching filters.
    Unfortunately, topic dispatch is not available in JCSMP at this time.
    If this functionality is important to you, I suggest logging a support ticket for this as a Feature Request.

  • alinaqvialinaqvi Member Posts: 17

    Thanks for your responses.
    Is JCSMP the recommended API for java clients?

  • alinaqvialinaqvi Member Posts: 17

    Also what I am trying to do here ... would it be possible through the spring integration API ?
    Apologies for being pendantic but it seems like a fundamental use case, can you please double check with your dev team.
    Thanks,

  • marcmarc Member, Moderator, Employee Posts: 33 mod

    Hi,
    Yes! Using our integration with Spring would be a great way to do this.
    Using our Spring Boot JMS Starter & the @JmsListener java annotation would likely be the easiest way to get what you want.
    You can do something like seen in the code below.

    package com.solace.samples.spring.boot;
    
    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.TextMessage;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.jms.annotation.JmsListener;
    
    @SpringBootApplication
    public class SpringBootReceiver {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBootReceiver.class, args);
        }
    
        @JmsListener(destination = "abc/topic1")
        public void handleFoo(Message message) {
    
            if (message instanceof TextMessage) {
                TextMessage tm = (TextMessage) message;
                try {
                    System.out.println("TextMessage Received on destination "+ tm.getJMSDestination());
                } catch (JMSException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                System.out.println("Message of another type was received");
            }
        }
    
    
        @JmsListener(destination = "abc/topic2")
        public void handleBar(Message message) {
    
            if (message instanceof TextMessage) {
                TextMessage tm = (TextMessage) message;
                try {
                    System.out.println("TextMessage Received on destination " + tm.getJMSDestination());
                } catch (JMSException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                System.out.println("Message of another type was received");
            }
        }
    }
    

    Note that by default when using the @JmsListener annotation Spring Boot autoconfigures objects that are set to listen from a queue so if you want to subscribe to a topic you'll need to set this property in your application.properties file (or equivalent).

    spring.jms.pub-sub-domain=true
    

    Hope that helps!

  • alinaqvialinaqvi Member Posts: 17

    Can you please share the properties for kerberos authentication as well ?
    Also we are using solcache and queues as well in our app flow. Would the SB jms work with those as well ?

Sign In or Register to comment.