|
| 1 | +package io.javaoperatorsdk.operator.api.config; |
| 2 | + |
| 3 | +import java.io.Closeable; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.List; |
| 6 | +import java.util.concurrent.Callable; |
| 7 | +import java.util.concurrent.ExecutionException; |
| 8 | +import java.util.concurrent.ExecutorService; |
| 9 | +import java.util.concurrent.Future; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | +import java.util.concurrent.TimeoutException; |
| 12 | + |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | + |
| 16 | +public class ExecutorServiceManager implements Closeable { |
| 17 | + private static final Logger log = LoggerFactory.getLogger(ExecutorServiceManager.class); |
| 18 | + private static ExecutorServiceManager instance; |
| 19 | + |
| 20 | + private final ExecutorService executor; |
| 21 | + private final int terminationTimeoutSeconds; |
| 22 | + |
| 23 | + private ExecutorServiceManager(ExecutorService executor, int terminationTimeoutSeconds) { |
| 24 | + this.executor = executor; |
| 25 | + this.terminationTimeoutSeconds = terminationTimeoutSeconds; |
| 26 | + } |
| 27 | + |
| 28 | + public static void start(ConfigurationService configuration) { |
| 29 | + if (instance == null) { |
| 30 | + instance = new ExecutorServiceManager( |
| 31 | + new InstrumentedExecutorService(configuration.getExecutorService()), |
| 32 | + configuration.getTerminationTimeoutSeconds()); |
| 33 | + } else { |
| 34 | + log.debug("Already started, reusing already setup instance!"); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public static ExecutorServiceManager instance() { |
| 39 | + if (instance == null) { |
| 40 | + throw new IllegalStateException( |
| 41 | + "ExecutorServiceManager hasn't been started. Call start method before using!"); |
| 42 | + } |
| 43 | + return instance; |
| 44 | + } |
| 45 | + |
| 46 | + public ExecutorService executorService() { |
| 47 | + return executor; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void close() { |
| 52 | + try { |
| 53 | + log.debug("Closing executor"); |
| 54 | + executor.shutdown(); |
| 55 | + if (!executor.awaitTermination(terminationTimeoutSeconds, TimeUnit.SECONDS)) { |
| 56 | + executor.shutdownNow(); // if we timed out, waiting, cancel everything |
| 57 | + } |
| 58 | + } catch (InterruptedException e) { |
| 59 | + log.debug("Exception closing executor: {}", e.getLocalizedMessage()); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private static class InstrumentedExecutorService implements ExecutorService { |
| 64 | + private final boolean debug; |
| 65 | + private final ExecutorService executor; |
| 66 | + |
| 67 | + private InstrumentedExecutorService(ExecutorService executor) { |
| 68 | + this.executor = executor; |
| 69 | + debug = Utils.debugThreadPool(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void shutdown() { |
| 74 | + if (debug) { |
| 75 | + Thread.dumpStack(); |
| 76 | + } |
| 77 | + executor.shutdown(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public List<Runnable> shutdownNow() { |
| 82 | + return executor.shutdownNow(); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public boolean isShutdown() { |
| 87 | + return executor.isShutdown(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean isTerminated() { |
| 92 | + return executor.isTerminated(); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { |
| 97 | + return executor.awaitTermination(timeout, unit); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public <T> Future<T> submit(Callable<T> task) { |
| 102 | + return executor.submit(task); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public <T> Future<T> submit(Runnable task, T result) { |
| 107 | + return executor.submit(task, result); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public Future<?> submit(Runnable task) { |
| 112 | + return executor.submit(task); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) |
| 117 | + throws InterruptedException { |
| 118 | + return executor.invokeAll(tasks); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, |
| 123 | + TimeUnit unit) throws InterruptedException { |
| 124 | + return executor.invokeAll(tasks, timeout, unit); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public <T> T invokeAny(Collection<? extends Callable<T>> tasks) |
| 129 | + throws InterruptedException, ExecutionException { |
| 130 | + return executor.invokeAny(tasks); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) |
| 135 | + throws InterruptedException, ExecutionException, TimeoutException { |
| 136 | + return executor.invokeAny(tasks, timeout, unit); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void execute(Runnable command) { |
| 141 | + executor.execute(command); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments