我在我的REST控制器上使用jobParameters
启动Spring Batch作业@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
RequestMapping(value="/startjob", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody List<EventReports> addReportIds(@RequestBody List<Integer> reportIds) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
Logger logger = LoggerFactory.getLogger(this.getClass());
try {
JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
.addString("eventType", "Event Reports")
.toJobParameters();
jobLauncher.run(job, jobParameters);
} catch (Exception e) {
logger.info(e.getMessage());
}
System.out.println("Completed event reports batch job");
return null;
}
我的BatchConfiguration类看起来像
@Configuration
@EnableBatchProcessing
@Import(AppConfig.class)
public class BatchConfiguration {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private AppConfig appConfig;
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.incrementer(new RunIdIncrementer())
.flow(step1())
.end()
.build();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<String, String> chunk(1)
.reader(new Reader())
.processor(new Processor())
.writer(new Writer())
.build();
}
}
我试图在我的Reader类中打印出jobParameters
@Component
@StepScope
public class Reader implements ItemReader<String> {
private String[] messages = {"Hello World!", "Welcome to Spring Batch!"};
@Value("#{jobParameters['time']}")
private Long time;
@Value("#{jobParameters['eventType']}")
private String eventType;
private int count=0;
Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
System.out.println("Time: " + time);
System.out.println("Type: " + eventType);
if(count < messages.length){
return messages[count++];
}else{
count=0;
}
return null;
}
}
其他一切正常 - 但我的时间和eventType jobParameters的值在我的Reader类中打印为null - 尽管看起来作业是从日志判断作业参数启动
2017-04-28 15:49:25.302 INFO 12568 --- [nio-8081-exec-1] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [nam
e=job]] launched with the following parameters: [{time=1493412564856, eventType=Event Reports}]
2017-04-28 15:49:25.716 INFO 12568 --- [nio-8081-exec-1] o.s.batch.core.job.SimpleStepHandler : Executing step: [st
ep1]
Time: null
Type: null
#Writer Step: HELLO WORLD!
Time: null
Type: null
#Writer Step: WELCOME TO SPRING BATCH!
Time: null
Type: null
2017-04-28 15:49:26.069 INFO 12568 --- [nio-8081-exec-1] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [nam
e=job]] completed with the following parameters: [{time=1493412564856, eventType=Event Reports}] and the following statu
s: [COMPLETED]
Completed event reports batch job
2017-04-28 16:16:52.155 INFO 12568 --- [ Thread-3] ationConfigEmbeddedWebApplicationContext : Closing org.springf
ramework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3d82c5f3: startup date [Fri Apr 28 15:49:00
不确定我在这里失踪了什么。任何建议表示赞赏。
答案 0 :(得分:1)
答案 1 :(得分:0)
你需要掌握Job Execution&amp;使用它获取工作参数。使用SpEL不会从作业参数中获取值。 请参考今年春季论坛问题:Getting objects from JobExecutionContext