Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 16:11:35 GMT
  2. Package jalview.ws2.actions

File PollingTaskExecutor.java

 

Coverage histogram

../../../img/srcFileCovDistChart8.png
20% of files have more coverage

Code metrics

12
25
5
2
108
91
15
0.6
5
2.5
3

Classes

Class Line # Actions
PollingTaskExecutor 16 4 3
1.0100%
PollingTaskExecutor.TaskRunnable 40 21 12
0.7142857371.4%
 

Contributing tests

This file is covered by 27 tests. .

Source view

1    package jalview.ws2.actions;
2   
3    import java.io.IOException;
4    import java.util.Collections;
5    import java.util.Map;
6    import java.util.Objects;
7    import java.util.WeakHashMap;
8    import java.util.concurrent.CancellationException;
9    import java.util.concurrent.CompletionException;
10    import java.util.concurrent.Future;
11    import java.util.concurrent.ScheduledExecutorService;
12    import java.util.concurrent.TimeUnit;
13   
14    import jalview.ws2.actions.api.TaskI;
15   
 
16    public class PollingTaskExecutor
17    {
18    private static final Map<ScheduledExecutorService, PollingTaskExecutor> executorPool =
19    Collections.synchronizedMap(new WeakHashMap<>());
20   
 
21  26 toggle public static PollingTaskExecutor fromPool(ScheduledExecutorService executor)
22    {
23  26 return executorPool.computeIfAbsent(executor, PollingTaskExecutor::new);
24    }
25   
26    private final ScheduledExecutorService executor;
27   
 
28  26 toggle public PollingTaskExecutor(ScheduledExecutorService executor)
29    {
30  26 this.executor = executor;
31    }
32   
 
33  26 toggle public Future<?> submit(TaskI<?> task)
34    {
35  26 Objects.requireNonNull(task);
36  26 return executor.scheduleWithFixedDelay(
37    new TaskRunnable(task), 0, 2, TimeUnit.SECONDS);
38    }
39   
 
40    private static class TaskRunnable implements Runnable
41    {
42    private final TaskI<?> task;
43   
44    private static final int STAGE_INIT = 0;
45   
46    private static final int STAGE_POLLING = 2;
47   
48    private static final int STAGE_FINISHED = 3;
49   
50    private static final int STAGE_STOPPED = 4;
51   
52    private int stage = STAGE_INIT;
53   
54    private static final int MAX_POLL_RETRY = 5;
55   
56    private int pollRetryCount = 0;
57   
 
58  26 toggle private TaskRunnable(TaskI<?> task)
59    {
60  26 this.task = task;
61    }
62   
 
63  34 toggle @Override
64    public void run()
65    {
66  34 if (task.getStatus().isDone())
67    {
68  0 stage = STAGE_STOPPED;
69    }
70  34 if (stage == STAGE_INIT)
71    {
72  26 try
73    {
74  26 task.init();
75  26 stage = STAGE_POLLING;
76    } catch (Exception e)
77    {
78  0 stage = STAGE_STOPPED;
79  0 throw new CompletionException(e);
80    }
81    }
82  34 try
83    {
84  34 if (stage == STAGE_POLLING && task.poll())
85    {
86  26 stage = STAGE_FINISHED;
87    }
88  34 if (stage == STAGE_FINISHED)
89    {
90  26 task.complete();
91  26 stage = STAGE_STOPPED;
92    }
93    } catch (Exception e)
94    {
95  0 if (++pollRetryCount > MAX_POLL_RETRY || e instanceof RuntimeException)
96    {
97  0 task.cancel();
98  0 stage = STAGE_STOPPED;
99  0 throw new CompletionException(e);
100    }
101    }
102  34 if (stage == STAGE_STOPPED)
103    {
104  26 throw new CancellationException();
105    }
106    }
107    }
108    }