Andres
Andres

Reputation: 10707

How to get the Message Id from a Spring Listener

I have the following code:

@JmsListener(destination = "myQueue", containerFactory = "myFactory")
public void receiveMessage(MyClass message) {
    service.process(message);
}

Now, I want to obtain the jms message id. I know I can override the MappingJackson2MessageConverter class to do this, but is it possible to do it in a simpler way, like with a second argument on the method?

Upvotes: 1

Views: 2758

Answers (1)

Gary Russell
Gary Russell

Reputation: 174494

Use the @Header annotation:

@SpringBootApplication
public class So46794317Application {

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

    @Bean
    public ApplicationRunner runner(JmsTemplate template) {
        return args -> template.convertAndSend("foo", "bar");
    }

    @JmsListener(destination = "foo")
    public void receive(String in, @Header(JmsHeaders.MESSAGE_ID) String messageId) {
        System.out.println(in + ", id:" + messageId);
    }

}

Result:

bar, id:ID:host.local-50513-1508260336349-4:2:1:1:1

Upvotes: 6

Related Questions