访问FlatFileFooterCallback中的stepExecution

时间:2019-12-18 21:05:34

标签: spring-batch

我正在创建固定长度的文件,我必须在页脚中附加读取的文件数。我需要访问stepExecution来获取写计数,我遵循了此FlatFileFooterCallback - how to get access to StepExecution For Count。 StepExecution为空?

FlatFileFooterCallback

公共类LexisNexisRequestFileFooter实现FlatFileFooterCallback {

@Value("#{StepExecution}")
private StepExecution stepExecution;
int totalItemsWritten = 0;

@Override
public void writeFooter(Writer writer) throws IOException {
    System.out.println(stepExecution.getWriteCount());
    String julianDate = createJulianDate();
    String SAT = "##!!SAT#"+julianDate+totalItemsWritten+"                                                                                                                                                                                                                       \r\n";
    String SIT = "##!!SIT#"+julianDate+totalItemsWritten+"                                                                                                                                                                                                                           \r\n";
    String footer = SAT+SIT;
    writer.write(footer);

}

}

配置文件

@Bean
@StepScope
public FlatFileFooterCallback customFooterCallback() {
    return new LexisNexisRequestFileFooter();
}

编写器文件

    // Create writer instance
    FlatFileItemWriter<LexisNexisRequestRecord> writer = new FlatFileItemWriter<>();

    LexisNexisRequestFileFooter lexisNexisRequestFileFooter = new LexisNexisRequestFileFooter();

    writer.setFooterCallback(lexisNexisRequestFileFooter);

    // Set output file location
    writer.setResource(new FileSystemResource("homeData.txt"));

    // All job reptitions should append to same output file
    writer.setAppendAllowed(true);

    writer.setEncoding("ascii");

1 个答案:

答案 0 :(得分:0)

在编写器配置中,您正在此处手动创建页脚回调:

LexisNexisRequestFileFooter lexisNexisRequestFileFooter = new LexisNexisRequestFileFooter();
writer.setFooterCallback(lexisNexisRequestFileFooter);

,并且不注入步骤作用域的bean。您的item writer bean定义方法应类似于:

@Bean
public FlatFileItemWriter writer() {

   // Create writer instance
   FlatFileItemWriter<LexisNexisRequestRecord> writer = new FlatFileItemWriter<>();

   writer.setFooterCallback(customFooterCallback());

   // Set output file location
   writer.setResource(new FileSystemResource("homeData.txt"));

   // All job reptitions should append to same output file
   writer.setAppendAllowed(true);

   writer.setEncoding("ascii");
}