Spring Batch从上一个异常重启

时间:2015-10-03 14:42:14

标签: spring-batch

我希望在终止后重新启动批处理 当特定异常抛出时,我会停止批处理:

public class IntegrationItemProcessorExceptionHandler implements ExceptionHandler {

    private static final Logger LOG = LoggerFactory.getLogger(IntegrationItemProcessorExceptionHandler.class);

    @Override
    public void handleException(RepeatContext context, Throwable throwable) throws Throwable {
        LOG.error("handleException", throwable);
        if (throwable instanceof CustomResponseException) {
            context.setTerminateOnly();
        }
    }   
}

输入是我用FlatFileItemReader

读取的json
@Bean
public ItemReader<UserDto> reader() {
    FlatFileItemReader<UserDto> reader = new FlatFileItemReader<MerchantDTO>();
    reader.setResource(new ClassPathResource("user.json"));
    reader.setRecordSeparatorPolicy(new CustomJsonRecordSeparatorPolicy());
    reader.setLineMapper(new CustomLineMapper());
    return reader;  
}

如果ItemProcessor抛出CustomResponseException,我会停止批处理。 之后,我想重新启动批处理,但在同一行我停止了它。

我需要具备这种行为吗?

我的工作配置:

@Bean
public Job importUserJob(JobBuilderFactory jobs, Step s1) {
    return jobs.get("integrationJob")
            .incrementer(new RunIdIncrementer())
            .flow(s1)
            .end()
            .build();
}

@Bean
public Step step1(StepBuilderFactory stepBuilderFactory) {
    return stepBuilderFactory.get("step1")
            .<UserDTO, User>chunk(10)
            .reader(reader())
            .processor(processor())
            .writer(writer())
            .listener(new IntegrationItemProcessorListener())
            .exceptionHandler(new IntegrationItemProcessorExceptionHandler())
            .build();
}

0 个答案:

没有答案