Search code examples
springspring-bootspring-jmsmdc

spring jms - perform action before message received


Is it possible to perform an action before a jms message is received in spring boot? I know I could put it at the very top of my @JmsListener, but I have several listeners and I'd prefer avoiding adding a call to all of them.

I'm trying to use the logging MDC (a threadlocal, if you're not familiar with the MDC,) to track various things and I'd like to set some properties before beginning to process the message. I can do this on my controllers with a Filter, but does spring jms have the same concept?


Solution

  • I would try to start with an Before or Around (in case there should be some logic implemented after handling message as well) aspect:

    @Before("@annotation(JmsListener)")
    public void handle(ProceedingJoinPoint joinPoint) { ... }
    
    @Around("@annotation(JmsListener)")
    public void handle(ProceedingJoinPoint joinPoint) { ... }
    

    Couple of links: enabling aspectj support, before advice, around advice.