如何从MultiResourceItemWriter获取currentResourceItemCount

时间:2014-07-07 09:41:57

标签: spring batch-processing spring-batch

org.springframework.batch.item.file.MultiResourceItemWriter将currentResourceItemCount保存到executionContext,但是在更新函数内部,它总是在写入函数之前调用,结果当我总是将currentResourceItemCount等于0时调用。 我想要实现的是获取有多少项将bean写入文件并将其放入页脚的值。

public class FooterCallback extends StepExecutionListenerSupport implements FlatFileFooterCallback {

private StepExecution stepExecution;

@Override
public void writeFooter(Writer writer) throws IOException {
    Integer itemCount  = stepExecution.getExecutionContext().getInt("MultiResourceItemWriter.resource.item.count");


      writer.write("F;" + itemCount  );
  }
}

配置是:

<bean id="csvGenerateWriter" class="org.springframework.batch.item.file.MultiResourceItemWriter" scope="step">
    <property name="resource" value="file:#{jobExecutionContext[outputPath]}#{jobExecutionContext[outputFile]}"/>
    <property name="itemCountLimitPerResource" value="3" /> 
    <property name="delegate" ref="delegateWriter" />
</bean>
<bean id="delegateWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
    <property name="footerCallback" ref="footerCallback" />
</bean>

但是使用上面的代码,我总是在itemCount中得到0。是错误还是功能?为什么在写入后不调用MultiResourceItemWriter中的更新附件?

1 个答案:

答案 0 :(得分:2)

创建一个带委托的自定义编写器;此作者也将负责页脚,并应处理书面项目计数。

public class FooterWriter implements ResourceAwareItemWriterItemStream<Item>,FlatFileFooterCallback  {
  ResourceAwareItemWriterItemStream<Item> delegate;
  int count;
  public void write(List<? extends Item> items) throws Exception {
    count += items.size();
    delegate.write(items);
  }
  public void setDelegate(ResourceAwareItemWriterItemStream<Item> delegate) {
    this.delegate = delegate;
  }
  public void writeFooter(Writer writer) throws IOException {
    writer.write("F;" + count);
  }
  public void open(ExecutionContext executionContext) throws ItemStreamException {
    count = executionContext.getInt("fwc", 0);
    delegate.open(executionContext);
  }
  public void update(ExecutionContext executionContext) throws ItemStreamException {
    executionContext.putInt("fwc", count);
    delegate.update(executionContext);
  }
  public void close() throws ItemStreamException {
    // call close() before reset count because close() calls writeFooter()!
    delegate.close();
    this.count = 0;
  }
  public void setResource(Resource resource) {
    delegate.setResource(resource);
  }
}

<bean id="csvGenerateWriter" class="org.springframework.batch.item.file.MultiResourceItemWriter" scope="step">
  <property name="resource" value="file:output.txt"/>
  <property name="itemCountLimitPerResource" value="3" /> 
  <property name="delegate" ref="delegateWriter" />
</bean>
<bean id="flatFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
  <property name="footerCallback" ref="delegateWriter" />
  <property name="lineAggregator">
    <bean class="org.springframework.batch.item.file.transform.PassThroughLineAggregator" />
  </property>
</bean>
<bean id="delegateWriter" class="footercallback.FooterWriter">
  <property name="delegate" ref="flatFileItemWriter" />
</bean>