Spring batch 在Spring批处理中存储文件名以发送电子邮件

Spring batch 在Spring批处理中存储文件名以发送电子邮件,spring-batch,Spring Batch,我正在Spring Batch中编写一个应用程序来实现这一点: 逐个文件读取文件夹的内容。 重命名文件并将其移动到多个文件夹中。 发送两封电子邮件:一封处理成功的名称文件,另一封发送错误的名称文件。 我已经得到1。二,。但我需要指出3点。?如何使用Spring Batch以优雅的方式存储已发送到writer方法的文件名 您可以使用执行上下文来存储文件名的值,这些文件名会被处理,也会因错误而失败 我们将有一个列表/类似的数据结构,其文件名位于业务逻辑之后。下面是一个实现StepExecutionL

我正在Spring Batch中编写一个应用程序来实现这一点:

逐个文件读取文件夹的内容。 重命名文件并将其移动到多个文件夹中。 发送两封电子邮件:一封处理成功的名称文件,另一封发送错误的名称文件。
我已经得到1。二,。但我需要指出3点。?如何使用Spring Batch以优雅的方式存储已发送到writer方法的文件名

您可以使用执行上下文来存储文件名的值,这些文件名会被处理,也会因错误而失败

我们将有一个列表/类似的数据结构,其文件名位于业务逻辑之后。下面是一个实现StepExecutionListener的小片段供参考

public class FileProcessor implements ItemWriter<TestData>, StepExecutionListener {

   private List<String> success = new ArrayList<>();
   private List<String> failed = new ArrayList<>();

   @Override
   public void beforeStep(StepExecution stepExecution) {

   }

   @Override
   public void write(List<? extends BatchTenantBackupData> items) throws Exception {
       // Business logic which adds the success and failure file names to the list 
          after processing
   }

   @Override
   public ExitStatus afterStep(StepExecution stepExecution) {     
       stepExecution.getJobExecution().getExecutionContext()
                         .put("fileProcessedSuccessfully", success);

       stepExecution.getJobExecution().getExecutionContext()
                         .put("fileProcessedFailure", failed);

       return ExitStatus.COMPLETED;
   }
}
现在我们已经将文件名存储在执行上下文中,我们将能够在发送电子邮件步骤中使用它

public class sendReport implements Tasklet, StepExecutionListener {

   private List<String> success = new ArrayList<>();
   private List<String> failed = new ArrayList<>();

   @Override
   public void beforeStep(StepExecution stepExecution) {

    try {

        // Fetch the list of file names which we have stored in the context from previous step

        success = (List<String>) stepExecution.getJobExecution().getExecutionContext()
                .get("fileProcessedSuccessfully");

        failed = (List<BatchJobReportContent>) stepExecution.getJobExecution()
                .getExecutionContext().get("fileProcessedFailure"); 

       } catch (Exception e) {
       }
   }

   @Override
   public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

    // Business logic to send email with the file names
   }

   @Override
   public ExitStatus afterStep(StepExecution stepExecution) {
       logger.debug("Email Trigger step completed successfully!");
       return ExitStatus.COMPLETED;
   }
}