简体   繁体   中英

Get message ID from producer.send(message)

How can I get MessageId after sending the message but without consuming the message?

I have this method which send the message to queue

public void sentMessage(JmsTemplate jmsTemplate, String queueName, String message, String uuid) {
        jmsTemplate.convertAndSend(queueName, message,
                new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws JMSException {
                        message.setStringProperty("uuid", uuid);
                        log.info("CorId:{}", message.getJMSCorrelationID());
                        return message;
                    }
                });
    }

but I needed to find out the id after sending the message, but without consuming the message and I wrote this method:

public ApplicationRunner sentMessage(JmsTemplate jmsTemplate, String queueName, String message, String uuid) {
        return args -> {
            final AtomicReference<Message> msg = new AtomicReference<>();
            jmsTemplate.convertAndSend(queueName, message, m -> {
                msg.set(m);
                return m;
            });
            String message1 = msg.get().getJMSCorrelationID();
            log.info("CorId:{}", message1);
        };
    }

how to rewrite a method so that you can call it in another class and get messageId?

I write this

public String sentMessage(JmsTemplate jmsTemplate, String queueName, String message, String uuid) throws JMSException {
        return t -> {
            final AtomicReference<Message> msg = new AtomicReference<>();
            jmsTemplate.convertAndSend(queueName, message, m -> {
                msg.set(m);
                return m;
            });
            return msg.get().getJMSCorrelationID();
        };
    }

but get error: target type of lambda conversion must be an interface

Why are you using a lambda there? What is t ? A String is not a Consumer<?> .

public String sentMessage(JmsTemplate jmsTemplate, String queueName, String message, String uuid)
        throws JMSException {

    final AtomicReference<Message> msg = new AtomicReference<>();
    jmsTemplate.convertAndSend(queueName, message, m -> {
        msg.set(m);
        return m;
    });
    return msg.get().getJMSCorrelationID();
}

is all you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM