简体   繁体   中英

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

Programmatically configuring fault tolerance for a chunk works kind of as follows:

stepBuilders.get("step")
  .<Partner,Partner>chunk(1)
  .reader(reader())
  .processor(processor())
  .writer(writer())
  .listener(logProcessListener())
  .faultTolerant()
  .skipLimit(10)
  .skip(UnknownGenderException.class)
  .listener(logSkipListener())
  .build();

The trick is, that with adding "chunk", the chain switches to a SimpleStepBuilder which offers the "faultTolerant" method.

My question is how to do that if you just have a tasklet (no reader, processor, writer)?

Defining a tasklet works as follows:

stepBuilders.get("step")
  .tasklet(tasklet())
  .build();

The usage of "tasklet" switches to a TaskletStepBuilder which does not offer a "faultTolerant" method. Therefore I see no way how to define properties like skipLimit and that like.

Any ideas?

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;
          }
        }

To answer your Question, no there is no support :-(

I really like to have the Spring Batch retry support for Tasklet too. And to be honest, i don't see why they implement it. Calling a unstable System from a Tasklet is a valid use case. I don't want to use the @Retryable as the Spring Batch support for retry is more clearer. So i came up with this solution:

  @Bean
    public Step setInboxMessageStatusToReceivedStep(OneItemReader oneItemReader, SetInboxMessageStatusToReceivedItemWriter setInboxMessageStatusToReceivedItemWriter) {
        return this.stepBuilders.get("setInboxMessageStatusToReceivedStep")
                .<String, String>chunk(1)
                .reader(oneItemReader)
                .writer(setInboxMessageStatusToReceivedItemWriter)
                .faultTolerant()
                .backOffPolicy(milliSecondsBetweenRetiesPolicy(this.milliSecondsBetweenReties))
                .retryLimit(this.retryLimit)
                .retry(Throwable.class)
                .build();
    }

/**
 * Spring Batch has some cool things like retry or backOffPolicy. These things are only available for ItemReader + ItemWriter and not for Tasklets.
 * This is a nice Hack to use these Spring Batch features for Tasklets like thinks wrapped into a ItemReader + ItemWriter.
 */
public class OneItemReader implements ItemReader<String> {

    public static final String ONE_ITEM_READER_EXECUTION_CONTEXT_KEY = "ONE_ITEM_READER_EXECUTION_CONTEXT_KEY";
    public static final String JOB_SUCCESSFUL_FINISHED = "ONE_ITEM_READER_EXECUTION_CONTEXT_VALUE_JOB_SUCCESSFUL_FINISHED";
    private StepExecution stepExecution;

    @BeforeStep
    public void beforeStep(StepExecution stepExecution) {
        this.stepExecution = stepExecution;
    }

    @Override
    public String read() {
        String isFirstTimeRead = (String) this.stepExecution.getExecutionContext().get(ONE_ITEM_READER_EXECUTION_CONTEXT_KEY);
        if (isFirstTimeRead == null) {
            return "dummy value just to ensure the writer is invoked";
        } else {
            // null will stop the reading
            return null;
        }
    }

    /**
     * As the last action in a Step, this method must be called. It tells the OneItemReader to stop returning Items.
     */
    public static void markDataAsRead(StepExecution stepExecution) {
        stepExecution.getExecutionContext().put(ONE_ITEM_READER_EXECUTION_CONTEXT_KEY, JOB_SUCCESSFUL_FINISHED);
    }
}

Your ItemWriter can now work just like a Tasklet. It is only once called.

Note, that you have to call markDataAsRead(this.stepExecution); at the in for your ItemWriter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM