Spring Batch 中 SkipPolicy 接口的自定义实现

huangapple go评论55阅读模式
英文:

Own implementation of SkipPolicy interface in spring batch

问题

我尝试给RetryPolicy接口的自定义实现。以下是我的代码。我能够实现canRetry方法,但不确定如何实现open、close和registerThrowable方法。

每当我运行我的代码时,重试会无限次发生。流程进入open方法,返回类型为null。我认为这是导致RetryPlolicy不按预期工作的地方。

@Slf4j
public class RecordRetryPolicy implements RetryPolicy {

  public static final int MAX_RETRY_COUNT = 3;

  @Override
  public boolean canRetry(RetryContext retryContext) {
    Throwable t = retryContext.getLastThrowable();
    if ((t instanceof BillingException || t instanceof InternalServerException) && retryContext.getRetryCount() <= MAX_RETRY_COUNT) {
      log.warn("1st block in canRetry");
      return true;
    } else if (t instanceof InternalServerException && retryContext.getRetryCount() <= MAX_RETRY_COUNT) {
      log.warn("2nd block in canRetry");
      return true;
    } else {
      log.warn("3rd block in canRetry");
      return false;
    }
  }

  @Override
  public RetryContext open(RetryContext retryContext) {
    //return retryContext.getParent();
    return null;
  }

  @Override
  public void close(RetryContext retryContext) {

  }

  @Override
  public void registerThrowable(RetryContext retryContext, Throwable throwable) {

  }
}

我的步骤定义如下:

@Bean
public Step InfoStep(JpaTransactionManager transactionManager) {
  ExponentialBackOffPolicy exponentialBackOffPolicy = new ExponentialBackOffPolicy();
  exponentialBackOffPolicy.setInitialInterval(1000);
  exponentialBackOffPolicy.setMaxInterval(16000);
  exponentialBackOffPolicy.setMultiplier(4.0);

  return new StepBuilder("read-step", jobRepository)
      .<ShopInfo, ShopInfo>chunk(10, transactionManager)
      .reader(dataInfoReader())
      .processor(dataInfoProcessor())
      .writer(dataInfoWriter())
      .taskExecutor(dataTaskExecutor())
      .faultTolerant()
      .retryPolicy(retryPolicy())
      .backOffPolicy(exponentialBackOffPolicy)
      .skipPolicy(skipPolicy())
      .build();
}

@Bean
public RecordSkipPolicy skipPolicy(){
  return new RecordSkipPolicy();
}

@Bean
public RecordRetryPolicy retryPolicy(){
  return new RecordRetryPolicy();
}

对于open、close和registerThrowable方法,应该给出什么样的实现?

英文:

I am trying to give own implementation of RetryPolicy interface. Below is my code. I am able to give implementation of canRetry method but not sure about open, close & registerThrowable method. How to give their implementation.

When ever I am running my code the retry happens for infinite number of times. flow goes to open method where return type is null. Which is where i think is causing. So my RetryPlolicy is not working as expected.

@Slf4j
public class RecordRetryPolicy implements RetryPolicy {

  public static final int MAX_RETRY_COUNT = 3;


  @Override
  public boolean canRetry(RetryContext retryContext) {
    Throwable t = retryContext.getLastThrowable();
    if ((t instanceof BillingException||t instanceof InternalServerException) &amp;&amp; retryContext.getRetryCount() &lt;= MAX_RETRY_COUNT) {
      log.warn(&quot;1st block in canRetry&quot;);
      return true;
    } else  if (t instanceof InternalServerException &amp;&amp; retryContext.getRetryCount() &lt;= MAX_RETRY_COUNT){
      log.warn(&quot;2nd block in canRetry&quot;);
      return true;
    } else {
      log.warn(&quot;3rd block in canRetry&quot;);
      return false;
    }
  }

  @Override
  public RetryContext open(RetryContext retryContext) {
    //return retryContext.getParent();
           return null;
  }

  @Override
  public void close(RetryContext retryContext) {

  }

  @Override
  public void registerThrowable(RetryContext retryContext, Throwable throwable) {

  }
}

My Step is defines as

 @Bean
  public Step InfoStep(JpaTransactionManager transactionManager) {
    ExponentialBackOffPolicy exponentialBackOffPolicy = new ExponentialBackOffPolicy();
    exponentialBackOffPolicy.setInitialInterval(1000);
    exponentialBackOffPolicy.setMaxInterval(16000);
    exponentialBackOffPolicy.setMultiplier(4.0);

    return new StepBuilder(&quot;read-step&quot;, jobRepository)
        .&lt;ShopInfo, ShopInfo&gt;chunk(10, transactionManager)
        .reader(dataInfoReader())
        .processor(dataInfoProcessor())
        .writer(dataInfoWriter())
        .taskExecutor(dataTaskExecutor())
        .faultTolerant()
        .retryPolicy(retryPolicy())
        .backOffPolicy(exponentialBackOffPolicy)
        .skipPolicy(skipPolicy())
        .build();
  }

@Bean
  public RecordSkipPolicy skipPolicy(){
    return new RecordSkipPolicy();
  }

  @Bean
  public RecordRetryPolicy retryPolicy(){
    return new RecordRetryPolicy();
  }

What implementation should be give for open, close & registerThrowable method.

答案1

得分: 1

open/close 用于获取/释放策略所需的任何资源。registerThrowable 用于在重试上下文中注册异常,以便在后续的重试尝试中决定如何处理。

Spring Retry 提供了多个 RetryPolicy 的实现(请参阅此),你可以从中获取灵感。

英文:

> not sure about open, close & registerThrowable method. How to give their implementation.

open/close are for acquiring/releasing any resources needed by the policy . registerThrowable is used to register an exception in the retry context so it can be used to decide what to do in the subsequent retry attempt.

Spring Retry provides several implementations of RetryPolicy (see this package) from which you can get inspiration.

huangapple
  • 本文由 发表于 2023年6月8日 10:04:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定