我一直在努力使用弹簧批访问作业的作业参数。这是我到目前为止的实施。
@Configuration
@EnableBatchProcessing
@PropertySource("classpath:batch.properties")
public class CSVBatchServiceImpl extends StepExecutionListenerSupport implements CSVBatchService {
private static final Logger LOGGER = LoggerFactory.getLogger(CSVBatchServiceImpl.class);
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
private QuestionReader questionReader = new QuestionReader();
@Bean(name = "importQuestionsJob")
public Job importQuestionsJob() {
return jobBuilderFactory.get("importQuestionsJob")
.incrementer(new RunIdIncrementer())
.flow(step1())
.end()
.build();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<Question, Question>chunk(2)
.reader(questionReader.reader())
.processor(processor())
.build();
}
@Bean
public QuestionProcessor processor() {
return new QuestionProcessor();
}
}
class QuestionReader extends StepExecutionListenerSupport {
private static final Logger LOGGER = LoggerFactory.getLogger(QuestionReader.class);
//TODO: remove this
private static JsonNode getJsonNode(String str) {
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readTree(str);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Bean
public FlatFileItemReader<Question> reader() {
FlatFileItemReader<Question> reader = new FlatFileItemReader<>();
//TODO get this as a parameter
reader.setResource(new ClassPathResource("duplicateLabels.csv"));
reader.setLinesToSkip(1);
reader.setLineMapper(new DefaultLineMapper<Question>() {{
setLineTokenizer((new DelimitedLineTokenizer() {{
setNames(new String[]{"label", "body", "real_answer"});
}}));
setFieldSetMapper(new QuestionFieldSetMapper());
}});
return reader;
}
private static class QuestionFieldSetMapper implements FieldSetMapper<Question> {
public Question mapFieldSet(FieldSet fieldSet) {
Question question = new Question();
question.setLabel(fieldSet.readString(0));
question.setBody(getJsonNode(fieldSet.readString(1)));
question.setRealAnswer(getJsonNode(fieldSet.readString(2)));
return question;
}
}
}
我称这项工作为:
JobParameters parameters = new JobParametersBuilder()
.addLong("time", System.currentTimeMillis())
.addString("filePath", "file.csv")
.toJobParameters();
jobLauncher.run(importQuestionsJob, parameters);
如何访问reader函数中的filePath参数?
答案 0 :(得分:2)
ItemProcessors批注是另一种对ItemReaders,ItemWriters,@BeforeStep等有效的解决方案。像StepExecutionListener一样由Eugene To mentioned支持。这是该解决方案的捷径。
一个实现可能看起来像这样
@BeforeStep
public void beforeStep(StepExecution stepExecution) {
JobParameters jobParameters = stepExecution.getJobParameters();
Long millis = jobParameters.getLong("time");
String path = jobParameters.getString("filePath");
}
答案 1 :(得分:1)
你应该可以做到,
@Value("#{jobParameters['filePath']}") String filePath;
如果出现任何问题,您可以尝试将您的阅读器放入@StepScope
。
答案 2 :(得分:0)
您可以在步骤中添加org.springframework.batch.core.listener.JobParameterExecutionContextCopyListener
。
TaskletStep step = stepBuilderFactory.get("my-best-step")
.<Item, Item>chunk(10)
.reader(myBestReader)
.writer(myBestWriter)
.listener(new JobParameterExecutionContextCopyListener())
.build();
该监听器会将JobParameters
复制到ItemReader的开放和更新方法中可用的ExecutionContext
答案 3 :(得分:0)
访问作业参数的一种方法是对您的阅读器类实现StepExecutionListener,以利用其重写的beforeStep和afterStep方法,
@Override
public void beforeStep(StepExecution stepExecution) {
String filePath = (String) stepExecution.getJobExecution().getExecutionContext()
.get("filePath");
}