我正在从我的控制器启动一个弹簧批处理作业。我需要通过基于java的配置从配置的自定义fileName
访问作业参数ItemReader
。但是我总是从我的ItemReader获取fileName为null。请帮忙。
我这样启动是因为我必须根据用户请求更改ItemReader文件。
我的控制器:
@RestController
@RequestMapping(value = "/")
public class SimpleController {
@Autowired
Step step;
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Autowired
private JobRepository jobRepository;
@Autowired
JobLauncher jobLauncher;
@RequestMapping(method = RequestMethod.GET)
public void StartJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
JobParameter param = new JobParameter("ValueBeingSetFromRequestHeader");
HashMap map = new HashMap<String, JobParameter>();
map.put("fileName",param);
SimpleJob job = new SimpleJob("test");
job.addStep(step);
System.out.println(jobRepository);
job.setJobRepository(jobRepository);
System.out.println( jobRepository.getLastJobExecution(job.getName(), new JobParameters(map)));
jobLauncher.run(job, new JobParameters(map));
}
}
我的Job步骤配置类是
public class ProducerBatchJobConfigurer {
private String fileName;
public String getFileName() {
return fileName;
}
@Value("#{jobParameters['fileName']}")
public void setFileName(String fileName) {
this.fileName = fileName;
}
@Bean
public ItemReader reader() {
System.out.println(fileName);
if (fileName == null)
return new DummyReader();
FlatFileItemReader faltFileReader = new FlatFileItemReader();
faltFileReader.setResource(new FileSystemResource(new File(fileName)));
faltFileReader.setResource(new FileSystemResource(new File(fileName)));
DefaultLineMapper<KafkaMessage> lineMapper = new DefaultLineMapper<KafkaMessage>();
lineMapper.setLineTokenizer(new DelimitedLineTokenizer());
lineMapper.setFieldSetMapper(new CSVFieldMapper());
faltFileReader.setLineMapper(lineMapper);
return faltFileReader;
}
@Bean
public ItemProcessor processor() {
return new CustomItemProcessor();
}
@Bean
public ItemWriter writer() {
return new KafkaWriter();
}
@Bean
public Step step1(StepBuilderFactory stepBuilderFactory,
ItemReader reader, ItemWriter writer,
ItemProcessor processor) {
System.out.println("ddddddddddddddd");
return stepBuilderFactory.get("step1")
.chunk(10).reader(reader)
.processor(processor).writer(writer).build();
}
@Bean
public StepScope stepScope() {
final StepScope stepScope = new StepScope();
stepScope.setAutoProxy(true);
return stepScope;
}
@Bean
public JobScope jobScope(){
return new JobScope();
}
}
答案 0 :(得分:3)
@Bean
public ItemReader reader() {
...
}
应改为
@Bean
public ItemStreamReader reader() {
...
}
或甚至更具体的
@Bean
public FlatFileItemReader reader() {
...
}
步骤定义也应改为
@Bean
public Step step1(StepBuilderFactory stepBuilderFactory,
ItemStreamReader reader, ItemWriter writer,
ItemProcessor processor) {
}
为什么这有必要?
如果itemReader实现ItemStream接口(或ItemStreamReader),Spring会将其视为此类接口。
直接连接到Step的读取器,写入器或处理器 如果它实现了ItemStream或a,它将自动注册 StepListener接口。
这意味着spring将调用open,update和close方法
处的类似问题答案 1 :(得分:0)
使用@Value("#{jobParameters[fileName]}")
代替@Value("#{jobParameters['fileName']}")