消息发送后立即检索 JMS 标头而不消耗消息

use*_*345 2 spring activemq-classic jmstemplate spring-jms

如何在发送消息后检索 JMS 消息标头但不使用该消息?

这是我的消息发送代码

jmsTemplate.convertAndSend(que, text, message -> {

       LOGGER.info("setting JMS Message header values");    
       message.setStringProperty(RequestContext.HEADER_ID, id);
     //  LOGGER.info(message.getJMSMessageId()); -- this gives a null value here
       return message;
 });
Run Code Online (Sandbox Code Playgroud)

消息头仅在消息发布到队列后生成,因此在使用 MessagePostProcessor 时是否有一种简单的方法来检索 JMS 消息头?

我已经引用了链接 -这里这里,但没有太多帮助:(

Gar*_*ell 5

JmsMessageID在消息实际发送之前,您无法获取标头;后处理器只允许您在发送转换后的消息之前对其进行修改。

但是,您的第二个链接应该可以正常工作,因为它保存了对消息的引用,您可以稍后访问。

编辑

确认的:

@SpringBootApplication
public class So48001045Application {

    public static void main(String[] args) {
        SpringApplication.run(So48001045Application.class, args).close();
    }

    @Bean
    public ApplicationRunner runner(JmsTemplate template) {
        return args -> {
            final AtomicReference<Message> msg = new AtomicReference<>();
            template.convertAndSend("foo", "bar", m -> {
                msg.set(m);
                return m;
            });
            System.out.println(msg.get().getJMSMessageID());
        };
    }

}
Run Code Online (Sandbox Code Playgroud)

ID:host.local-61612-1514496441253-4:1:1:1:1
Run Code Online (Sandbox Code Playgroud)