2

How can I retrieve JMS Message headers after sending the message but without consuming the message ?

this is my message sending code

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;
 });

the message headers get generated only after the message is posted to the queue so iis there a simple way to retrieve JMS message headers when using MessagePostProcessor?

I've referred the links - here and here but not much of help :(

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
user2340345
  • 793
  • 4
  • 16
  • 38

1 Answers1

5

You can't get the JmsMessageID header until the message is actually sent; the post processor just allows you to modify the converted message JUST BEFORE it is sent.

However, your second link should work ok, since it saves off a reference to the message which you can access later.

EDIT

Confirmed:

@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());
        };
    }

}

and

ID:host.local-61612-1514496441253-4:1:1:1:1
Gary Russell
  • 166,535
  • 14
  • 146
  • 179