简体   繁体   English

有序的、长时间运行的异步作业

[英]Ordered, long-running asynchronous jobs

My Spring Boot application should work as follows: 1. Receive a request 2. Trigger some long-running job by making an API call to external microservice 3. Periodically check the job status 4. Once the job is completed, start the second job the same way 5. Periodically check the job status 6. Once the job is completed, start the third job the same way 7. And so on (there are a few such steps)我的 Spring Boot 应用程序应该如下工作: 1. 接收请求 2. 通过对外部微服务进行 API 调用来触发一些长时间运行的作业 3. 定期检查作业状态 4. 作业完成后,启动第二个作业同理 5. 定期检查作业状态 6. 作业完成后,以同样的方式启动第三个作业 7. 依此类推(有几个这样的步骤)

The process outlined above can take up to 30 minutes, so I don't want to block a single thread for such a long time.上面概述的过程可能需要长达 30 分钟,所以我不想阻塞一个线程这么长时间。 I would appreciate any ideas of how to implement that in a reasonable way.我将不胜感激如何以合理的方式实现它的任何想法。

Based on provided description Scheduler seems like a fitting approach.根据提供的描述,调度程序似乎是一种合适的方法。 By default schedulers use one, separate thread in which you can implement your logic.默认情况下,调度程序使用一个单独的线程,您可以在其中实现您的逻辑。 Additianally if it's running, new job waits...另外,如果它正在运行,新作业会等待......

You can do this with CompletableFuture .你可以用CompletableFuture做到这一点。

CompletableFuture.supplyAsync(() -> getApiValue())
                .thenApplyAsync(apiValue -> handle(apiValue))
                .thenApplyAsync(proc1Result -> handleMore(proc1Result))
                ... and so on

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM