gpt4 book ai didi

spring-batch - 带有 Rest API 启动/停止的 Spring Batch

转载 作者:行者123 更新时间:2023-12-05 05:08:02 24 4
gpt4 key购买 nike

我正在使用部署在 PCF 上的 spring batch,并使用 rest Controller 创建了 2 个 rest 调用。

1) 开始 - Rest 调用将调用 spring batch - 工作正常

2) End - Rest 调用将终止 spring batch - 无法找到解决方案或方法如何做到这一点

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

最佳答案

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

作业启动器用于启动作业,它不提供停止作业的方法。

你要找的是更精致的JobOperator允许您启动/停止/重新启动/放弃作业的 API。您可以在 JobOperator 中找到更多详细信息部分。

关于spring-batch - 带有 Rest API 启动/停止的 Spring Batch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58596310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com