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

前端之家收集整理的这篇文章主要介绍了java-如何选择应在Spring Batch Spring Rest API中运行的作业 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试实现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",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;
}
最佳答案
您可以像我一样以这种方式进行操作.

我假设您为每个作业都有一个春季批处理作业配置.例如.:

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

现在,在您的控制器中,您只需为两个作业自动布线:

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

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

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

final JobExecution jobExecution = jobLauncher.run(job,jobParameters);
原文链接:https://www.f2er.com/spring/531611.html

猜你在找的Spring相关文章