GithubHelp home page GithubHelp logo

robertoduessmann / paralyzed-fetch Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 1.0 61 KB

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

License: MIT License

Java 100.00%
threads forkjoin feign

paralyzed-fetch's Introduction

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)

paralyzed-fetch's People

Contributors

alikilickaya avatar robertoduessmann avatar

Watchers

 avatar  avatar

Forkers

alikilickaya

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.