Problem: I am migrating from the MessageListener interface interface to @RabbitListener. I had this logic when I was processing the messages "pre" and "post" on a MessageListener inherited from several classes
example:
public AbstractMessageListener implements MessageListener { @Override public void onMessage(Message message) {
Question: Is there a way I can achieve something like this using the @RabbitListener annotation. Where can I inherit the processing logic before / after the message without having to re-implement or invoke the processing of the message before / after the message inside each @RabbitListener child annotation and all the while supporting custom method signatures for the @RabbitListener child? Or is it too greedy?
An example of the desired result:
public class SomeRabbitListenerClass { @RabbitListener( id = "listener.mypojo",queues = "${rabbitmq.some.queue}") public void listen(@Valid MyPojo myPojo) { //... } } public class SomeOtherRabbitListenerClass { @RabbitListener(id = "listener.orders",queues ="${rabbitmq.some.other.queue}") public void listen(Order order, @Header("order_type") String orderType) { //... } }
for both of these @RabbitListener (s) using the same inherited pre / post message processing
I see that the @RabbitListener annotation has an argument of 'containerFactory', but I already declare it in config ... and I'm really sure how to achieve the desired inheritance with customFactoryFactory.
Updated answer: This is what I ended up doing.
Tip:
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.springframework.amqp.core.Message; public class RabbitListenerAroundAdvice implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { Message message = (Message) invocation.getArguments()[1]; before(message) Object result = invocation.proceed(); after(message); return result; }
declare beans: In the rabbitmq configuration, declare the advice as a Spring bean and pass it to rabbitListenerContainerFactory # setAdviceChain (...)
//... @Bean public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() { SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); factory.setConnectionFactory( cachingConnectionFactory() ); factory.setTaskExecutor(threadPoolTaskExecutor()); factory.setMessageConverter(jackson2JsonMessageConverter()); factory.setAdviceChain(rabbitListenerAroundAdvice()); return factory; } @Bean public RabbitListenerAroundAdvice rabbitListenerAroundAdvice() { return new RabbitListenerAroundAdvice(); } // ...
spring-amqp spring-rabbit
Selwyn
source share