We implemented a distributed request / response type architecture for a particular use case where we want to wait for the response. The JMS broker we use is ActiveMq and the code is wired together using Spring .
The issue we see is that it appears that if sending a bunch of requests to the same destination, any request that, say, takes a significant amount of time to complete, blocks the request messages that follow it. The SessionAwareMessageListener interface that the consumer uses only supports the onMessage() method. What is the best way to achieve parallelism here i.e. if a particular request takes a long time, the other messages in the queue should not be blocked?
There is this SO post but it doesn't answer my question. JMS: Can we get multiple messages from queue in OnMessage() withtout commit or rollback
Thanks
Relevant snippets of code (exception handling etc removed for brevity)
Producer
public class MyJmsProducer {
private ProcessingResponse sendMessage(final Serializable serializable) {
//send JMS request and wait for response
return jmsMessagingTemplate.convertSendAndReceive(destination, serializable, ProcessingResponse.class); //this operation seems to be blocking + sync
}
}
And the listener (consumer)
public class MyJmsListener
implements SessionAwareMessageListener<Message>, NotificationHandler<Task> {
@Override
public void onMessage(Message message, Session session)
throws JMSException {
ProcessingRequest processingRequest = (ProcessingRequest) ((ObjectMessage) message).getObject();
// handle the request here (THIS COULD TAKE A WHILE)
handleRequest(processingRequest);
// done handling the request, now create a response message
final ObjectMessage responseMessage = new ActiveMQObjectMessage();
responseMessage.setJMSCorrelationID(message.getJMSCorrelationID());
responseMessage.setObject(processingResponse);
// Message sent back to the replyTo address of the income message.
final MessageProducer producer = session.createProducer(message.getJMSReplyTo());
producer.send(responseMessage);
}
}
producer.send(responseMessage);
? Are you thinking that producer producing request and sending it to broker wait for getting it consumed by consumer and getting completed? – hagrawal