How to read x-death header of a RabbitMQ dead-lettered message using Spring Boot? -
i trying implement re-routing of dead-lettered messages described in answer. using spring config. have no idea on how read headers original routing key , original queue. following config:
@configuration public class notifenginerabbitmqconfig { @bean public messagehandler handler(){ return new messagehandler(); } @bean public jackson2jsonmessageconverter messageconverter(){ return new jackson2jsonmessageconverter(); } @bean public messagelisteneradapter messagelisteneradapter(){ return new messagelisteneradapter(handler(), messageconverter()); } /** * listens incoming messages * allows multiple queue listen * */ @bean public simplemessagelistenercontainer simplemessagelistenercontainer(){ simplemessagelistenercontainer container = new simplemessagelistenercontainer(); container.addqueuenames(queue_to_listen_to.split(",")); container.setmessagelistener(messagelisteneradapter()); container.setconnectionfactory(rabbitconnectionfactory()); container.setdefaultrequeuerejected(false); return container; } @bean public connectionfactory rabbitconnectionfactory(){ cachingconnectionfactory factory = new cachingconnectionfactory(host); factory.setusername(username); factory.setpassword(password); return factory; } }
the headers not available using "old" style pojo messaging (with messagelisteneradapter
). need implement messagelistener
gives access headers.
however, need invoke converter in case and, if using request/reply messaging, lose reply mechanism within adapter , have send reply yourself.
alternatively, can use custom message converter , "enhance" converted object header after invoking standard converter.
consider instead using newer style pojo messaging @rabbitlistener
- gives access headers , has request/reply capability.
here's example:
@springbootapplication public class so37581560application { public static void main(string[] args) { springapplication.run(so37581560application.class, args); } @bean public foolistener foolistener() { return new foolistener(); } public static class foolistener { @rabbitlistener(queues="foo") public void pojolistener(string body, @header(required = false, name = "x-death") list<string> xdeath) { system.out.println(body + ":" + (xdeath == null ? "" : xdeath)); } } }
result:
foo:[{reason=expired, count=1, exchange=, time=thu jun 02 08:44:19 edt 2016, routing-keys=[bar], queue=bar}]