What I want is to have an aspect around all methods annotated with @RabbitHandler so that AssertionErrors won't kill the handler thread.
I just want to wrap them inside RuntimeExceptions and rethrow.
Motivation: there is additional error handling that I want to use which works well except for these AssertionErrors.
I could add a try-catch for AssertionErrors in each method but there are too many places and instead I was thinking of using aspects.
@Aspect
public class RabbitAssertionErrorHandlerAspect {
@Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)")
public Object intercept(ProceedingJoinPoint pjp) throws Throwable {
try {
return pjp.proceed();
} catch (AssertionError e) {
throw new RuntimeException(e);
}
}
}
All nice and elegant but it doesn't get called. I assume this has something to do with the way these methods are discovered in the first place.
Any reasonable workarounds anyone sees?
It works with Spring AOP...
@SpringBootApplication
public class So48324210Application {
public static void main(String[] args) {
SpringApplication.run(So48324210Application.class, args);
}
@Bean
public MethodInterceptor interceptor() {
return i -> {
try {
System.out.println("here");
return i.proceed();
}
catch (AssertionError e) {
throw new RuntimeException(e);
}
};
}
@Bean
public static BeanNameAutoProxyCreator proxyCreator() {
BeanNameAutoProxyCreator pc = new BeanNameAutoProxyCreator();
pc.setBeanNames("foo");
pc.setInterceptorNames("interceptor");
return pc;
}
@Bean
public Foo foo() {
return new Foo();
}
public static class Foo {
@RabbitListener(queues = "one")
public void listen(Object in) {
System.out.println(in);
}
}
}
or, as Artem said, a custom error handler will work too.