Exit Spring Batch Job within tasklet

笑着哭i 提交于 2020-06-28 02:19:12

问题


I have a Spring Batch tasklet and I can't figure out how to fail from it. I want to check for certain parameters and if they aren't there, fail the job out on that step.

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

}

The commented out lines are what I've tried to get the job to exit. Anyone had experience with this?


回答1:


To fail the tasklet, just throw an exception from it.




回答2:


you can implements interface StepExecutionListener, and in

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


来源:https://stackoverflow.com/questions/52484874/exit-spring-batch-job-within-tasklet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!