| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 16 |
|
public class PollingTaskExecutor |
| 17 |
|
{ |
| 18 |
|
private static final Map<ScheduledExecutorService, PollingTaskExecutor> executorPool = |
| 19 |
|
Collections.synchronizedMap(new WeakHashMap<>()); |
| 20 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 21 |
26 |
public static PollingTaskExecutor fromPool(ScheduledExecutorService executor)... |
| 22 |
|
{ |
| 23 |
26 |
return executorPool.computeIfAbsent(executor, PollingTaskExecutor::new); |
| 24 |
|
} |
| 25 |
|
|
| 26 |
|
private final ScheduledExecutorService executor; |
| 27 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 28 |
26 |
public PollingTaskExecutor(ScheduledExecutorService executor)... |
| 29 |
|
{ |
| 30 |
26 |
this.executor = executor; |
| 31 |
|
} |
| 32 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 33 |
26 |
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 |
|
|
| |
|
| 71.4% |
Uncovered Elements: 10 (35) |
Complexity: 12 |
Complexity Density: 0.57 |
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 58 |
26 |
private TaskRunnable(TaskI<?> task)... |
| 59 |
|
{ |
| 60 |
26 |
this.task = task; |
| 61 |
|
} |
| 62 |
|
|
| |
|
| 68.8% |
Uncovered Elements: 10 (32) |
Complexity: 11 |
Complexity Density: 0.55 |
|
| 63 |
34 |
@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 |
|
} |