completable-future

Replace Futures.successfulAsList with Java 8 CompletableFuture?

為{幸葍}努か 提交于 2019-12-10 15:15:44
问题 I am Looking for canonical code to replace Guava's Futures.successfulAsList() with Java 8's CompletableFuture code. I think CompletableFuture.allOf() seems like a replacement for Futures.allAsList(), but I don't see anything quite like successfulAsList() . 回答1: CompletableFuture.allOf(…) is actually closer to successfulAsList() than allAsList() . Indeed, allOf() only completes after all the given futures have completed, be it with a value or an exception. You can then inspect each future to

How can I test exception in completable future?

拈花ヽ惹草 提交于 2019-12-10 05:43:43
问题 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

When to use non-async methods of CompletableFuture?

假装没事ソ 提交于 2019-12-10 02:45:12
问题 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

Should I return CompletableFuture or Future when defining API?

送分小仙女□ 提交于 2019-12-10 01:45:56
问题 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 ? 回答1: My 2 cts: by returning a Future, you keep your options open and can return a Future, or a CompletableFuture - it makes no

Recursively cancel an allOf CompletableFuture

馋奶兔 提交于 2019-12-09 12:14:27
问题 If I have CompletableFuture<Something> future1 = service.request(param1); CompletableFuture<Something> future2 = service.request(param2); CompletableFuture<Void> many = CompletableFuture.allOf(future1, future2); what will happen when I do many.cancel() ? Will future1 and future2 be cancelled as well? If not, what would be the cleanest way to achieve this? I'm reluctant to hold on to future1 and future2 , just to be able to cancel them when I want to cancel many . Some background on why I want

How to use ExecutorService to poll until a result arrives

一个人想着一个人 提交于 2019-12-09 06:47:56
问题 I have a scenario where I have to poll a remote server checking if a task has completed. Once it has, I make a different call to retrieve the result. I originally figured I should use a SingleThreadScheduledExecutor with scheduleWithFixedDelay for polling: ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); ScheduledFuture future = executor.scheduleWithFixedDelay(() -> poll(jobId), 0, 10, TimeUnit.SECONDS); public void poll(String jobId) { boolean jobDone =

CompletableFuture withFallback / handle only some errors

喜欢而已 提交于 2019-12-09 05:46:04
问题 I'm receiving responses from a service call via CompletableFuture. I'd like to handle some known exceptions the service returns — such as optimistic concurrency control conflicts. Here's what I've got. Is there a better way to do this which does not wrap the exceptions or use SneakyThrows? Wrapping exceptions would mean other exception handlers must inspect causal chains instead of merely using instanceof . someService.call(request) .handle((response, error) -> { if (error == null) return

ForkJoinTask vs CompletableFuture

為{幸葍}努か 提交于 2019-12-08 15:59:03
问题 In Java 8 there are two ways of starting asynchronous computations - CompletableFuture and ForkJoinTask . They both seem fairly similar - the inner classes of CompletableFuture even extend ForkJoinTask . Is there a reason to use one over the other? One key difference that I can see is that the CompletableFuture.join method simply blocks until the future is complete ( waitingGet just spins using a ManagedBlocker ), whereas a ForkJoinTask.join can steal work off the queue to help the task you

Multiple thenApply in a completableFuture

梦想与她 提交于 2019-12-08 09:06:39
问题 I have a situation where I want to execute some methods in different threads but want to pass the result of one thread to another. I have following methods in my class. public static int addition(int a, int b){ System.out.println((a+b)); return (a+b); } public static int subtract(int a, int b){ System.out.println((a-b)); return (a-b); } public static int multiply(int a, int b){ System.out.println((a*b)); return (a*b); } public static String convert(Integer a){ System.out.println((a)); return

instance of CompletableFuture cannot get expected result

隐身守侯 提交于 2019-12-08 08:04:17
问题 CompletableFuture cf1 = CompletableFuture.supplyAsync(() -> { System.out.println("enter into completableFuture()"); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("start to out of completableFuture()"); return "a"; }); System.out.println("do something else"); cf1.thenApply(v -> v + " b").thenAcceptAsync(v -> System.out.println(v) ); System.out.println("finalize..."); //cannot get expected result, if this line was comment out. /