gpt4 book ai didi

java - 访问作业参数 Spring Batch

转载 作者:行者123 更新时间:2023-11-30 10:33:59 26 4
gpt4 key购买 nike

我一直在努力使用 spring batch 访问作业的作业参数。到目前为止,这是我的实现。

@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 参数?

最佳答案

另一种非常适用于 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");
}

关于java - 访问作业参数 Spring Batch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41927582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com