[英]Get message ID from producer.send(message)
How can I get MessageId after sending the message but without consuming the message? 如何在发送消息后获取MessageId但不消费消息?
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: 但是我需要在发送消息后找出id,但是没有消费消息,我写了这个方法:
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? 如何重写一个方法,以便你可以在另一个类中调用它并获取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 但得到错误:lambda转换的目标类型必须是一个接口
Why are you using a lambda there? 你为什么在那里使用lambda? What is
t
? 什么是
t
? A String
is not a Consumer<?>
. String
不是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. 是你所需要的全部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.