在Tasklet中退出Spring Batch Job

时间:2018-09-24 17:56:36

标签: java spring-batch

我有一个Spring Batch Tasklet,但我不知道如何从中失败。我要检查某些参数,如果不存在,请在该步骤中使作业失败。

@Component
public class Tfp211SetupTasklet extends AbstractSetupTasklet {

    final static Logger LOGGER = LoggerFactory.getLogger(Tfp211SetupTasklet.class);

    @Override
    protected RepeatStatus performTask(ExecutionContext ec, ChunkContext chunkContext) {
        //TODO
        //add error checking. If the parameter is not there, fail out or throw an error message.
        Map<String, String> params = new HashMap<>();
        List<String> requiredParams = new ArrayList<>();
        requiredParams.add("name");
        requiredParams.add("id");
        requiredParams.add("test");
        JobParameters jobParameters = chunkContext.getStepContext().getStepExecution().getJobParameters();
        params.put("name", jobParameters.getString("name"));
        params.put("id", jobParameters.getString("id"));
        params.put("test", jobParameters.getString("test"));
//        if (!params.values().containsAll(requiredParams)) {
//            LOGGER.info("not all required parameters exist for the job execution to succeed.");
//            return RepeatStatus.FINISHED;
//        }
        ec.put(AbstractSetupTasklet.BATCH_PROGRAM_PARAMS, params);
        ec.put(AbstractSetupTasklet.BATCH_PROGRAM_NAME, NTfp211.class.getSimpleName());
        return RepeatStatus.FINISHED;
    }

}

注释掉的行是我试图让工作退出的内容。有人有经验吗?

2 个答案:

答案 0 :(得分:2)

要使小任务失败,只需抛出一个异常即可。

答案 1 :(得分:1)

您可以implements界面StepExecutionListener,并在

@Override
public ExitStatus afterStep(StepExecution stepExecution) {
    // check failed condition
    return ExitStatus.FAILED;
}