简体   繁体   中英

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)

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. 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.supplyAsync(() -> getApiValue())
                .thenApplyAsync(apiValue -> handle(apiValue))
                .thenApplyAsync(proc1Result -> handleMore(proc1Result))
                ... and so on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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