completable-future

How to divide 1 completablefuture to many completablefuture in stream?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 15:44:33
For example I have such methods: public CompletableFuture<Page> getPage(int i) { ... } public CompletableFuture<Document> getDocument(int i) { ... } public CompletableFuture<Void> parseLinks(Document doc) { ... } And my flow: List<CompletableFuture> list = IntStream .range(0, 10) .mapToObj(i -> getPage(i)) // I want method like this: .thenApplyAndSplit(CompletableFuture<Page> page -> { List<CompletableFuture<Document>> docs = page.getDocsId() .stream() .map(i -> getDocument(i)) .collect(Collectors.toList()); return docs; }) .map(CompletableFuture<Document> future -> { return future.thenApply

Break the flow of CompletableFuture

旧巷老猫 提交于 2019-12-05 10:48:06
I have certain flow that runs async using the CompletableFuture , e.g.: foo(...) .thenAccept(aaa -> { if (aaa == null) { break! } else { ... } }) .thenApply(aaa -> { ... }) .thenApply(... So if my foo() returns null (in a future) I need to break very soon, otherwise, the flow continue. For now I have to check for null all the way, in every future block; but that is ugly. Would this be possible with CompletableFuture ? EDIT With CompletableFuture you can define your flow of async tasks, that are executed one after the other. For example, you may say: Do A, and when A finishes, do B, and then do

Is it correct to convert a CompletableFuture<Stream<T>> to a Publisher<T>?

我怕爱的太早我们不能终老 提交于 2019-12-05 10:46:39
问题 To allow multiple iterations on the resulting stream from a CompletableFuture<Stream<String>> I am considering one of the following approaches: Convert the resulting future to CompletableFuture<List<String>> through: teams.thenApply(st -> st.collect(toList())) Convert the resulting future to Flux<String> with cache: Flux.fromStream(teams::join).cache(); Flux<T> is the implementation of Publisher<T> in project reactor. Use case: I would like to get a sequence with the premier league teams

How can I test exception in completable future?

删除回忆录丶 提交于 2019-12-05 09:42:20
I have been converting some code to be asynchronous. The original unit test used the annotation @Test(expected = MyExcpetion.class) but I don't think this will work because the exception I want to assert on is wrapped in java.util.concurrent.ExcutionException . I did try calling my future like this but my assertion is still failing and I don't love that I had to add in return null myApiCall.get(123).exceptionally((ex) -> { assertEquals(ex.getCause(),MyCustomException.class) return null } I also tried this flavor but still not working myApiCall.get(123).exceptionally((ex) -> { assertThat(ex

What is the order in which multiple thenAccept blocks of a CompletableFuture are executed

喜夏-厌秋 提交于 2019-12-05 09:10:39
So I have a method that returns a CompletableFuture . Before returning, this method adds a block with thenAccept which is executed after the CompletableFuture completes. The caller of this method also adds another block with thenAccept . Obviously this can go on with multiple chained calls. In what order are the CompletionStage returned by the thenAccept invocations executed? Is it guaranteed to be the order in which they are added? If not, how can one guarantee that they are executed in the order in which they are added? PS: I am asking this based on my own experience with CompletableFuture

When to use non-async methods of CompletableFuture?

别等时光非礼了梦想. 提交于 2019-12-05 03:43:19
I (mostly) understand the three execution methods of CompletableFuture : non-async ( synchronous execution ) default async (asynchronous using the default executor) custom async (asynchronous using a custom executor) My question is: when should one favor the use of non-async methods? What happens if you have a code block that invokes other methods that also return CompletableFuture s? This might look cheap on the surface, but what happens if those methods also use non-async invocation? Doesn't this add up to one long non-async block that could get expensive? Should one restrict the use of non

Which executor is used when composing Java CompletableFutures?

耗尽温柔 提交于 2019-12-05 01:13:30
问题 I have a method on some repository class that returns a CompletableFuture . The code that completes these futures uses a third party library which blocks. I intend to have a separate bounded Executor which this repository class will use to make these blocking calls. Here is an example: public class PersonRepository { private Executor executor = ... public CompletableFuture<Void> create(Person person) {...} public CompletableFuture<Boolean> delete(Person person) {...} } The rest of my

Should I return CompletableFuture or Future when defining API?

99封情书 提交于 2019-12-05 00:49:41
In Java 8, is it better for interface or abstract class to define APIs returning CompletableFuture instead of returning Future ? Considering it is ugly converting Future to CompletableFuture and the fact that CompletableFuture will give the caller more flexibility of using functional style directly, what could be a good reason for an API to just return Future ? My 2 cts: by returning a Future, you keep your options open and can return a Future, or a CompletableFuture - it makes no difference from the caller's perspective. by returning a CompletableFuture, you give the caller more options (they

Most efficient way to stream on list of Futures

跟風遠走 提交于 2019-12-04 23:10:34
问题 I'm calling an async client method by streaming over a list of objects. The method returns Future. What's the best way to iterate over the list of Futures returned after the call (so as to process those Future which comes first)? Note: The async client only returns Future not CompletableFuture. Following is the code: List<Future<Object>> listOfFuture = objectsToProcess.parallelStream() .map((object) -> { /* calling an async client returning a Future<Object> */ }) .collect(Collectors.toList())

CompletableFuture | thenApplyAsync vs thenCompose and their use cases [duplicate]

假装没事ソ 提交于 2019-12-04 17:19:53
This question already has answers here : CompletableFuture | thenApply vs thenCompose (4 answers) Closed 2 years ago . I was trying to understand CompletableFuture, and came across 2 methods, thenApplyAsync and thenCompose. Am trying to understand the difference between these two. CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + " Printing hello"); return "Hello"; }).thenCompose((String s) -> { return CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + " Adding abc")