如何从Spring Listener获取Message Id

时间:2017-10-17 15:56:02

标签: java spring jms spring-jms

我有以下代码:

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

现在,我想获取jms消息ID。我知道我可以覆盖MappingJackson2MessageConverter类来执行此操作,但是可以以更简单的方式执行此操作,例如方法的第二个参数吗?

1 个答案:

答案 0 :(得分:2)

使用@Header注释:

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

}

结果:

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