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 java.lang.reflect.InvocationTargetException;
20:
21: import org.apache.commons.logging.Log;
22: import org.apache.commons.logging.LogFactory;
23:
24: import org.springframework.beans.factory.BeanClassLoaderAware;
25: import org.springframework.beans.factory.InitializingBean;
26: import org.springframework.beans.support.ArgumentConvertingMethodInvoker;
27: import org.springframework.util.ClassUtils;
28:
29: /**
30: * Adapter that implements the Runnable interface as a configurable
31: * method invocation based on Spring's MethodInvoker.
32: *
33: * <p>Inherits common configuration properties from
34: * {@link org.springframework.util.MethodInvoker}.
35: *
36: * <p>Useful to generically encapsulate a method invocation as timer task
37: * for <code>java.util.Timer</code>, in combination with a
38: * {@link org.springframework.scheduling.timer.DelegatingTimerTask} adapter.
39: * Can also be used with JDK 1.5's <code>java.util.concurrent.Executor</code>
40: * abstraction, which works with plain Runnables.
41: *
42: * <p>Extended by Spring's
43: * {@link org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean}
44: * adapter for <code>java.util.TimerTask</code>. Note that you can populate a
45: * ScheduledTimerTask object with a plain MethodInvokingRunnable instance
46: * as well, which will automatically get wrapped with a DelegatingTimerTask.
47: *
48: * @author Juergen Hoeller
49: * @since 1.2.4
50: * @see org.springframework.scheduling.timer.ScheduledTimerTask#setRunnable(Runnable)
51: * @see java.util.concurrent.Executor#execute(Runnable)
52: */
53: public class MethodInvokingRunnable extends
54: ArgumentConvertingMethodInvoker implements Runnable,
55: BeanClassLoaderAware, InitializingBean {
56:
57: protected final Log logger = LogFactory.getLog(getClass());
58:
59: private ClassLoader beanClassLoader = ClassUtils
60: .getDefaultClassLoader();
61:
62: public void setBeanClassLoader(ClassLoader classLoader) {
63: this .beanClassLoader = classLoader;
64: }
65:
66: protected Class resolveClassName(String className)
67: throws ClassNotFoundException {
68: return ClassUtils.forName(className, this .beanClassLoader);
69: }
70:
71: public void afterPropertiesSet() throws ClassNotFoundException,
72: NoSuchMethodException {
73: prepare();
74: }
75:
76: public void run() {
77: try {
78: invoke();
79: } catch (InvocationTargetException ex) {
80: logger.warn(getInvocationFailureMessage(), ex);
81: // Do not throw exception, else the main loop of the scheduler might stop!
82: } catch (Throwable ex) {
83: logger.warn(getInvocationFailureMessage(), ex);
84: // Do not throw exception, else the main loop of the scheduler might stop!
85: }
86: }
87:
88: /**
89: * Build a message for an invocation failure exception.
90: * @return the error message, including the target method name etc
91: */
92: protected String getInvocationFailureMessage() {
93: return "Invocation of method '" + getTargetMethod()
94: + "' on target class [" + getTargetClass() + "] failed";
95: }
96:
97: }
|