01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.springframework.scheduling.timer;
18:
19: import java.util.TimerTask;
20:
21: import org.apache.commons.logging.Log;
22: import org.apache.commons.logging.LogFactory;
23:
24: import org.springframework.util.Assert;
25:
26: /**
27: * Simple {@link java.util.TimerTask} adapter that delegates to a
28: * given {@link java.lang.Runnable}.
29: *
30: * <p>This is often preferable to deriving from TimerTask, to be able to
31: * implement an interface rather than extend an abstract base class.
32: *
33: * @author Juergen Hoeller
34: * @since 1.2.4
35: */
36: public class DelegatingTimerTask extends TimerTask {
37:
38: private static final Log logger = LogFactory
39: .getLog(DelegatingTimerTask.class);
40:
41: private final Runnable delegate;
42:
43: /**
44: * Create a new DelegatingTimerTask.
45: * @param delegate the Runnable implementation to delegate to
46: */
47: public DelegatingTimerTask(Runnable delegate) {
48: Assert.notNull(delegate, "Delegate must not be null");
49: this .delegate = delegate;
50: }
51:
52: /**
53: * Return the wrapped Runnable implementation.
54: */
55: public final Runnable getDelegate() {
56: return this .delegate;
57: }
58:
59: /**
60: * Delegates execution to the underlying Runnable, catching any exception
61: * or error thrown in order to continue scheduled execution.
62: */
63: public void run() {
64: try {
65: this .delegate.run();
66: } catch (Throwable ex) {
67: logger.error("Unexpected exception thrown from Runnable",
68: ex);
69: // Do not throw the exception, else the main loop of the Timer might stop!
70: }
71: }
72:
73: }
|