Search code examples
jmsspring-jmssolace

Is there any possibility to read topic from JMS message as a consumer


Summary:

I have created a small Spring Boot application which should consume messages from a Solace instance. In Solace the publisher has maintained a queue which is subscribed to different topics. I, as consumer, am processing the messages provided in the queue. Depending on the original topic leading to a message in this queue I would like to react differently in my business logic. Means I need to somehow extract the topic of the message provided via the solace queue.

I have already have checked JMS header/properties, but I found nothing related to topic.

Anyone any idea or had a similar use case?

A workaround which came to my mind was to directly subscribe to all topics and create for every topic a method to consume and react accordingly, but then we would miss the queue features, or?


Solution

  • Actually the Destination and JMSDestination headers should contain the topic that the message was published to.

    For example, to test this real quick I created a StackOverflowQueue queue with a topic subscription to the this/is/a/topic topic. And upon publishing a message to this/is/a/topic my consumer, which was listening to the queue, got the topic info in the header.

    To quickly test I used the JMS sample here: https://github.com/SolaceSamples/solace-samples-jms/blob/master/src/main/java/com/solace/samples/QueueConsumer.java

    Awaiting message...
    Message received.
    Message Content:
    JMSDeliveryMode:                        2
    JMSDestination:                         Topic 'this/is/a/topic'
    JMSExpiration:                          0
    JMSPriority:                            0
    JMSDeliveryCount:                       1
    JMSTimestamp:                           1665667425784
    JMSProperties:                          {JMS_Solace_DeliverToOne:false,JMS_Solace_DeadMsgQueueEligible:false,JMS_Solace_ElidingEligible:false,Solace_JMS_Prop_IS_Reply_Message:false,JMS_Solace_SenderId:Try-Me-Pub/solclientjs/chrome-105.0.0-OSX-10.15.7/3410903749/0001,JMSXDeliveryCount:1}
    Destination:                            Topic 'this/is/a/topic'
    SenderId:                               Try-Me-Pub/solclientjs/chrome-105.0.0-OSX-10.15.7/3410903749/0001
    SendTimestamp:                          1665667425784 (Thu. Oct. 13 2022 09:23:45.784)
    Class Of Service:                       USER_COS_1
    DeliveryMode:                           PERSISTENT
    Message Id:                             1
    Replication Group Message ID:           rmid1:18874-bc0e45b2aa1-00000000-00000001
    Binary Attachment:                      len=12
      48 65 6c 6c 6f 20 77 6f    72 6c 64 21                Hello.world!
    

    Note that sample code doesn't use Spring Boot, but that shouldn't make a difference.