简体   繁体   中英

Java Parallel Invocation for 3rd party system

I have to invoke a third party API 5 times with different params and produce a single output to my caller. Sequential way is fine but it takes time as each call takes 2 seconds as result 10 seconds. If I can invoke parallel I can produce the output in 3 seconds. What is the best way to achieve in spring, java environment?

Actually there aa lot of approaches you can try ... The easiest might be:

Annotate your method that makes the API call with @Async and enable the async execution in your spring app by adding @EnableAsync in your configuration. Now you can just call your method 5 times and they will be executed in parallel. You can find an example here https://spring.io/guides/gs/async-method/

Or create your own thread pool eg

ExecutorService pool = Executors.newFixedThreadPool(5);
pool.submit(() -> myApiCallMethod());

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