From: Mateusz Warowny Date: Thu, 16 Jul 2020 12:37:22 +0000 (+0100) Subject: JAL-3690 switching to CompletableFuture to allow chaining X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=8aa5c7c615e4fb1e3ee581f7299d7bebde60b5af;p=jalview.git JAL-3690 switching to CompletableFuture to allow chaining --- diff --git a/src/jalview/gui/SlivkaPreferences.java b/src/jalview/gui/SlivkaPreferences.java index 0743d58..6c365b9 100644 --- a/src/jalview/gui/SlivkaPreferences.java +++ b/src/jalview/gui/SlivkaPreferences.java @@ -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) -> { diff --git a/src/jalview/ws/WSDiscovererI.java b/src/jalview/ws/WSDiscovererI.java index b8a1d43..e9eedd3 100644 --- a/src/jalview/ws/WSDiscovererI.java +++ b/src/jalview/ws/WSDiscovererI.java @@ -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 startDiscoverer(); + public CompletableFuture startDiscoverer(); public String getErrorMessages(); diff --git a/src/jalview/ws/jws2/Jws2Discoverer.java b/src/jalview/ws/jws2/Jws2Discoverer.java index 73d3700..7cf6993 100644 --- a/src/jalview/ws/jws2/Jws2Discoverer.java +++ b/src/jalview/ws/jws2/Jws2Discoverer.java @@ -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 startDiscoverer() + public CompletableFuture startDiscoverer() { /* if (restart()) { @@ -554,8 +555,11 @@ public class Jws2Discoverer implements WSDiscovererI, Runnable { setAborted(true); } - FutureTask task = new FutureTask<>(this, this); - new Thread(task).start(); + CompletableFuture task = CompletableFuture + .supplyAsync(() -> { + run(); + return Jws2Discoverer.this; + }); return task; } diff --git a/src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java b/src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java index 0e66c28..a42b382 100644 --- a/src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java +++ b/src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java @@ -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 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 services) { - for (var listener : serviceListeners) { + + public void notifyServiceListeners(List 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> discoveryTasks = new Vector<>(); - public Future startDiscoverer() + public CompletableFuture startDiscoverer() { - FutureTask task = new FutureTask<>(this::reloadServices, this); + CompletableFuture task = CompletableFuture + .supplyAsync(() -> { + reloadServices(); + return SlivkaWSDiscoverer.this; + }, executor); discoveryTasks.add(task); - executor.execute(task); return task; }