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.support;
18:
19: import org.apache.commons.logging.Log;
20: import org.apache.commons.logging.LogFactory;
21:
22: import org.springframework.util.Assert;
23:
24: /**
25: * Runnable wrapper that catches any exception or error thrown
26: * from its delegate Runnable. Used for continuing scheduled
27: * execution even after an exception thrown from a task's Runnable.
28: *
29: * @author Juergen Hoeller
30: * @since 2.0.5
31: */
32: public class DelegatingExceptionProofRunnable implements Runnable {
33:
34: private static final Log logger = LogFactory
35: .getLog(DelegatingExceptionProofRunnable.class);
36:
37: private Runnable delegate;
38:
39: /**
40: * Create a new DelegatingExceptionProofRunnable.
41: * @param delegate the Runnable implementation to delegate to
42: */
43: public DelegatingExceptionProofRunnable(Runnable delegate) {
44: Assert.notNull(delegate, "Delegate must not be null");
45: this .delegate = delegate;
46: }
47:
48: public final Runnable getDelegate() {
49: return this .delegate;
50: }
51:
52: public void run() {
53: try {
54: this .delegate.run();
55: } catch (Throwable ex) {
56: logger.error("Unexpected exception thrown from Runnable",
57: ex);
58: // Do not throw the exception, else the main loop of the scheduler might stop!
59: }
60: }
61:
62: }
|