D
D
Denis Kuznetsov2021-07-16 13:28:30
Spring
Denis Kuznetsov, 2021-07-16 13:28:30

How to implement ReplyToQueue?

Hello, I have application 1 and it has a method

@PostMapping
    public ResponseEntity<String> createOrder(@RequestBody OrderRequest order) throws JMSException {
        log.info("### 1 ### Order Service sending order message '{}' to the queue", order.getMessage());

        MQQueue orderRequestQueue = new MQQueue("DEV.QUEUE.1");
        jmsTemplate.convertAndSend(orderRequestQueue, order.getMessage(), textMessage -> {
            textMessage.setJMSCorrelationID(order.getIdentifier());
            return textMessage;
        });
        return new ResponseEntity(order, HttpStatus.ACCEPTED);
    }

which sends a request to the queue
and there is application 2, which, if I understand correctly, listens to this queue and sends a response to it
@JmsListener(destination = "DEV.QUEUE.1")
    public void receive(Message message) throws JMSException {
        TextMessage textMessage = (TextMessage) message;
        final String textMessageBody = textMessage.getText();
        log.info("### 2 ### Payment Service received message: {} with correlationId: {}",
                textMessageBody,
                textMessage.getJMSCorrelationID());
        // some random logic to complete the order (80% of times it returns true)
        Random random = new Random(); //message to convertAndSend
        String orderCompleted = (random.nextInt(101) >= 20) ? "payment_ok" : "payment_failed";
        // send response
        log.info("### 3 ### Payment Service sending response");
        MQQueue mqQueue = new MQQueue("DEV.QUEUE.1");
        jmsTemplate.convertAndSend(mqQueue, orderCompleted, responseMessage -> {
            responseMessage.setJMSCorrelationID(textMessage.getJMSCorrelationID());
            return responseMessage;
        });
    }

If I understand correctly, then the CorrelationId to CorrelationId principle is implemented here.
How to change the receive method under the principle of replyToQueue, tried to google, but not very successfully?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-07-16
@DennisKingsman

It's all written here:

MQQueue mqQueue = new MQQueue("DEV.QUEUE.1");
        jmsTemplate.convertAndSend(mqQueue, orderCompleted, responseMessage -> {
            responseMessage.setJMSCorrelationID(textMessage.getJMSCorrelationID());
            return responseMessage;
        });

There is a queue announcement. Declare it the way you want.
If you don't need CorrelationID, don't set it.
replyTo - can also be obtained from the original message:
Destination destination = textMessage.getJMSReplyTo();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question