Search code examples
spring-bootspring-jms

Spring Boot JmsTemplate not taking into account replyTo in Message


I am invoking jmsTemplate according to the following code

    @Override
    public String sendAndReceive(String payload) {
        String retVal = "";
        try {
            final Message rspMessage = jmsTemplate.sendAndReceive(requestQueueName, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    final TextMessage rqstMessage = session.createTextMessage(payload);
                    final Destination replyTo = session.createQueue(responseQueueName);
                    rqstMessage.setJMSReplyTo(replyTo);
                    return rqstMessage;
                }
            });
            assert(rspMessage != null);
            retVal = ((TextMessage)rspMessage).getText();
        } catch (JMSException jmsException) {
            // TODO
        }
        return retVal;
    }

Unfortunately, the replyTo destination given in message is not taken into account. Instead the JmsTemplate creates a temporary response queue as given in JmsTemplate code:

    @Nullable
    protected Message doSendAndReceive(Session session, Destination destination, MessageCreator messageCreator)
            throws JMSException {

        Assert.notNull(messageCreator, "MessageCreator must not be null");
        TemporaryQueue responseQueue = null;
        MessageProducer producer = null;
        MessageConsumer consumer = null;
        try {
            Message requestMessage = messageCreator.createMessage(session);
            responseQueue = session.createTemporaryQueue();
            producer = session.createProducer(destination);
            consumer = session.createConsumer(responseQueue);
            requestMessage.setJMSReplyTo(responseQueue);
            if (logger.isDebugEnabled()) {
                logger.debug("Sending created message: " + requestMessage);
            }
            doSend(producer, requestMessage);
            return receiveFromConsumer(consumer, getReceiveTimeout());
        }
        finally {
            JmsUtils.closeMessageConsumer(consumer);
            JmsUtils.closeMessageProducer(producer);
            if (responseQueue != null) {
                responseQueue.delete();
            }
        }
    }

Is there a common way to have sendAndReceive accept tu use a given replyTo queue ? Or should I extend JmsTemplateand override the doSendAQndReceive function ?


Solution

  • The documentation for sendAndReceive is crystal clear:

    Send a message and receive the reply from the specified destination. The MessageCreator callback creates the message given a Session. A temporary queue is created as part of this operation and is set in the JMSReplyTO header of the message.

    You have to use send