JAL-3878 Rename web service worker to more generic pollable task.
authorMateusz Warowny <warownia1@gmail.com>
Wed, 13 Oct 2021 11:58:31 +0000 (13:58 +0200)
committerMateusz Warowny <warownia1@gmail.com>
Wed, 13 Oct 2021 12:56:52 +0000 (14:56 +0200)
src/jalview/viewmodel/AlignmentViewport.java
src/jalview/ws2/PollableTaskI.java [new file with mode: 0644]
src/jalview/ws2/PollableTaskListenerI.java [new file with mode: 0644]
src/jalview/ws2/PollingTaskExecutor.java [new file with mode: 0644]
src/jalview/ws2/WebServiceExecutor.java [deleted file]
src/jalview/ws2/WebServiceThreadListenerI.java [deleted file]
src/jalview/ws2/WebServiceWorkerI.java
src/jalview/ws2/operations/AlignmentOperation.java
src/jalview/ws2/operations/AnnotationOperation.java

index 179bfa0..a5cde39 100644 (file)
@@ -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 (file)
index 0000000..88c7371
--- /dev/null
@@ -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 (file)
index 0000000..e5a3682
--- /dev/null
@@ -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 (file)
index 0000000..3e04c17
--- /dev/null
@@ -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<PollableTaskListenerI> 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 (file)
index 86016d2..0000000
+++ /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<WebServiceThreadListenerI> 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 (file)
index 386c6ac..0000000
+++ /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
index f1864fc..22b4c73 100644 (file)
@@ -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<WSJob> getJobs();
 
-  void startJobs() throws IOException;
+  void start() throws IOException;
 
-  boolean pollJobs() throws IOException;
+  boolean poll() throws IOException;
 
   WebServiceI getWebService();
 
index b70678a..5b2f554 100644 (file)
@@ -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())
index 6e887f9..b940f3b 100644 (file)
@@ -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())