0

I want to apply filters to log only for rabbit events using MDC properties. And set trace Id && correlation id from the event header.

I already have a RequestResponseLoggingFilter which is used for setting the tenant Id.

I am not sure how to trigger this filter only for async rabbit events.

rakesh
  • 3
  • 2

1 Answers1

0

If you are using a MessageListener or @RabbitListener you can add a MessagePostProcessor to the listener container (or listener container factory, respectively) in the afterReceivePostProcessors property.

The post processor(s) are called after a message is received and before the listener is called.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • That's great for setting, but what about clearing? MDC.clear() afterwards is important too to prevent leaking data between requests – mjaggard Dec 08 '21 at 22:26
  • You can add an around advice to the container's advice chain. – Gary Russell Dec 09 '21 at 02:13
  • Thanks @Gary Russell but I'm not totally clear. Where should I add it and can I add it to every listener or do I need to add an AOP annotation to every method that listens for messages? – mjaggard Dec 10 '21 at 12:41
  • No; it goes on the container factory - see https://stackoverflow.com/questions/70306081/how-to-add-an-around-advice-to-rabbitlistener/70306082#70306082 – Gary Russell Dec 10 '21 at 14:35