如何保证工作完成后才能开始下一步的IntegrationFlow?

How to guarantee that job is finished before starting the next step of IntegrationFlow?

我有以下流程声明:

return flow -> flow.handle(myHandler)                
                .handle(springBatchJobLauncher)              
                .gateway(acknowledgementFlow);

启动器看起来像这样:

@ServiceActivator
public JobExecution launch(JobLaunchRequest request) throws JobExecutionException {
    Job job = request.getJob();
    JobParameters jobParameters = request.getJobParameters();

    return jobLauncher.run(job, jobParameters);
}

我需要在 springBatchJobLauncher 开始的作业完成后立即开始 acknowledgementFlow。按照这个配置有保障吗?

我们来看看 JobLauncher.run() JavaDocs!

 * Start a job execution for the given {@link Job} and {@link JobParameters}
 * . If a {@link JobExecution} was able to be created successfully, it will
 * always be returned by this method, regardless of whether or not the
 * execution was successful. If there is a past {@link JobExecution} which
 * has paused, the same {@link JobExecution} is returned instead of a new
 * one created. A exception will only be thrown if there is a failure to
 * start the job. If the job encounters some error while processing, the
 * JobExecution will be returned, and the status will need to be inspected.

因此,如果作业已成功启动,您将获得一个 JobExecution 对象,但这与您的作业是否完成无关紧要。为了这个目标,我相信我们需要有类似 JobExecutionListener 的钩子。

在 Spring 批处理文档中查看更多信息:https://docs.spring.io/spring-batch/4.2.x/reference/html/spring-batch-integration.html#providing-feedback-with-informational-messages

step有一个示例:

@MessagingGateway(name = "notificationExecutionsListener", defaultRequestChannel = "stepExecutionsChannel")
public interface NotificationExecutionListener extends StepExecutionListener {}

但同样的方法也适用于 JobExecutionListener

因此,您需要将流程分成两部分,并让您的 .gateway(acknowledgementFlow) 仅从 afterJob(JobExecution jobExecution) 中调用,从您的作业中执行。