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.

java spring multithreading

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java Multithreading Example | Multithreading in Java

Java Multithreading Tutorial With Example | Multithreading in Java. Multithreading is a process of executing multiple threads simultaneously.

Spring Beans in Java

Spring IoC container is the core of the Spring Framework.In spring-based applications, objects live inside a spring containerThe container instan.

Java Spring Boot First App

Step by Step to your First Spring App

Spring | Spring JDBC Tutorial | Java Spring Tutorial Spring Training

Spring | Spring JDBC Tutorial | Java Spring Tutorial Spring Training . Spring Framework Certification Training This post Spring JDBC Tutorial video will help you