How to wait for threads spawned from a spring-scheduler to complete?

How to wait for threads spawned from a spring-scheduler to complete?

I am using spring-scheduler and spawning multiple threads from the&nbsp;@Scheduled&nbsp;method (one of them has a rest call).&nbsp;<strong>Whats the best way to wait in the&nbsp;@Scheduled&nbsp;method for all threads to complete and then exit?</strong>

I am using spring-scheduler and spawning multiple threads from the @Scheduled method (one of them has a rest call). Whats the best way to wait in the @Scheduled method for all threads to complete and then exit?

Application details: I have a scheduled task wherein I get a list single type of object (say Class Order) from the DB, divide them into chunks (So if I get 5000 Orders, I will divide into 5 chunks of 1000 each). Then I loop over every chunk and for every Order in the chunk I get a list of Products from the Order and then loop over them. For every Product I need to make a rest call to another service which returns a value (e.g. price) which I need to set in this product. This is scheduled to be done every 1 hour (configurable).

What I have thought so far: In the scheduler, call a UpdatePriceJobs execute method. This methods first divides the list into chunks. Each chunk is then processed by a ChunkProcessor by its process method. The process method is annotated with spring @Async so that each chunk is processed in a separate thread. Then it calls the process method on the productProcessor service which makes the rest call. This method is again annotated with @Async. So idea is to run Chunks in parallel and then make parallel price update calls. As you can see I will have to wait for all the threads to finish otherwise some products will not get the updated prices.

First thing that I thought of was a countdown latch. However, I will have to pass it around in order to decrement the counter and second will have to calculate the number of threads that will be created.

@Override
public void process(Chunk<Order> chunk) {
    final List<Chunk<Order>> chunks = orderChunkProvider.getChunks(items);
    for (final Chunk<Order> chunk : chunks) {
        OrderChunkProcessor.process(chunk);
    }

}

@Async
@Override
public void process(Chunk<Order> chunk) {
    final List<Order> orders = chunk.getItems();
    for (final Order order : orders ) {
        final List<Product> products = order.getProductss();
        for (final Product product : products ) {
            productProcessor.process(product);
        }
 }

productProcessor.process(product) method makes the rest call and is also annotated with @Async

What is the best way to wait for all the threads created? Or is the design entirely flawed and some big changes would be required.

PS: I cannot use spring-batch as we use a NoSql database which doesn't support transactions and spring-batch doesn't support this.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Spring Boot Tutorials - Spring Boot Full Course

Spring Boot Tutorials | Full Course - What is Spring? Spring Boot is an open source Java-based framework used to create a Micro Service. Spring Boot contains a comprehensive infrastructure support for developing a micro service and enables you to develop enterprise-ready applications that you can “just run”.

Java Spring - Limiting Query Result with Spring Data JPA

Java Spring - Limiting Query Result with Spring Data JPA

Full-stack Reactive Java with Spring Framework, Spring Boot and Project Reactor

Reactive programming offers Java developers a way to build message-driven, elastic, resilient, and responsive services...yet many Java developers don't know where to begin.