From 86cd00a0cdc19cdb40b51e704067ec2c331f8e61 Mon Sep 17 00:00:00 2001 From: Mateusz Warowny Date: Wed, 13 Oct 2021 13:58:31 +0200 Subject: [PATCH] JAL-3878 Rename web service worker to more generic pollable task. --- src/jalview/viewmodel/AlignmentViewport.java | 6 +- src/jalview/ws2/PollableTaskI.java | 12 ++ src/jalview/ws2/PollableTaskListenerI.java | 14 +++ src/jalview/ws2/PollingTaskExecutor.java | 126 +++++++++++++++++++ src/jalview/ws2/WebServiceExecutor.java | 133 -------------------- src/jalview/ws2/WebServiceThreadListenerI.java | 14 --- src/jalview/ws2/WebServiceWorkerI.java | 10 +- src/jalview/ws2/operations/AlignmentOperation.java | 8 +- .../ws2/operations/AnnotationOperation.java | 8 +- 9 files changed, 166 insertions(+), 165 deletions(-) create mode 100644 src/jalview/ws2/PollableTaskI.java create mode 100644 src/jalview/ws2/PollableTaskListenerI.java create mode 100644 src/jalview/ws2/PollingTaskExecutor.java delete mode 100644 src/jalview/ws2/WebServiceExecutor.java delete mode 100644 src/jalview/ws2/WebServiceThreadListenerI.java diff --git a/src/jalview/viewmodel/AlignmentViewport.java b/src/jalview/viewmodel/AlignmentViewport.java index 179bfa0..a5cde39 100644 --- a/src/jalview/viewmodel/AlignmentViewport.java +++ b/src/jalview/viewmodel/AlignmentViewport.java @@ -62,7 +62,7 @@ import jalview.workers.ComplementConsensusThread; import jalview.workers.ConsensusThread; import jalview.workers.InformationThread; import jalview.workers.StrucConsensusThread; -import jalview.ws2.WebServiceExecutor; +import jalview.ws2.PollingTaskExecutor; import java.awt.Color; import java.beans.PropertyChangeSupport; @@ -996,9 +996,9 @@ public abstract class AlignmentViewport return false; } - private WebServiceExecutor wsExecutor = new WebServiceExecutor(); + private PollingTaskExecutor wsExecutor = new PollingTaskExecutor(); - public WebServiceExecutor getWSExecutor() + public PollingTaskExecutor getWSExecutor() { return wsExecutor; } diff --git a/src/jalview/ws2/PollableTaskI.java b/src/jalview/ws2/PollableTaskI.java new file mode 100644 index 0000000..88c7371 --- /dev/null +++ b/src/jalview/ws2/PollableTaskI.java @@ -0,0 +1,12 @@ +package jalview.ws2; + +public interface PollableTaskI +{ + void start() throws Exception; + + boolean poll() throws Exception; + + boolean isDone(); + + void done(); +} diff --git a/src/jalview/ws2/PollableTaskListenerI.java b/src/jalview/ws2/PollableTaskListenerI.java new file mode 100644 index 0000000..e5a3682 --- /dev/null +++ b/src/jalview/ws2/PollableTaskListenerI.java @@ -0,0 +1,14 @@ +package jalview.ws2; + +public interface PollableTaskListenerI +{ + public void submitted(PollableTaskI task); + + public void submissionFailed(PollableTaskI task, Exception e); + + public void pollFailed(PollableTaskI task, Exception e); + + public void cancelled(PollableTaskI task); + + public void done(PollableTaskI task); +} diff --git a/src/jalview/ws2/PollingTaskExecutor.java b/src/jalview/ws2/PollingTaskExecutor.java new file mode 100644 index 0000000..3e04c17 --- /dev/null +++ b/src/jalview/ws2/PollingTaskExecutor.java @@ -0,0 +1,126 @@ +package jalview.ws2; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import jalview.bin.Cache; + +public class PollingTaskExecutor +{ + private ScheduledExecutorService executor = Executors + .newSingleThreadScheduledExecutor(); + + public void submit(final PollableTaskI task) + { + executor.submit(() -> { + try + { + task.start(); + wsThreadSupport.submitted(task); + } catch (Exception e) + { + Cache.log.error("Failed to submit web service jobs.", e); + wsThreadSupport.submissionFailed(task, e); + return; + } + executor.schedule(() -> poll(task), 1, TimeUnit.SECONDS); + }); + } + + private void poll(PollableTaskI task) + { + boolean done; + try + { + done = task.poll(); + } catch (Exception e) + { + Cache.log.error("Failed to poll task.", e); + wsThreadSupport.pollFailed(task, e); + return; + } + if (!done) + { + executor.schedule(() -> poll(task), 1, TimeUnit.SECONDS); + } + else + { + task.done(); + wsThreadSupport.done(task); + } + } + + private WebServiceThreadSupport wsThreadSupport = new WebServiceThreadSupport(); + + public void addThreadListener(PollableTaskListenerI listener) + { + wsThreadSupport.addListener(listener); + } + + public void removeThreadListener(PollableTaskListenerI listener) + { + wsThreadSupport.removeListener(listener); + } + + public void shutdown() + { + executor.shutdownNow(); + } + +} + +class WebServiceThreadSupport implements PollableTaskListenerI +{ + List listeners = new CopyOnWriteArrayList<>(); + + @Override + public void submitted(PollableTaskI task) + { + for (var listener : listeners) + listener.submitted(task); + } + + @Override + public void submissionFailed(PollableTaskI task, Exception e) + { + for (var listener : listeners) + listener.submissionFailed(task, e); + } + + @Override + public void pollFailed(PollableTaskI task, Exception e) + { + for (var listener : listeners) + listener.pollFailed(task, e); + } + + @Override + public void cancelled(PollableTaskI task) + { + for (var listener : listeners) + listener.cancelled(task); + } + + @Override + public void done(PollableTaskI task) + { + for (var listener : listeners) + listener.done(task); + } + + public void addListener(PollableTaskListenerI listener) + { + if (!listeners.contains(listener)) + { + listeners.add(listener); + } + } + + public void removeListener(PollableTaskListenerI listener) + { + listeners.remove(listener); + } +} diff --git a/src/jalview/ws2/WebServiceExecutor.java b/src/jalview/ws2/WebServiceExecutor.java deleted file mode 100644 index 86016d2..0000000 --- a/src/jalview/ws2/WebServiceExecutor.java +++ /dev/null @@ -1,133 +0,0 @@ -package jalview.ws2; - -import java.io.IOException; -import java.util.EventObject; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -import jalview.bin.Cache; - -public class WebServiceExecutor -{ - private ScheduledExecutorService executor = Executors - .newSingleThreadScheduledExecutor(); - - public void submit(final WebServiceWorkerI worker) - { - executor.submit(() -> { - try - { - worker.startJobs(); - wsThreadSupport.submitted(worker); - } catch (Exception e) - { - Cache.log.error("Failed to submit web service jobs.", e); - wsThreadSupport.submissionFailed(worker, e); - return; - } - executor.schedule(() -> pollJobs(worker), 1, TimeUnit.SECONDS); - }); - } - - private void pollJobs(WebServiceWorkerI worker) - { - boolean done; - try - { - done = worker.pollJobs(); - } catch (Exception e) - { - Cache.log.error("Failed to poll web service jobs.", e); - for (WSJob job : worker.getJobs()) - { - job.setStatus(WSJobStatus.SERVER_ERROR); - } - wsThreadSupport.pollFailed(worker, e); - return; - } - if (!done) - { - executor.schedule(() -> pollJobs(worker), 1, TimeUnit.SECONDS); - } - else - { - worker.done(); - wsThreadSupport.done(worker); - } - } - - private WebServiceThreadSupport wsThreadSupport = new WebServiceThreadSupport(); - - public void addThreadListener(WebServiceThreadListenerI listener) - { - wsThreadSupport.addListener(listener); - } - - public void removeThreadListener(WebServiceThreadListenerI listener) - { - wsThreadSupport.removeListener(listener); - } - - public void shutdown() - { - executor.shutdownNow(); - } - -} - -class WebServiceThreadSupport implements WebServiceThreadListenerI -{ - List listeners = new CopyOnWriteArrayList<>(); - - @Override - public void submitted(WebServiceWorkerI thread) - { - for (var listener : listeners) - listener.submitted(thread); - } - - @Override - public void submissionFailed(WebServiceWorkerI thread, Exception e) - { - for (var listener : listeners) - listener.submissionFailed(thread, e); - } - - @Override - public void pollFailed(WebServiceWorkerI thread, Exception e) - { - for (var listener : listeners) - listener.pollFailed(thread, e); - } - - @Override - public void cancelled(WebServiceWorkerI thread) - { - for (var listener : listeners) - listener.cancelled(thread); - } - - @Override - public void done(WebServiceWorkerI thread) - { - for (var listener : listeners) - listener.done(thread); - } - - public void addListener(WebServiceThreadListenerI listener) - { - if (!listeners.contains(listener)) - { - listeners.add(listener); - } - } - - public void removeListener(WebServiceThreadListenerI listener) - { - listeners.remove(listener); - } -} diff --git a/src/jalview/ws2/WebServiceThreadListenerI.java b/src/jalview/ws2/WebServiceThreadListenerI.java deleted file mode 100644 index 386c6ac..0000000 --- a/src/jalview/ws2/WebServiceThreadListenerI.java +++ /dev/null @@ -1,14 +0,0 @@ -package jalview.ws2; - -public interface WebServiceThreadListenerI -{ - public void submitted(WebServiceWorkerI thread); - - public void submissionFailed(WebServiceWorkerI thread, Exception e); - - public void pollFailed(WebServiceWorkerI thread, Exception e); - - public void cancelled(WebServiceWorkerI thread); - - public void done(WebServiceWorkerI thread); -} \ No newline at end of file diff --git a/src/jalview/ws2/WebServiceWorkerI.java b/src/jalview/ws2/WebServiceWorkerI.java index f1864fc..22b4c73 100644 --- a/src/jalview/ws2/WebServiceWorkerI.java +++ b/src/jalview/ws2/WebServiceWorkerI.java @@ -3,19 +3,15 @@ package jalview.ws2; import java.io.IOException; import java.util.List; -import javax.print.attribute.standard.JobState; - -import jalview.util.MathUtils; - -public interface WebServiceWorkerI +public interface WebServiceWorkerI extends PollableTaskI { long getUID(); List getJobs(); - void startJobs() throws IOException; + void start() throws IOException; - boolean pollJobs() throws IOException; + boolean poll() throws IOException; WebServiceI getWebService(); diff --git a/src/jalview/ws2/operations/AlignmentOperation.java b/src/jalview/ws2/operations/AlignmentOperation.java index b70678a..5b2f554 100644 --- a/src/jalview/ws2/operations/AlignmentOperation.java +++ b/src/jalview/ws2/operations/AlignmentOperation.java @@ -45,7 +45,7 @@ import jalview.ws2.MenuEntryProviderI; import jalview.ws2.ResultSupplier; import jalview.ws2.WSJob; import jalview.ws2.WSJobStatus; -import jalview.ws2.WebServiceExecutor; +import jalview.ws2.PollingTaskExecutor; import jalview.ws2.WebServiceI; import jalview.ws2.WebServiceInfoUpdater; import jalview.ws2.WebServiceWorkerI; @@ -157,7 +157,7 @@ public class AlignmentOperation implements Operation final var calcName = service.getName(); String title = frame.getTitle(); - WebServiceExecutor executor = frame.getViewport().getWSExecutor(); + PollingTaskExecutor executor = frame.getViewport().getWSExecutor(); { var item = new JMenuItem(MessageManager.formatMessage( "label.calcname_with_default_settings", calcName)); @@ -367,7 +367,7 @@ public class AlignmentOperation implements Operation } @Override - public void startJobs() throws IOException + public void start() throws IOException { Cache.log.info(format("Starting new %s job.", service.getName())); String outputHeader = String.format("%s of %s%nJob details%n", @@ -447,7 +447,7 @@ public class AlignmentOperation implements Operation } @Override - public boolean pollJobs() + public boolean poll() { boolean done = true; for (WSJob job : getJobs()) diff --git a/src/jalview/ws2/operations/AnnotationOperation.java b/src/jalview/ws2/operations/AnnotationOperation.java index 6e887f9..b940f3b 100644 --- a/src/jalview/ws2/operations/AnnotationOperation.java +++ b/src/jalview/ws2/operations/AnnotationOperation.java @@ -24,7 +24,7 @@ import jalview.ws2.MenuEntryProviderI; import jalview.ws2.ResultSupplier; import jalview.ws2.WSJob; import jalview.ws2.WSJobStatus; -import jalview.ws2.WebServiceExecutor; +import jalview.ws2.PollingTaskExecutor; import jalview.ws2.WebServiceI; import jalview.ws2.WebServiceWorkerI; import jalview.ws2.utils.WSJobList; @@ -119,7 +119,7 @@ public class AnnotationOperation implements Operation protected void buildMenu(JMenu parent, AlignFrame frame) { final var calcName = service.getName(); - WebServiceExecutor wsExecutor = frame.getViewport().getWSExecutor(); + PollingTaskExecutor wsExecutor = frame.getViewport().getWSExecutor(); { var item = new JMenuItem(MessageManager.formatMessage( "label.calcname_with_default_settings", calcName)); @@ -207,13 +207,13 @@ public class AnnotationOperation implements Operation } @Override - public void startJobs() throws IOException + public void start() throws IOException { } @Override - public boolean pollJobs() throws IOException + public boolean poll() throws IOException { boolean done = true; for (WSJob job : getJobs()) -- 1.7.10.2