Spring Batch with Rest API start /stop

Viewed 2846

I am using spring batch deployed on PCF and created 2 rest call using rest controller.

1) Start - Rest call will invoke spring batch -working fine

2) End - Rest call will terminate spring batch - Not able to find solution or method how to do that

I am looking how to terminate spring batch job in between running program same like when we kill java process id and terminate .

 */
@RestController
public class BatchTriggerController {

    private static final Logger log = LogManager.getLogger(BatchTriggerController.class);


    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    Job job;

    @RequestMapping(value = "/trigger/start", method = RequestMethod.GET)
    public String invokeBatch() {
        //
        long start = System.currentTimeMillis();

        JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis()).toJobParameters();

        long end = System.currentTimeMillis();

        NumberFormat formatter = new DecimalFormat("#0.00000");
        System.out.print("Execution time is " + formatter.format((end - start) / 1000d) + " seconds");
        log.info("loader Execution time is " + formatter.format((end - start) / 1000d) + " seconds");


        try {
            jobLauncher.run(job, jobParameters);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("BatchTriggerController Exception "+e.getMessage());
            return "Fail";
        }
        return "Success";
    }

    @RequestMapping(value = "/trigger/stop", method = RequestMethod.GET)
    public String stopBatch() {
        //
        long start = System.currentTimeMillis();

        JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis()).toJobParameters();

        long end = System.currentTimeMillis();

        NumberFormat formatter = new DecimalFormat("#0.00000");
        System.out.print("Execution time is " + formatter.format((end - start) / 1000d) + " seconds");
        log.info("loader Execution time is " + formatter.format((end - start) / 1000d) + " seconds");


        try {
            jobLauncher.STOP(job, jobParameters);//NO METHOD STOP 
        } catch (Exception e) {
            e.printStackTrace();
            log.error("BatchTriggerController Exception "+e.getMessage());
            return "Fail";
        }
        return "Success";
    }
1 Answers

jobLauncher.STOP(job, jobParameters);//NO METHOD STOP

The job launcher is used to launch job, It does not offer a method to stop a job.

What you are looking for is the more elaborate JobOperator API that allows you to start/stop/restart/abandon jobs. You can find more details in the JobOperator section.

Related