Search code examples
javaspringrabbitmqspring-amqp

How to get Spring AMQP Message from Advice


I have 2 services Producer and Consumer. In the service Consumer I have multiple places using SecurityContextHolder to get the current user. Then I want to pass a User Id from Producer to Consumer when sending a message.

I follow the answer here: https://stackoverflow.com/a/49957948/6484502 but I don't know how to get the Message from Advice?

Could you please provide an example of that Advice?


Solution

  • You need to implement a org.aopalliance.intercept.MethodInterceptor. The AMQP message is a second argument of the MethodInvocation.getArguments(). You cast it into a org.springframework.amqp.core.Message, take your userId and call ctx.setAuthentication() before invocation.proceed().