Jian Li | 1b4cb33 | 2016-03-02 16:32:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onlab.util; |
| 18 | |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 19 | import com.google.common.base.Throwables; |
Jian Li | 1b4cb33 | 2016-03-02 16:32:51 -0800 | [diff] [blame] | 20 | import org.slf4j.Logger; |
| 21 | |
| 22 | import java.util.Collection; |
| 23 | import java.util.List; |
| 24 | import java.util.concurrent.Callable; |
| 25 | import java.util.concurrent.ExecutionException; |
| 26 | import java.util.concurrent.Future; |
| 27 | import java.util.concurrent.ScheduledExecutorService; |
| 28 | import java.util.concurrent.ScheduledFuture; |
| 29 | import java.util.concurrent.TimeUnit; |
| 30 | import java.util.concurrent.TimeoutException; |
| 31 | |
| 32 | import static org.slf4j.LoggerFactory.getLogger; |
| 33 | |
| 34 | /** |
| 35 | * A new scheduled executor service that does not eat exception. |
| 36 | */ |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 37 | class SharedScheduledExecutorService implements ScheduledExecutorService { |
Jian Li | 1b4cb33 | 2016-03-02 16:32:51 -0800 | [diff] [blame] | 38 | |
| 39 | private static final String NOT_ALLOWED = "Shutdown of scheduled executor is not allowed"; |
| 40 | private final Logger log = getLogger(getClass()); |
| 41 | |
| 42 | private ScheduledExecutorService executor; |
| 43 | |
| 44 | /** |
| 45 | * Creates a wrapper for the given scheduled executor service. |
| 46 | * |
| 47 | * @param executor executor service to wrap |
| 48 | */ |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 49 | SharedScheduledExecutorService(ScheduledExecutorService executor) { |
Jian Li | 1b4cb33 | 2016-03-02 16:32:51 -0800 | [diff] [blame] | 50 | this.executor = executor; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns the backing scheduled executor service. |
| 55 | * |
| 56 | * @return backing executor service |
| 57 | */ |
| 58 | ScheduledExecutorService backingExecutor() { |
| 59 | return executor; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Swaps the backing executor with a new one and shuts down the old one. |
| 64 | * |
| 65 | * @param executor new scheduled executor service |
| 66 | */ |
| 67 | void setBackingExecutor(ScheduledExecutorService executor) { |
| 68 | ScheduledExecutorService oldExecutor = this.executor; |
| 69 | this.executor = executor; |
| 70 | oldExecutor.shutdown(); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { |
| 75 | return executor.schedule(wrap(command), delay, unit); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { |
| 80 | return executor.schedule(() -> { |
| 81 | V v = null; |
| 82 | try { |
| 83 | v = callable.call(); |
| 84 | } catch (Exception e) { |
| 85 | log.error("Uncaught exception on " + callable.getClass(), e); |
| 86 | } |
| 87 | return v; |
| 88 | }, delay, unit); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, |
| 93 | long period, TimeUnit unit) { |
| 94 | return executor.scheduleAtFixedRate(wrap(command), initialDelay, period, unit); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, |
| 99 | long delay, TimeUnit unit) { |
| 100 | return executor.scheduleWithFixedDelay(wrap(command), initialDelay, delay, unit); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void shutdown() { |
| 105 | throw new UnsupportedOperationException(NOT_ALLOWED); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public List<Runnable> shutdownNow() { |
| 110 | throw new UnsupportedOperationException(NOT_ALLOWED); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public boolean isShutdown() { |
| 115 | return executor.isShutdown(); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public boolean isTerminated() { |
| 120 | return executor.isTerminated(); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { |
| 125 | return executor.awaitTermination(timeout, unit); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public <T> Future<T> submit(Callable<T> task) { |
| 130 | return executor.submit(task); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public <T> Future<T> submit(Runnable task, T result) { |
| 135 | return executor.submit(task, result); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public Future<?> submit(Runnable task) { |
| 140 | return executor.submit(task); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) |
| 145 | throws InterruptedException { |
| 146 | return executor.invokeAll(tasks); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, |
| 151 | long timeout, TimeUnit unit) |
| 152 | throws InterruptedException { |
| 153 | return executor.invokeAll(tasks, timeout, unit); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public <T> T invokeAny(Collection<? extends Callable<T>> tasks) |
| 158 | throws InterruptedException, ExecutionException { |
| 159 | return executor.invokeAny(tasks); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public <T> T invokeAny(Collection<? extends Callable<T>> tasks, |
| 164 | long timeout, TimeUnit unit) throws InterruptedException, |
| 165 | ExecutionException, TimeoutException { |
| 166 | return executor.invokeAny(tasks, timeout, unit); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void execute(Runnable command) { |
| 171 | executor.execute(command); |
| 172 | } |
| 173 | |
| 174 | private Runnable wrap(Runnable command) { |
| 175 | return new LoggableRunnable(command); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * A runnable class that allows to capture and log the exceptions. |
| 180 | */ |
| 181 | private class LoggableRunnable implements Runnable { |
| 182 | private Runnable runnable; |
| 183 | |
| 184 | public LoggableRunnable(Runnable runnable) { |
| 185 | super(); |
| 186 | this.runnable = runnable; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void run() { |
| 191 | try { |
| 192 | runnable.run(); |
| 193 | } catch (Exception e) { |
| 194 | log.error("Uncaught exception on " + runnable.getClass().getSimpleName(), e); |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 195 | throw Throwables.propagate(e); |
Jian Li | 1b4cb33 | 2016-03-02 16:32:51 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |