Spring批处理退出,退出状态:在实际作业完成之前完成?

Spring批处理退出,退出状态:在实际作业完成之前完成?,spring,amazon-dynamodb,spring-batch,Spring,Amazon Dynamodb,Spring Batch,在我的Spring批处理应用程序中,我编写了一个CustomItemWriter,它使用DynamoDBAsyncClient在内部将项写入DynamoDB,该客户端返回Future对象。我有一个有数百万条记录的输入文件。由于CustomItemWriter立即返回future对象,我的批处理作业将在5秒内退出,状态为已完成,但实际上,将所有项写入数据库需要3-4分钟,因此我希望批处理作业仅在将所有项写入数据库后完成。我该怎么做 作业定义如下 <bean id="report" c

在我的Spring批处理应用程序中,我编写了一个CustomItemWriter,它使用DynamoDBAsyncClient在内部将项写入DynamoDB,该客户端返回Future对象。我有一个有数百万条记录的输入文件。由于CustomItemWriter立即返回future对象,我的批处理作业将在5秒内退出,状态为已完成,但实际上,将所有项写入数据库需要3-4分钟,因此我希望批处理作业仅在将所有项写入数据库后完成。我该怎么做

作业定义如下

    <bean id="report" class="com.solution.model.Report" scope="prototype" />
        <batch:job id="job" restartable="true">
            <batch:step id="step1">
                <batch:tasklet>
                    <batch:chunk reader="cvsFileItemReader"  processor="filterReportProcessor" writer="customItemWriter"
                        commit-interval="20">
                    </batch:chunk>
                </batch:tasklet>
            </batch:step>
        </batch:job>
<bean id="customItemWriter" class="com.solution.writer.CustomeWriter"></bean>
public class CustomeWriter implements ItemWriter<Report>{
    public void write(List<? extends Report> item) throws Exception {
    List<Future<PutItemResult>> list = new LinkedList();
    AmazonDynamoDBAsyncClient client = new AmazonDynamoDBAsyncClient();
        for(Report report : item) {
            PutItemRequest req = new PutItemRequest();
            req.setTableName("MyTable");
            req.setReturnValue(ReturnValue.ALL_ODD);
            req.addItemEntry("customerId",new 
            AttributeValue(item.getCustomeId()));
            Future<PutItemResult> res = client.putItemAsync(req);
            list.add(res);
            }
    }

}
因为在ItemWriter中,它返回的future对象并不等待操作完成。从主菜单开始,因为所有项目都已提交以供写入,所以批处理状态显示为已完成,作业终止。 我希望只有在DynamoDB中执行实际写入操作后,此作业才会终止。
我们能不能再等一步,或者找个听者

这里有一种方法。因为
ItemWriter::write
不会返回任何内容,所以您可以使用侦听器功能

@Component
@JobScope
public class YourWriteListener implements ItemWriteListener<WhatEverYourTypeIs> {


  @Value("#{jobExecution.executionContext}")
  private ExecutionContext executionContext;


  @Override
  public void afterWrite(final List<? extends WhatEverYourTypeIs> paramList) {
     Future future = this.executionContext.readAndValidate("FutureKey", Future.class);
     //wait till the job is done using future object
  }

  @Override
  public void beforeWrite(final List<? extends WhatEverYourTypeIs> paramList) {

  }

  @Override
  public void onWriteError(final Exception paramException, final List<? extends WhatEverYourTypeIs> paramList) {

  }
}
@组件
@工作范围
公共类YourWriteListener实现ItemWriteListener{
@值(“#{jobExecution.executionContext}”)
私有ExecutionContext ExecutionContext;
@凌驾

public void afterWrite(最终列表)我们可以看到您的编写器代码更新了代码。从
未来的
中获取结果,该结果将阻止编写器,直到它实际完成。谢谢@M.Deinum
@Component
@JobScope
public class YourWriteListener implements ItemWriteListener<WhatEverYourTypeIs> {


  @Value("#{jobExecution.executionContext}")
  private ExecutionContext executionContext;


  @Override
  public void afterWrite(final List<? extends WhatEverYourTypeIs> paramList) {
     Future future = this.executionContext.readAndValidate("FutureKey", Future.class);
     //wait till the job is done using future object
  }

  @Override
  public void beforeWrite(final List<? extends WhatEverYourTypeIs> paramList) {

  }

  @Override
  public void onWriteError(final Exception paramException, final List<? extends WhatEverYourTypeIs> paramList) {

  }
}
public class YourItemWriter extends ItemWriter<WhatEverYourTypeIs> {

  @Value("#{jobExecution.executionContext}")
  private ExecutionContext executionContext;

  @Override
  protected void doWrite(final List<? extends WhatEverYourTypeIs> youritems) 

     //write to DynamoDb and get Future object
    executionContext.put("FutureKey", future);
    }

  }
@Bean
  public Step initStep() {

    return this.stepBuilders.get("someStepName").<YourTypeX, YourTypeY>chunk(10)
        .reader(yourReader).processor(yourProcessor)
        .writer(yourWriter).listener(YourWriteListener)
        .build();
  }