2
votes

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?

2

2 Answers

6
votes

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

2
votes

you can implements interface StepExecutionListener, and in

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