Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.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
Java 将侦听器注册到spring的注释方式';s@Retryable_Java_Spring Retry - Fatal编程技术网

Java 将侦听器注册到spring的注释方式';s@Retryable

Java 将侦听器注册到spring的注释方式';s@Retryable,java,spring-retry,Java,Spring Retry,如何使用@Retryable()注释在spring retry中注册侦听器 @Bean public RetryListener myRetryListener() { return new MyRetryListener(); } @Service public class SampleService { private int cnt = 1; @Retryable(RuntimeException.class) public void retryMethod(

如何使用
@Retryable()
注释在
spring retry
中注册侦听器

@Bean
public RetryListener myRetryListener() {
    return new MyRetryListener();
}

@Service
public class SampleService {
   private int cnt = 1;

   @Retryable(RuntimeException.class)
   public void retryMethod(int x) throws Exception {
      System.out.println("SampleService invoked.");
      Thread.sleep(500);

      if (cnt++ < 4) {
        throw new RuntimeException("SampleService throwing exception");
      }
   }

}
@Bean
公共RetryListener myRetryListener(){
返回新的MyRetryListener();
}
@服务
公共类示例服务{
私有int cnt=1;
@可重试(RuntimeException.class)
公共void retryMethod(int x)引发异常{
System.out.println(“调用了SampleService”);
睡眠(500);
if(cnt++<4){
抛出新的RuntimeException(“SampleService抛出异常”);
}
}
}

如果我创建任何侦听器bean,它会自动注册到
RetryTemplate
。因此,如果我有多个侦听器,那么我如何让特定的侦听器侦听我的可重试服务呢?

在spring retry的官方Github repo中有一个非常古老的pull请求,它似乎可以实现您想要做的事情


我想您正在寻找下面这样的产品
Retryable
注释具有侦听器参数(
spring重试:1.2.4.RELEASE


我希望这有帮助。

您可以像这样在
@Retryable
中注册
侦听器。侦听器属性接受字符串数组。字符串表示侦听器类名

@Retryable(maxAttempts = 5,backoff = @Backoff(delay = 5000),listeners = {"retryListener"})
public void getSomeData() {
                log.info("Get Some Date !!!!");
                throw new RuntimeException("Test Exception");
            } 


@Component
public class RetryListener extends RetryListenerSupport {

    @Override
    public <T, E extends Throwable> void close(RetryContext context,
                                               RetryCallback<T, E> callback, Throwable throwable) {

        log.error("Unable to recover from  Exception");
        log.error("Error ", throwable);
        super.close(context, callback, throwable);
    }

    @Override
    public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
        log.error("Exception Occurred, Retry Count {} ", context.getRetryCount());
        super.onError(context, callback, throwable);
    }

    @Override
    public <T, E extends Throwable> boolean open(RetryContext context,
                                                 RetryCallback<T, E> callback) {
        log.error("Exception Occurred, Retry Session Started ");
        return super.open(context, callback);
    }
}
@Retryable(maxturents=5,backoff=@backoff(delay=5000),侦听器={“retryListener”})
public void getSomeData(){
log.info(“获得一些日期!!!!”);
抛出新的运行时异常(“测试异常”);
} 
@组成部分
公共类RetryListener扩展了RetryListener支持{
@凌驾
公共无效关闭(RetryContext上下文,
RetryCallback回调,Throwable-Throwable){
日志错误(“无法从异常中恢复”);
日志错误(“错误”,可丢弃);
super.close(上下文、回调、可丢弃);
}
@凌驾
public void onError(RetryContext上下文、RetryCallback回调、Throwable Throwable){
错误(“发生异常,重试计数{}”,context.getRetryCount());
super.onError(上下文、回调、可丢弃);
}
@凌驾
公共布尔打开(RetryContext上下文,
RetryCallback(回调){
log.error(“发生异常,重试会话已启动”);
返回super.open(上下文,回调);
}
}

那么它不应该是RetryListener而不是RetryListener吗?
public class NetworkcallRetryLogListener extends RetryListenerSupport {
private static final Logger LOG = LoggerFactory.getLogger(NetworkcallRetryLogListener.class);

@Override
public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
    LOG.warn("Retry attempt {} for retryable method {} threw exception {}", context.getRetryCount(),
            context.getAttribute("context.name"), ExceptionUtils.getStackTrace(throwable));
    super.onError(context, callback, throwable);
    }
}
@Retryable(maxAttempts = 5,backoff = @Backoff(delay = 5000),listeners = {"retryListener"})
public void getSomeData() {
                log.info("Get Some Date !!!!");
                throw new RuntimeException("Test Exception");
            } 


@Component
public class RetryListener extends RetryListenerSupport {

    @Override
    public <T, E extends Throwable> void close(RetryContext context,
                                               RetryCallback<T, E> callback, Throwable throwable) {

        log.error("Unable to recover from  Exception");
        log.error("Error ", throwable);
        super.close(context, callback, throwable);
    }

    @Override
    public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
        log.error("Exception Occurred, Retry Count {} ", context.getRetryCount());
        super.onError(context, callback, throwable);
    }

    @Override
    public <T, E extends Throwable> boolean open(RetryContext context,
                                                 RetryCallback<T, E> callback) {
        log.error("Exception Occurred, Retry Session Started ");
        return super.open(context, callback);
    }
}