无法将 JobParameters 值传递给 Spring 批次中的 tasklet
Unable to pass the JobParameters Value to the tasklet in Spring Batch
我已经关注 link: ,但在将 jobParameters
值传递给 tasklet
时仍然遇到问题。
我开发了如下代码:
JobConfiguration.java
@Component
public class JobConfiguration implements ApplicationContextAware{
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private JobExplorer jobExplorer;
@Autowired
private JobRepository jobRepository;
@Autowired
private JobRegistry jobRegistry;
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Bean
@StepScope
public Tasklet tasklet(@Value("#{jobParameters['name']}") String name) {
System.out.println("NAME VALUE = "+name);
return (contribution, chunkContext) -> {
System.out.println(String.format("The job run for %s", name));
return RepeatStatus.FINISHED;
};
}
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1")
.tasklet(tasklet(null))
.build())
.build();
}
}
JobLaunchingController.java
@RestController
public class JobLaunchingController {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
@PostMapping("/")
@ResponseStatus(value = HttpStatus.ACCEPTED)
public void launch(@RequestParam("name") String name) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
JobParameters jobParameters = new JobParametersBuilder()
.addString("name", name)
.toJobParameters();
JobExecution jobExecution = this.jobLauncher.run(job, jobParameters);
System.out.println("STATUS = "+jobExecution.getStatus());
}
}
日志:
2018-12-13 23:09:35.930 INFO 20004 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2018-12-13 23:09:35.930 INFO 20004 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2018-12-13 23:09:35.938 INFO 20004 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms
2018-12-13 23:09:55.046 INFO 20004 --- [nio-8080-exec-1] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=job]] launched with the following parameters: [{name=foo}]
2018-12-13 23:09:55.414 INFO 20004 --- [nio-8080-exec-1] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
The job run for null
2018-12-13 23:09:55.672 INFO 20004 --- [nio-8080-exec-1] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=job]] completed with the following parameters: [{name=foo}] and the following status: [COMPLETED]
STATUS = COMPLETED
StartingAJobApplication.java
@SpringBootApplication
@EnableBatchProcessing
public class StartingAJobApplication {
public static void main(String[] args) {
SpringApplication.run(StartingAJobApplication.class, args);
}
}
卷曲:
curl --data 'name=foo' localhost:8080
这是正常的,因为你自己将 tasklet 传递给作业并且它有 null 参数。
为了使用@StepScop 功能,您需要使用 spring 创建的 bean
@Bean
public Job job(Tasklet tasklet) {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1")
.tasklet(tasklet)
.build())
.build();
}
当你实现“执行”方法时,你有 SetpContribution 和 ChunkContext 作为参数。你必须使用 ChunkContext 来获取 jobParameter。
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
JobParameters jobParameters = chunkContext.getStepContext().getStepExecution().getJobParameters();
...
}
我已经关注 link: jobParameters
值传递给 tasklet
时仍然遇到问题。
我开发了如下代码:
JobConfiguration.java
@Component
public class JobConfiguration implements ApplicationContextAware{
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private JobExplorer jobExplorer;
@Autowired
private JobRepository jobRepository;
@Autowired
private JobRegistry jobRegistry;
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Bean
@StepScope
public Tasklet tasklet(@Value("#{jobParameters['name']}") String name) {
System.out.println("NAME VALUE = "+name);
return (contribution, chunkContext) -> {
System.out.println(String.format("The job run for %s", name));
return RepeatStatus.FINISHED;
};
}
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1")
.tasklet(tasklet(null))
.build())
.build();
}
}
JobLaunchingController.java
@RestController
public class JobLaunchingController {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
@PostMapping("/")
@ResponseStatus(value = HttpStatus.ACCEPTED)
public void launch(@RequestParam("name") String name) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
JobParameters jobParameters = new JobParametersBuilder()
.addString("name", name)
.toJobParameters();
JobExecution jobExecution = this.jobLauncher.run(job, jobParameters);
System.out.println("STATUS = "+jobExecution.getStatus());
}
}
日志:
2018-12-13 23:09:35.930 INFO 20004 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2018-12-13 23:09:35.930 INFO 20004 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2018-12-13 23:09:35.938 INFO 20004 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms
2018-12-13 23:09:55.046 INFO 20004 --- [nio-8080-exec-1] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=job]] launched with the following parameters: [{name=foo}]
2018-12-13 23:09:55.414 INFO 20004 --- [nio-8080-exec-1] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
The job run for null
2018-12-13 23:09:55.672 INFO 20004 --- [nio-8080-exec-1] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=job]] completed with the following parameters: [{name=foo}] and the following status: [COMPLETED]
STATUS = COMPLETED
StartingAJobApplication.java
@SpringBootApplication
@EnableBatchProcessing
public class StartingAJobApplication {
public static void main(String[] args) {
SpringApplication.run(StartingAJobApplication.class, args);
}
}
卷曲:
curl --data 'name=foo' localhost:8080
这是正常的,因为你自己将 tasklet 传递给作业并且它有 null 参数。
为了使用@StepScop 功能,您需要使用 spring 创建的 bean
@Bean
public Job job(Tasklet tasklet) {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1")
.tasklet(tasklet)
.build())
.build();
}
当你实现“执行”方法时,你有 SetpContribution 和 ChunkContext 作为参数。你必须使用 ChunkContext 来获取 jobParameter。
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
JobParameters jobParameters = chunkContext.getStepContext().getStepExecution().getJobParameters();
...
}