相关文章推荐
追风的烤土司  ·  【Python】sqlalchemy ...·  7 月前    · 
含蓄的高山  ·  centos 编译python3.7.0 ...·  1 年前    · 
腼腆的椅子  ·  SQL Server ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

* Set an {@link org.springframework.amqp.rabbit.listener.RabbitListenerErrorHandler} * to invoke if the listener method throws an exception. * @return the error handler. * @since 2.0 String errorHandler() default "";

So, consider to go the custom RabbitListenerErrorHandler way instead.

I already have an error handler but only Exceptions are caught. AssertionError is an Error and is thus not included but just falls through the cracks and kills the thread. – Emanuel George Hategan Jan 18, 2018 at 15:50 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.

In the end I've customised SimpleRabbitListenerContainerFactory with an advice chain including such a MethodInterceptor. Namely factory.setAdviceChain(interceptor()). Thanks – Emanuel George Hategan Jan 23, 2018 at 15:52

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.