Committed by
Gerrit Code Review
[ONOS-3528] Log exceptions of the tasks for SharedExecutorService
This commit tries to log all the runtime exceptions of the tasks that are executed by using submit method of SharedExecutorService. Change-Id: I72760213a0f977096353dacd20263e5c93af5c6a
Showing
1 changed file
with
32 additions
and
4 deletions
| ... | @@ -16,9 +16,11 @@ | ... | @@ -16,9 +16,11 @@ |
| 16 | package org.onlab.util; | 16 | package org.onlab.util; |
| 17 | 17 | ||
| 18 | import com.codahale.metrics.Timer; | 18 | import com.codahale.metrics.Timer; |
| 19 | +import com.google.common.base.Throwables; | ||
| 19 | import org.onlab.metrics.MetricsComponent; | 20 | import org.onlab.metrics.MetricsComponent; |
| 20 | import org.onlab.metrics.MetricsFeature; | 21 | import org.onlab.metrics.MetricsFeature; |
| 21 | import org.onlab.metrics.MetricsService; | 22 | import org.onlab.metrics.MetricsService; |
| 23 | +import org.slf4j.Logger; | ||
| 22 | 24 | ||
| 23 | import java.util.Collection; | 25 | import java.util.Collection; |
| 24 | import java.util.List; | 26 | import java.util.List; |
| ... | @@ -39,6 +41,7 @@ import static org.slf4j.LoggerFactory.getLogger; | ... | @@ -39,6 +41,7 @@ import static org.slf4j.LoggerFactory.getLogger; |
| 39 | class SharedExecutorService implements ExecutorService { | 41 | class SharedExecutorService implements ExecutorService { |
| 40 | 42 | ||
| 41 | private static final String NOT_ALLOWED = "Shutdown of shared executor is not allowed"; | 43 | private static final String NOT_ALLOWED = "Shutdown of shared executor is not allowed"; |
| 44 | + private final Logger log = getLogger(getClass()); | ||
| 42 | 45 | ||
| 43 | private ExecutorService executor; | 46 | private ExecutorService executor; |
| 44 | 47 | ||
| ... | @@ -78,7 +81,6 @@ class SharedExecutorService implements ExecutorService { | ... | @@ -78,7 +81,6 @@ class SharedExecutorService implements ExecutorService { |
| 78 | oldExecutor.shutdown(); | 81 | oldExecutor.shutdown(); |
| 79 | } | 82 | } |
| 80 | 83 | ||
| 81 | - | ||
| 82 | @Override | 84 | @Override |
| 83 | public void shutdown() { | 85 | public void shutdown() { |
| 84 | throw new UnsupportedOperationException(NOT_ALLOWED); | 86 | throw new UnsupportedOperationException(NOT_ALLOWED); |
| ... | @@ -138,12 +140,12 @@ class SharedExecutorService implements ExecutorService { | ... | @@ -138,12 +140,12 @@ class SharedExecutorService implements ExecutorService { |
| 138 | 140 | ||
| 139 | @Override | 141 | @Override |
| 140 | public <T> Future<T> submit(Runnable task, T result) { | 142 | public <T> Future<T> submit(Runnable task, T result) { |
| 141 | - return executor.submit(task, result); | 143 | + return executor.submit(wrap(task), result); |
| 142 | } | 144 | } |
| 143 | 145 | ||
| 144 | @Override | 146 | @Override |
| 145 | public Future<?> submit(Runnable task) { | 147 | public Future<?> submit(Runnable task) { |
| 146 | - return executor.submit(task); | 148 | + return executor.submit(wrap(task)); |
| 147 | } | 149 | } |
| 148 | 150 | ||
| 149 | @Override | 151 | @Override |
| ... | @@ -193,10 +195,36 @@ class SharedExecutorService implements ExecutorService { | ... | @@ -193,10 +195,36 @@ class SharedExecutorService implements ExecutorService { |
| 193 | } | 195 | } |
| 194 | } | 196 | } |
| 195 | 197 | ||
| 198 | + private Runnable wrap(Runnable command) { | ||
| 199 | + return new LoggableRunnable(command); | ||
| 200 | + } | ||
| 201 | + | ||
| 202 | + /** | ||
| 203 | + * A runnable class that allows to capture and log the exceptions. | ||
| 204 | + */ | ||
| 205 | + private class LoggableRunnable implements Runnable { | ||
| 206 | + | ||
| 207 | + private Runnable runnable; | ||
| 208 | + | ||
| 209 | + public LoggableRunnable(Runnable runnable) { | ||
| 210 | + super(); | ||
| 211 | + this.runnable = runnable; | ||
| 212 | + } | ||
| 213 | + | ||
| 214 | + @Override | ||
| 215 | + public void run() { | ||
| 216 | + try { | ||
| 217 | + runnable.run(); | ||
| 218 | + } catch (Exception e) { | ||
| 219 | + log.error("Uncaught exception on " + runnable.getClass().getSimpleName(), e); | ||
| 220 | + throw Throwables.propagate(e); | ||
| 221 | + } | ||
| 222 | + } | ||
| 223 | + } | ||
| 224 | + | ||
| 196 | /** | 225 | /** |
| 197 | * CallableExtended class is used to get Runnable Object | 226 | * CallableExtended class is used to get Runnable Object |
| 198 | * from Callable Object. | 227 | * from Callable Object. |
| 199 | - * | ||
| 200 | */ | 228 | */ |
| 201 | class CallableExtended implements Callable { | 229 | class CallableExtended implements Callable { |
| 202 | 230 | ... | ... |
-
Please register or login to post a comment