blob: e2b9b6f499420d741ca285af20966c7220a1b80d [file] [log] [blame]
Jian Li1b4cb332016-03-02 16:32:51 -08001/*
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
17package org.onlab.util;
18
Jian Li66f15262016-03-03 11:18:40 -080019import com.google.common.base.Throwables;
Jian Li1b4cb332016-03-02 16:32:51 -080020import org.slf4j.Logger;
21
22import java.util.Collection;
23import java.util.List;
24import java.util.concurrent.Callable;
25import java.util.concurrent.ExecutionException;
26import java.util.concurrent.Future;
27import java.util.concurrent.ScheduledExecutorService;
28import java.util.concurrent.ScheduledFuture;
29import java.util.concurrent.TimeUnit;
30import java.util.concurrent.TimeoutException;
31
32import static org.slf4j.LoggerFactory.getLogger;
33
34/**
35 * A new scheduled executor service that does not eat exception.
36 */
Jian Li66f15262016-03-03 11:18:40 -080037class SharedScheduledExecutorService implements ScheduledExecutorService {
Jian Li1b4cb332016-03-02 16:32:51 -080038
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 Li66f15262016-03-03 11:18:40 -080049 SharedScheduledExecutorService(ScheduledExecutorService executor) {
Jian Li1b4cb332016-03-02 16:32:51 -080050 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 Li66f15262016-03-03 11:18:40 -0800195 throw Throwables.propagate(e);
Jian Li1b4cb332016-03-02 16:32:51 -0800196 }
197 }
198 }
199}