Skip to content

Commit

Permalink
util: don't use ListenableFuture in SpreadAndWait
Browse files Browse the repository at this point in the history
Motivation:
Java's CompletableFuture covers most of cases where ListenableFuture is
used. To reduce the dependency on external library use java API where
it's possible.

Modification:
Update SpreadAndWait to use CompletableFuture.

Result:
less dependency on guava.

Acked-by: Paul Millar
Acked-by: Lea Morschel
Target: master
Require-book: no
Require-notes: no
  • Loading branch information
kofemann committed Apr 20, 2023
1 parent 8ceacc8 commit c136bb3
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions modules/dcache/src/main/java/diskCacheV111/util/SpreadAndWait.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
// $Id: SpreadAndWait.java,v 1.6 2007-07-08 17:02:48 tigran Exp $
package diskCacheV111.util;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.MoreExecutors;
import dmg.cells.nucleus.CellPath;
import java.io.Serializable;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import org.dcache.cells.CellStub;

import static org.dcache.util.CompletableFutures.fromListenableFuture;

public class SpreadAndWait<T extends Serializable> {

private final CellStub _stub;
Expand All @@ -23,19 +22,14 @@ public SpreadAndWait(CellStub stub) {

public synchronized void send(final CellPath destination, Class<? extends T> type,
Serializable msg) {
Futures.addCallback(_stub.send(destination, msg, type),
new FutureCallback<T>() {
@Override
public void onSuccess(T answer) {
SpreadAndWait.this.success(destination, answer);
}

@Override
public void onFailure(Throwable t) {
SpreadAndWait.this.failure();
}
},
MoreExecutors.directExecutor());
fromListenableFuture(_stub.send(destination, msg, type))
.whenComplete((a, t) -> {
if (t != null) {
SpreadAndWait.this.failure();
} else {
SpreadAndWait.this.success(destination, a);
}
});
_pending++;
}

Expand Down

0 comments on commit c136bb3

Please sign in to comment.