Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 使用@RabbitHandler注释的方法周围的方面_Spring_Aspectj_Spring Amqp_Spring Rabbit - Fatal编程技术网

Spring 使用@RabbitHandler注释的方法周围的方面

Spring 使用@RabbitHandler注释的方法周围的方面,spring,aspectj,spring-amqp,spring-rabbit,Spring,Aspectj,Spring Amqp,Spring Rabbit,我想要的是有一个关于所有方法的方面,用@RabbitHandler注释,这样AssertionErrors就不会杀死处理程序线程 我只想将它们包装在RuntimeExceptions中并重新播放 动机:除了这些断言错误之外,我还想使用其他的错误处理方法 我可以在每个方法中为断言错误添加一个try-catch,但是有太多的地方,相反,我考虑使用方面 @Aspect public class RabbitAssertionErrorHandlerAspect { @Around("@annot

我想要的是有一个关于所有方法的方面,用@RabbitHandler注释,这样AssertionErrors就不会杀死处理程序线程

我只想将它们包装在RuntimeExceptions中并重新播放

动机:除了这些断言错误之外,我还想使用其他的错误处理方法

我可以在每个方法中为断言错误添加一个try-catch,但是有太多的地方,相反,我考虑使用方面

@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);
    }
  }
}
一切都很好,很优雅,但没有人叫它。我认为这与这些方法最初被发现的方式有关


有人看到合理的解决办法吗

类上带有
@RabbitHandler
@RabbitListener
可以配置为:

/**
 * 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 "";

>,请考虑使用习惯>代码> RabeListEnrError处理程序方式。

< p>它与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);
        }

    }

}

或者,正如Artem所说,自定义错误处理程序也可以工作。

我已经有了一个错误处理程序,但只捕获异常。AssertionError是一个错误,因此不包括在内,只是从裂缝中掉落并终止线程。最后,我定制了一个包含这样一个
方法拦截器的建议链
SimpleRableBitListenerContainerFactory
。即
factory.setAdviceChain(interceptor())
。谢谢