summaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/handler/RequestDispatch.java
diff options
context:
space:
mode:
authorBjørn Christian Seime <bjorncs@verizonmedia.com>2021-12-09 16:25:27 +0100
committerBjørn Christian Seime <bjorncs@verizonmedia.com>2021-12-10 10:28:28 +0100
commit134af4f00cb0302f1ba465e51973fe44e1646e19 (patch)
tree5724df56111808aca6c33161e6f2d6740be0f8dd /jdisc_core/src/main/java/com/yahoo/jdisc/handler/RequestDispatch.java
parent4bc0e6916d4532f00b735a79905e40f3b8eb51ea (diff)
Remove use of Guava ListenableFuture from com.yahoo.jdisc.handler
This change is not 100% API compatible. Many classes from this package inherited types from Guava. The classes will have the same methods as before, but their type has obviously changed. Two options; merge now with the small risk of breakage or wait for Vespa 8 branch.
Diffstat (limited to 'jdisc_core/src/main/java/com/yahoo/jdisc/handler/RequestDispatch.java')
-rw-r--r--jdisc_core/src/main/java/com/yahoo/jdisc/handler/RequestDispatch.java33
1 files changed, 19 insertions, 14 deletions
diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/RequestDispatch.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/RequestDispatch.java
index c85aa6375af..c1457290904 100644
--- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/RequestDispatch.java
+++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/RequestDispatch.java
@@ -1,19 +1,20 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.handler;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
+import com.yahoo.jdisc.References;
import com.yahoo.jdisc.Request;
import com.yahoo.jdisc.ResourceReference;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.SharedResource;
-import com.yahoo.jdisc.References;
import java.nio.ByteBuffer;
import java.util.Collections;
-import java.util.concurrent.*;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
/**
* <p>This class provides a convenient way of safely dispatching a {@link Request}. Using this class you do not have to
@@ -46,7 +47,7 @@ import java.util.List;
*
* @author Simon Thoresen Hult
*/
-public abstract class RequestDispatch implements ListenableFuture<Response>, ResponseHandler {
+public abstract class RequestDispatch implements Future<Response>, ResponseHandler {
private final FutureConjunction completions = new FutureConjunction();
private final FutureResponse futureResponse = new FutureResponse(this);
@@ -106,22 +107,26 @@ public abstract class RequestDispatch implements ListenableFuture<Response>, Res
*
* @return A Future that can be waited for.
*/
- public final ListenableFuture<Response> dispatch() {
+ public final CompletableFuture<Response> dispatch() {
try (FastContentWriter writer = new FastContentWriter(connect())) {
for (ByteBuffer buf : requestContent()) {
writer.write(buf);
}
completions.addOperand(writer);
}
- return this;
+ return CompletableFuture.allOf(completions.completableFuture(), futureResponse)
+ .thenApply(__ -> {
+ try {
+ return futureResponse.get();
+ } catch (InterruptedException | ExecutionException e) {
+ throw new IllegalStateException(e); // Should not happens since both futures are complete
+ }
+ });
}
- @Override
public void addListener(Runnable listener, Executor executor) {
- List<ListenableFuture<?>> combined = new ArrayList<>(2);
- combined.add(completions);
- combined.add(futureResponse);
- Futures.allAsList(combined).addListener(listener, executor);
+ CompletableFuture.allOf(completions.completableFuture(), futureResponse)
+ .whenCompleteAsync((__, ___) -> listener.run(), executor);
}
@Override