Skip to content

How to handle parallel requests (fetch or something else) and wait for the response in Java

License

Notifications You must be signed in to change notification settings

robertoduessmann/paralyzed-fetch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

paralyzed-fetch

It's a example to how you can run some things in parallel, fetch from an API for example, and wait for the response to return to previously call using ForkJoinPool.

How it works

On ParalyzedFetchWeather we define a new ForkJoinPool, it will be responsable for handle the parallel requests.

When we instantiate the ForkJoinPool without parameters in constructor, it will take from java.lang.Runtime#availableProcessors (if we want, we can also define, like 8 or 16 parallelisms).

private static final ForkJoinPool executorService = new ForkJoinPool();

To call in parallel all requests is only split the collection and for each item call the executor with method submit.

cities.forEach(city -> executorService.submit(() -> weathers.add(getWeather(city))));

Once the call is done, we can use the method awaitQuiescence to the process waits for the finish for all parallels executions before continue.

executorService.awaitQuiescence(TIMEOUT_MINUTES, TimeUnit.MINUTES);

Installation

Build

$ mvn clean install

Running

$ mvn spring-boot:run

Usage

GET /weather/cities

$ curl -X GET http://localhost:8080/weather/cities

License

The MIT License (MIT)

About

How to handle parallel requests (fetch or something else) and wait for the response in Java

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages