有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java访问Spring批处理中的作业参数

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

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


共 (4) 个答案

  1. # 1 楼答案

    您可以将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复制到ExecutionContext,这在ItemReader的open和update方法中是可用的

  2. # 2 楼答案

    你应该能做到

    @Value("#{jobParameters['filePath']}") String filePath;
    

    如果有任何问题,你可以试着把你的读者放在@StepScope

  3. # 3 楼答案

    另一个对ItemProcessorsItemReadersItemWriters等非常有效的解决方案是@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");
    }
    
  4. # 4 楼答案

    访问作业参数的方法之一是对reader类实现StepExecutionListener,以利用其重写的beforeStep和afterStep方法

    @Override
    public void beforeStep(StepExecution stepExecution) {
       String filePath = (String) stepExecution.getJobExecution().getExecutionContext()
            .get("filePath");
    }