0

I am developing a Spring JMS based client, which connects to an IBM MQ.

This part is working fine !!

Scenario:

The MQ client can get a request from multiple Spring "@Components". Considering, JMSTemplate's "send" and "receive" are completely different methods -

Question:

How do we co-relate the response from component-one to only its request ? e.g.

A sends request-A

B sends request-B

How will SpringListener know to respond the response-A to A, and response-B to B ?

Does spring provide an out of box functionality to handle such a scenario ?

Thanks in advance !!

Sample Listener :

public class MessageReceiver implements MessageListener {

    @Override
    public void onMessage(Message message) {

        if(message instanceof TextMessage){
            System.out.println(message.toString());
        }
    }
}

Sample Sender :

public class App 
{
    public static void main( String[] args )
    {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        JmsTemplate jmsTemplate = (JmsTemplate)context.getBean("jmsTemplate");

        jmsTemplate.send(s -> s.createTextMessage("TestingAMQ"));
    }
}
Deep
  • 673
  • 1
  • 10
  • 23

2 Answers2

1

based on the JMSReplyTo header you can do it with JmsTemplate.sendAndReceive for the sender

and in the listener side by

    public class MessageReceiver implements MessageListener {

        @Override
        public void onMessage(Message message) {
            jmsTemplate.send(message.getJMSReplyTo(), s -> s.createTextMessage("TestingAMQ"));
            if(message instanceof TextMessage){
                System.out.println(message.toString());
            }
        }
   }

OR

By setting a Message property in the sender side by Message.setStringProperty and use selectors in the listener or conditional response based on the StringProperty.

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jms/core/JmsTemplate.html#sendAndReceive-org.springframework.jms.core.MessageCreator-

Hassen Bennour
  • 3,885
  • 2
  • 12
  • 20
  • The method "sendAndReceive" creates a temporary Queue to respond ( per the jmsTemplate documentation ). "Message.setStringProperty" seems like a good idea ... but wouldn't the method have to be synchronized ? Unless the method is synchronized - a new thread might change the 'property' value in the method. – Deep Nov 14 '17 at 13:10
  • Another option could be to make - send + receive transactional. Although, I am not sure how this might be implemented. – Deep Nov 14 '17 at 13:13
  • Send a message and receive the reply from the specified destination.( per the jmsTemplate documentation ) since you pass it as a parameter to the sendAndReceive method, destination can be of any type and created before – Hassen Bennour Nov 14 '17 at 14:54
  • why a new thread might change the 'property' value in the method ? your message is created and sent by only one thread – Hassen Bennour Nov 14 '17 at 14:56
  • Thanks @Hassen Bennour .... I found some a link with the 'setProperty' approach you suggested ! – Deep Nov 15 '17 at 04:04
  • you are welcome, do not forget to check to remove it from unanswered queue – Hassen Bennour Nov 16 '17 at 12:17
0

Found this to be the approach of 'setProperty' -

https://codedependents.com/2010/03/04/synchronous-request-response-with-activemq-and-spring/

Deep
  • 673
  • 1
  • 10
  • 23