How to configure fault tolerance programmatically for a spring tasklet (not a chunk)

杀马特。学长 韩版系。学妹 提交于 2019-12-04 07:12:54

A Tasklet has no notion of "items" to skip, so fault tolerance only makes sense for a chunk-oriented step. I suggest you use Spring Retry in the raw (1.1.0.RELEASE is out now, just, and you have some fluent builder options and a @Retryable annotation there).

Based on the guidance given by @DaveSyer and using org.springframework.retry:spring-retry:1.1.0.RELEASE this is what I have ended up with:

Tasklet tasklet = // whatever
ProxyFactory proxyFactory = new ProxyFactory(Tasklet, new SingletonTargetSource(tasklet));
proxyFactory.addAdvice(RetryInterceptorBuilder.stateless()
                                              .maxAttempts(3)
                                              .build());
Step step = stepBuilderFactory.get("taskName")
                              .tasklet((Tasklet)proxyFactory.proxy)
                              .build();

The only thing I'm struggling with still is if I want to swallow the exception that causes the retry after maxAttempts is exceeded. If I add an ExceptionHandler to the step then the step is never retried. I guess that means the exception handler is inside the pointcut which I find a little surprising.

//you need to have a bean in order to define the retry policies        
            @Configuration
            @EnableAutoConfiguration
            public class MyRetryBean{

              @Autowired
              private RetryProperties properties;
              private RetryTemplate retryTemplate;

              @Bean
              public RetryTemplate initializeRetryTemplate() {
                Map<Class<? extends Throwable>, Boolean> retryExceptions = Collections.singletonMap(IOException.class,
                      Boolean.TRUE);
                SimpleRetryPolicy policy = new SimpleRetryPolicy(properties.getMaxAttempt(), retryExceptions);
                retryTemplate = new RetryTemplate();
                retryTemplate.setRetryPolicy(policy);
                FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
                backOffPolicy.setBackOffPeriod(properties.getBackoffPeriod());
                retryTemplate.setBackOffPolicy(backOffPolicy);
                return retryTemplate;
              }

              public RetryTemplate getRetryTemplate() {
                return retryTemplate;
              }
            }

// in spring batch steps config

            @Autowire
            private MyRetryBean retryBean;

            stepBuilders.get("step")
              .tasklet(new MyTasklet(retryBean))
              .build();

// in your tasklet

        public class MyTasklet implements Tasklet{

          private MyRetryBean retry;

          public MyTasklet (MyRetryBean aRetry) {
            this.retry= aRetry;
          }

          @Override
          public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws IOException {

            retry.getRetryTemplate().execute(new RetryCallback<RepeatStatus, IOException>() {
              @Override
              public RepeatStatus doWithRetry(RetryContext context) throws IOException {
                throw new IOException();
              }
            });
            return null;
          }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!