Use listenable future instead of plain ones
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 8 Jul 2015 18:01:11 +0000 (20:01 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 8 Jul 2015 18:03:17 +0000 (20:03 +0200)
src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommand.java
src/main/java/net/pterodactylus/fcp/quelaton/ClientGetCommandImpl.java
src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java
src/main/java/net/pterodactylus/fcp/quelaton/FcpReplySequence.java

index 280daae..7e039ba 100644 (file)
@@ -2,10 +2,11 @@ package net.pterodactylus.fcp.quelaton;
 
 import java.io.InputStream;
 import java.util.Optional;
-import java.util.concurrent.Future;
 
 import net.pterodactylus.fcp.Priority;
 
+import com.google.common.util.concurrent.ListenableFuture;
+
 /**
  * Command that retrieves data from Freenet.
  *
@@ -20,7 +21,7 @@ public interface ClientGetCommand {
        ClientGetCommand realTime();
        ClientGetCommand global();
 
-       Future<Optional<Data>> uri(String uri);
+       ListenableFuture<Optional<Data>> uri(String uri);
 
        interface Data {
 
index 98c416f..575d94b 100644 (file)
@@ -4,7 +4,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.Optional;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Future;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 
@@ -16,6 +15,10 @@ import net.pterodactylus.fcp.GetFailed;
 import net.pterodactylus.fcp.Priority;
 import net.pterodactylus.fcp.ReturnType;
 
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
 /**
  * Implementation of the {@link ClientGetCommand}.
  *
@@ -23,7 +26,7 @@ import net.pterodactylus.fcp.ReturnType;
  */
 class ClientGetCommandImpl implements ClientGetCommand {
 
-       private final ExecutorService threadPool;
+       private final ListeningExecutorService threadPool;
        private final ConnectionSupplier connectionSupplier;
 
        private boolean ignoreDataStore;
@@ -34,7 +37,7 @@ class ClientGetCommandImpl implements ClientGetCommand {
        private boolean global;
 
        public ClientGetCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
-               this.threadPool = threadPool;
+               this.threadPool = MoreExecutors.listeningDecorator(threadPool);
                this.connectionSupplier = connectionSupplier;
        }
 
@@ -75,7 +78,7 @@ class ClientGetCommandImpl implements ClientGetCommand {
        }
 
        @Override
-       public Future<Optional<Data>> uri(String uri) {
+       public ListenableFuture<Optional<Data>> uri(String uri) {
                ClientGet clientGet = createClientGetCommand(uri);
                return threadPool.submit(() -> new ClientGetReplySequence().send(clientGet).get());
        }
@@ -173,7 +176,7 @@ class ClientGetCommandImpl implements ClientGetCommand {
                }
 
                @Override
-               public Future<Optional<Data>> send(FcpMessage fcpMessage) throws IOException {
+               public ListenableFuture<Optional<Data>> send(FcpMessage fcpMessage) throws IOException {
                        identifier.set(fcpMessage.getField("Identifier"));
                        return super.send(fcpMessage);
                }
index 9c7a705..6d46ef7 100644 (file)
@@ -12,6 +12,9 @@ import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
 import net.pterodactylus.fcp.FcpConnection;
 import net.pterodactylus.fcp.NodeHello;
 
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
 /**
  * Default {@link FcpClient} implementation.
  *
@@ -19,7 +22,7 @@ import net.pterodactylus.fcp.NodeHello;
  */
 public class DefaultFcpClient implements FcpClient {
 
-       private final ExecutorService threadPool;
+       private final ListeningExecutorService threadPool;
        private final String hostname;
        private final int port;
        private final AtomicReference<FcpConnection> fcpConnection = new AtomicReference<>();
@@ -28,7 +31,7 @@ public class DefaultFcpClient implements FcpClient {
 
        public DefaultFcpClient(ExecutorService threadPool, String hostname, int port, Supplier<String> clientName,
                Supplier<String> expectedVersion) {
-               this.threadPool = threadPool;
+               this.threadPool = MoreExecutors.listeningDecorator(threadPool);
                this.hostname = hostname;
                this.port = port;
                this.clientName = clientName;
index 25db847..03d1b0b 100644 (file)
@@ -2,7 +2,6 @@ package net.pterodactylus.fcp.quelaton;
 
 import java.io.IOException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Future;
 import java.util.function.Consumer;
 
 import net.pterodactylus.fcp.AllData;
@@ -46,6 +45,10 @@ import net.pterodactylus.fcp.URIGenerated;
 import net.pterodactylus.fcp.UnknownNodeIdentifier;
 import net.pterodactylus.fcp.UnknownPeerNoteType;
 
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
 /**
  * An FCP reply sequence enables you to conveniently wait for a specific set of FCP replies.
  *
@@ -54,17 +57,17 @@ import net.pterodactylus.fcp.UnknownPeerNoteType;
 public abstract class FcpReplySequence<R> implements AutoCloseable, FcpListener {
 
        private final Object syncObject = new Object();
-       private final ExecutorService executorService;
+       private final ListeningExecutorService executorService;
        private final FcpConnection fcpConnection;
 
        public FcpReplySequence(ExecutorService executorService, FcpConnection fcpConnection) {
-               this.executorService = executorService;
+               this.executorService = MoreExecutors.listeningDecorator(executorService);
                this.fcpConnection = fcpConnection;
        }
 
        protected abstract boolean isFinished();
 
-       public Future<R> send(FcpMessage fcpMessage) throws IOException {
+       public ListenableFuture<R> send(FcpMessage fcpMessage) throws IOException {
                try {
                fcpConnection.addFcpListener(this);