【问题标题】:Spring batch pass dynamic file name to onSkipInReadSpring批处理将动态文件名传递给onSkipInRead
【发布时间】:2019-03-18 00:06:36
【问题描述】:

我想将动态文件名传递给 onSkipInRead。 例子: 公共 MySkipListener(@Value("#{jobParameters['file']}") 字符串文件) 抛出 IOException { bw= new BufferedWriter(new FileWriter(file); System.out.println("MySkipListener =========> :"+bw); }

  @Override
  public void onSkipInRead(Throwable throwable) {
      if (throwable instanceof FlatFileParseException) {
          FlatFileParseException flatFileParseException = (FlatFileParseException) throwable;
          try {
                bw.write(flatFileParseException.getInput()+"; Step Vérifiez les colonnes !!");
                bw.newLine();
                bw.flush();
          } catch (IOException e) {
              System.err.println("Unable to write skipped line to error file"); }
      }
  }

谢谢。

【问题讨论】:

  • 使用 Throwable 保存文件名。捕获异常并将文件名保存到 throwable 并在侦听器中使用它。

标签: spring-batch


【解决方案1】:

您可以在您的侦听器中创建一个带有文件的构造函数,并在定义步骤时将文件传递给它。这是一个例子:

class MySkipListener implements SkipListener<Integer, Integer> {

    private FileWriter fileWriter;

    public MySkipListener(File file) throws IOException {
        this.fileWriter = new FileWriter(file);
    }

    // your onSkipInRead method
}

然后在定义步骤时将作业参数文件传递给侦听器:

@Bean
@JobScope
public Step step(@Value("#{jobParameters['file']}") String file) throws IOException {
    return stepBuilderFactory.get("step")
            .<Integer, Integer>chunk(5)
            .reader(itemReader())
            .writer(itemWriter())
            .listener(new MySkipListener(new File(file)))
            .build();
}

希望这会有所帮助。

【讨论】:

  • 谢谢你,Mahoud,我是这条消息:原因:org.springframework.expression.spel.SpelEvaluationException:EL1008E:在“org.springframework”类型的对象上找不到属性或字段“jobParameters”。 beans.factory.config.BeanExpressionContext' - 可能不公开或无效?
  • 我可能错过了步骤上的@JobScope。我编辑了答案,现在应该可以正常工作了。
  • 好的,工作正确吗?我将 null 传递给 step? @Bean public Job jobCC5(JobBuilderFactory jobBuilders, StepBuilderFactory stepBuilders) throws IOException { return jobBuilders.get("jobCC5") .start(step1decryptNir(stepBuilders, null)).build(); }
  • OK bat 创建 jar 并运行后,我没有收到文件
  • private BufferedWriter bw = null;public MySkipListener(File file) throws IOException {bw= new BufferedWriter(new FileWriter("sftp_cpf_cc5\\fichiers\\env\\xdej_xcc5_cpt_logExeption.csv", true)); } @Override public void onSkipInRead(Throwable throwable) { if (throwable instanceof FlatFileParseException) { FlatFileParseException flatFileParseException = (FlatFileParseException) throwable;尝试 { bw.write(flatFileParseException.getInput()+";bw.newLine();bw.flush(); fileWriter.close();} catch (IOException e) { } } }
猜你喜欢
  • 1970-01-01
  • 2023-01-23
  • 2020-10-07
  • 2016-10-08
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多