completable-future

Axon & CompletableFuture

那年仲夏 提交于 2019-12-24 12:12:48
问题 I've faced with problems when i try to use CompletableFuture with Axon. For example: CompletableFuture future = CompletableFuture.supplyAsync(() -> { log.info("Start processing target: {}", target.toString()); return new Event(); }, threadPool); future.thenAcceptAsync(event -> { log.info("Send Event"); AggregateLifecycle.apply(event); }, currentExecutor); in thenAcceptAsync - AggregateLifecycle.apply(event) has unexpected behavior. Some of my @EventSourcingHandler handlers start handling

Force re-use of thread by CompletableFuture

允我心安 提交于 2019-12-24 00:49:47
问题 I am making critical use of: CompletableFuture .delayedExecutor(1, TimeUnit.MILLISECONDS).execute(() -> {}); From what I have read online, it's common for this to use a new thread for every call. I am wondering if there is a way to re-use a thread instead of creating new threads? Update : I wasn't clear - I want to use CompletableFuture , but I want CompletableFuture to reuse a certain thread, instead of managing its own threads. I see this question: CompletableFuture reuse thread from pool

Why CompletableFuture 's thenAccept() not running on the main thread

*爱你&永不变心* 提交于 2019-12-23 15:10:52
问题 I process the long running operation inside the CompletableFuture's supplyAsync() and get the result into thenAccept(). In some times thenAccept() perform on the main thread but some time it running on the worker thread.But I want run thenAccept() operation only on the main thread. this is the sample code. private void test() { ExecutorService executorService = Executors.newSingleThreadExecutor(); CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> { System.out.println(

Why calling get() before exceptional completion waits for exceptionally to execute?

你。 提交于 2019-12-23 09:15:11
问题 While answering this question, I noticed a strange behaviour of CompletableFuture : if you have a CompletableFuture cf and chain a call with cf.exceptionally() , calling cf.get() appears to behave strangely: if you call it before exceptional completion, it waits for the execution of the exceptionally() block before returning otherwise, it fails immediately by throwing the expected ExecutionException Am I missing something or is this a bug? I am using Oracle JDK 1.8.0_131 on Ubuntu 17.04. The

Difference between parallel stream and CompletableFuture

こ雲淡風輕ζ 提交于 2019-12-23 09:03:40
问题 In the book "Java 8 in action" (by Urma, Fusco and Mycroft) they highlight that parallel streams internally use the common fork join pool and that whilst this can be configured globally, e.g. using System.setProperty(...), that it is not possibly to specify a value for a single parallel stream. I have since seen the workaround that involves running the parallel stream inside a custom made ForkJoinPool. Later on in the book, they have an entire chapter dedicated to CompletableFuture, during

Difference between Java8 thenCompose and thenComposeAsync

你说的曾经没有我的故事 提交于 2019-12-23 03:18:09
问题 Given this piece of code: public List<String> findPrices(String product){ List<CompletableFuture<String>> priceFutures = shops.stream() .map(shop -> CompletableFuture.supplyAsync( () -> shop.getPrice(product), executor)) .map(future -> future.thenApply(Quote::parse)) .map(future -> future.thenCompose(quote -> CompletableFuture.supplyAsync( () -> Discount.applyDiscount(quote), executor ))) .collect(toList()); return priceFutures.stream() .map(CompletableFuture::join) .collect(toList()); } This

How to turn a List<Item> to a Map<Item, completeablefuture<xyz>>

[亡魂溺海] 提交于 2019-12-22 18:52:42
问题 I have an async method with a completeablefuture result: public CompletableFuture<DogLater> asyncDogLater(String dogName){} I have a list of dogs: List<Dog> dogs; Now, I want to create a map from the dog's name to the Completeablefuture: Map<String, CompletableFuture<DogLater>> map; After checking this and this I was trying to do so: Map<String, CompletableFuture<DogLater>> completableFutures = dogs.stream() .collect( Collectors.toMap(Dog::getName, asyncDogLater(Dog::getName ))); But the

How to turn a List<Item> to a Map<Item, completeablefuture<xyz>>

╄→гoц情女王★ 提交于 2019-12-22 18:52:23
问题 I have an async method with a completeablefuture result: public CompletableFuture<DogLater> asyncDogLater(String dogName){} I have a list of dogs: List<Dog> dogs; Now, I want to create a map from the dog's name to the Completeablefuture: Map<String, CompletableFuture<DogLater>> map; After checking this and this I was trying to do so: Map<String, CompletableFuture<DogLater>> completableFutures = dogs.stream() .collect( Collectors.toMap(Dog::getName, asyncDogLater(Dog::getName ))); But the

How to make a future that gets completed when any of the given CompletableFutures is completed with a result that matches a certain predicate?

时间秒杀一切 提交于 2019-12-22 12:18:37
问题 I have a list of CompletableFuture<MyService> : List<CompletableFuture<MyService>> where MyService is immutable like the following: public class MyService { public boolean isAvailable() { return true; // or false } } I now want a future that is completed when one of the futures: is completed; and for the MyService instance as provided by that future: MyService.isAvailable() returns true When proceeding, I need to have the MyService instance that is available. In other words, I want a

Break the flow of CompletableFuture

拜拜、爱过 提交于 2019-12-22 06:45:23
问题 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