Item Reader中的Spring批处理作业参数

时间:2018-06-08 14:33:11

标签: spring-batch

我的参数(workingDirectory)总是null,我不明白为什么?
我正在使用工作启动器来启动这项工作 我在数据库中看到了job参数,但没有注入我的读者。

这是我的读者。

@Component("customerReader")
@StepScope
public class CustomReader implements ItemReader<Customer> {

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

  private BufferedReader reader;
  protected ObjectMapper objectMapper = new ObjectMapper();
  private int fileIdx = 1;


  @Override
  public Customer read() throws IOException {

    if(reader == null) {
      reader = new BufferedReader(new FileReader(new File(workingDirectory + "output1.json")));
    }
    String line = reader.readLine();
    if (line == null) {
      try {
        fileIdx++;
        reader = new BufferedReader(
            new FileReader(new File(workingDirectory + "output" + fileIdx + ".json")));
      }
      catch (FileNotFoundException ex) {
        return null;
      }
      line = reader.readLine();
    }
    return objectMapper.readValue(line, Customer.class);
  }
}

我发号称:

JobParameters p = new JobParametersBuilder().addString("workingDirectory", workingDirectory).toJobParameters();
JobExecution e = jobLauncher.run(
            recordGenerator.getJob(file, "/Users/abc/data/"), p);

0 个答案:

没有答案