JAL-3690 switching to CompletableFuture to allow chaining
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Thu, 16 Jul 2020 12:37:22 +0000 (13:37 +0100)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Thu, 16 Jul 2020 12:37:22 +0000 (13:37 +0100)
src/jalview/gui/SlivkaPreferences.java
src/jalview/ws/WSDiscovererI.java
src/jalview/ws/jws2/Jws2Discoverer.java
src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java

index 0743d58..6c365b9 100644 (file)
@@ -308,18 +308,19 @@ public class SlivkaPreferences extends JPanel
   // Discoverer buttons action listeners
   private ActionListener refreshServicesAction = (ActionEvent e) -> {
     progressBar.setVisible(true);
-    new Thread(() -> {
-      Cache.log.info("Requesting service reload");
-      var task = discoverer.startDiscoverer();
-      try {
-        task.get();
+    Cache.log.info("Requesting service reload");
+    discoverer.startDiscoverer().handle((_discoverer, exception) -> {
+      if (exception == null)
+      {
         Cache.log.info("Reloading done");
-      } catch (Exception exc) {
-        Cache.log.error("Reloading failed", exc);
-      } finally {
-        SwingUtilities.invokeLater(() -> progressBar.setVisible(false));
       }
-    }).start();
+      else
+      {
+        Cache.log.error("Reloading failed", exception);
+      }
+      SwingUtilities.invokeLater(() -> progressBar.setVisible(false));
+      return null;
+    });
   };
 
   private ActionListener resetServicesAction = (ActionEvent e) -> {
index b8a1d43..e9eedd3 100644 (file)
@@ -4,7 +4,7 @@ import jalview.ws.api.ServiceWithParameters;
 
 import java.net.URL;
 import java.util.List;
-import java.util.concurrent.Future;
+import java.util.concurrent.CompletableFuture;
 
 public interface WSDiscovererI
 {
@@ -27,7 +27,7 @@ public interface WSDiscovererI
   
   public void removeServiceChangeListener(ServiceChangeListener listener);
 
-  public Future<WSDiscovererI> startDiscoverer();
+  public CompletableFuture<WSDiscovererI> startDiscoverer();
 
   public String getErrorMessages();
 
index 73d3700..7cf6993 100644 (file)
@@ -41,6 +41,7 @@ import java.util.List;
 import java.util.Set;
 import java.util.StringTokenizer;
 import java.util.Vector;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
@@ -538,7 +539,7 @@ public class Jws2Discoverer implements WSDiscovererI, Runnable
    * @return new thread
    */
   @Override
-  public FutureTask<WSDiscovererI> startDiscoverer()
+  public CompletableFuture<WSDiscovererI> startDiscoverer()
   {
     /*    if (restart())
         {
@@ -554,8 +555,11 @@ public class Jws2Discoverer implements WSDiscovererI, Runnable
     {
       setAborted(true);
     }
-    FutureTask<WSDiscovererI> task = new FutureTask<>(this, this);
-    new Thread(task).start();
+    CompletableFuture<WSDiscovererI> task = CompletableFuture
+            .supplyAsync(() -> {
+              run();
+              return Jws2Discoverer.this;
+            });
     return task;
   }
 
index 0e66c28..a42b382 100644 (file)
@@ -13,6 +13,7 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Set;
 import java.util.Vector;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -46,19 +47,23 @@ public class SlivkaWSDiscoverer implements WSDiscovererI
   }
 
   private Set<ServiceChangeListener> serviceListeners = new CopyOnWriteArraySet<>();
-  
+
   @Override
-  public void addServiceChangeListener(ServiceChangeListener l) {
+  public void addServiceChangeListener(ServiceChangeListener l)
+  {
     serviceListeners.add(l);
   }
-  
+
   @Override
-  public void removeServiceChangeListener(ServiceChangeListener l) {
+  public void removeServiceChangeListener(ServiceChangeListener l)
+  {
     serviceListeners.remove(l);
   }
-  
-  public void notifyServiceListeners(List<ServiceWithParameters> services) {
-    for (var listener : serviceListeners) {
+
+  public void notifyServiceListeners(List<ServiceWithParameters> services)
+  {
+    for (var listener : serviceListeners)
+    {
       listener.servicesChanged(this, services);
     }
   }
@@ -66,11 +71,14 @@ public class SlivkaWSDiscoverer implements WSDiscovererI
   private final ExecutorService executor = Executors.newSingleThreadExecutor();
   private Vector<Future<?>> discoveryTasks = new Vector<>();
 
-  public Future<WSDiscovererI> startDiscoverer()
+  public CompletableFuture<WSDiscovererI> startDiscoverer()
   {
-    FutureTask<WSDiscovererI> task = new FutureTask<>(this::reloadServices, this);
+    CompletableFuture<WSDiscovererI> task = CompletableFuture
+            .supplyAsync(() -> {
+              reloadServices();
+              return SlivkaWSDiscoverer.this;
+            }, executor);
     discoveryTasks.add(task);
-    executor.execute(task);
     return task;
   }