gpt4 book ai didi

java - 如何选择应该在 Spring Batch + Spring Rest API 中运行的作业

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:06:13 24 4
gpt4 key购买 nike

我正在尝试实现 2 个 Spring Batch 作业,这些作业将在使用端点时运行。由于两者的 JobLauncher 方法相同,您如何选择要执行的一个?

@Autowired
private JobLauncher jobLauncher;

@Autowired
private Job job;

@RequestMapping(
value = "/expired",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
params = {"expireDate"}
)
@ResponseBody
public ResponseDTO expiredJob(@RequestParam(value = "expireDate") String expireDate) throws BusinessException, Exception {

if (!DateValidator.isDateFormatValid(expireDate)) {
throw new BusinessException(ExceptionCodes.DATE_FORMAT_ERROR);
}
JobParameters jobParameters = new JobParametersBuilder().addString("expireDate", expireDate).toJobParameters();
jobLauncher.run(job, jobParameters);

ResponseDTO responseDTO = new ResponseDTO();

return responseDTO;
}

@RequestMapping(
value = "/lucky",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE
)
@ResponseBody
public ResponseDTO rciplusJob() throws BusinessException, Exception {

JobParameters jobParameters = new JobParameters();
jobLauncher.run(job, jobParameters);

ResponseDTO responseDTO = new ResponseDTO();

return responseDTO;
}

最佳答案

你可以像我一样这样做。

我假设每个作业都有一个 Spring Batch 作业配置。例如:

@Bean(name = "job1")
public Job job1() {
return jobBuilders.get("job1")
.incrementer(new RunIdIncrementer())
.flow(step1())
.end()
.build();
}

job2 也一样:

@Bean(name = "job2")
public Job job2() {
return jobBuilders.get("job2")
.incrementer(new RunIdIncrementer())
.flow(step2())
.end()
.build();
}

现在在你的 Controller 中,你只需 Autowiring 两个作业:

@Autowired
@Qualifier("job1")
private Job job1;

@Autowired
@Qualifier("job2")
private Job job2;

要启动它们中的每一个,您可以这样做:

final JobExecution jobExecution = jobLauncher.run(job, jobParameters);

关于java - 如何选择应该在 Spring Batch + Spring Rest API 中运行的作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54704657/

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