无法读取 Tasklet / ItemReader 中的 REST 参数

Unable to Read REST Parameters in Tasklet / ItemReader

我想设置一个新的批处理作业。

此作业应从 Rest 接口接收一些参数(我正在使用 @EnableBatchProcessing 进行自动 JobScanning)。

我只希望每次休息调用执行一次作业 -> 这就是为什么我认为 tasklet 是首选武器。但是我没有让 @StepScope 与仅 tasklet 的作业一起工作(似乎没有块就没有可用的 StepScope 但如果我错了请纠正我)...

我的另一个想法是创建一个 ItemReader,它读取 JobParameters 并创建一个域对象(从参数),然后处理数据并写入一个 Dummy ItemWriter。

我尝试像这样设置 ItemReader:

@Bean
@StepScope
public ItemReader<BatchPrinterJob> setupParameterItemReader(
        @Value("#{jobParameters}") Map<String, Object> jobParameters) {

    ItemReader<BatchPrinterJob> reader = new ItemReader<BatchPrinterJob>() {

        @Override
        public BatchPrinterJob read()
                throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {

            BatchPrinterJob job = new BatchPrinterJob();
            LOG.info(jobParameters.toString());
            return job;
        }
    };
    return reader;
}

我试图用这样的 POST 请求开始工作:myhost:8080/jobs/thisjobsname?name=testname

但唯一被记录的是 run.id。

i think a tasklet would be the weapon of choice. But i did not get @StepScope to work with a tasklet only Job (it seems as if there is no StepScope available without chunk but please correct me if i am wrong)...

你可以在 tasklet 上使用 @StepScope,这里是一个例子:

@Bean
@StepScope
public Tasklet tasklet(@Value("#{jobParameters['parameter']}") String parameter) {
    return (contribution, chunkContext) -> {
        // use job parameter here
        return RepeatStatus.FINISHED;
    };
}

然后使用tasklet创建步骤:

@Bean
public Step step() {
    return this.stepBuilderFactory.get("step")
            .tasklet(tasklet(null))
            .build();
}

好吧,我根据你的例子为这个工作写了我自己的启动器:

@RestController
public class AutoPrintLaunchingController {

@Autowired
private JobLauncher jobLauncher;

@Autowired
private AutoPrint autoprint;

@RequestMapping(value = "/jobs/AutoPrint", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.ACCEPTED)
public void launch(@RequestParam("Drucker") String printer, @RequestParam("Fach") String fach,
        @RequestParam("Dokument") String doc, @RequestParam("DokumentParameter") String param) throws Exception {

    JobParametersBuilder jpb = new JobParametersBuilder();
    jpb.addString("Drucker", printer);
    jpb.addString("Fach", fach);
    jpb.addString("Dokument", doc);
    jpb.addString("DokumentParameter", param);
    jpb.addDate("dateInstant", Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));

    JobParameters jobParameters = jpb.toJobParameters();
    this.jobLauncher.run(autoprint.createAutoPrintJob(), jobParameters);
}

}

这是按要求工作的!