KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > scheduling > support > MethodInvokingRunnable


1 /*
2  * Copyright 2002-2006 the original author or authors.
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
17 package org.springframework.scheduling.support;
18
19 import java.lang.reflect.InvocationTargetException JavaDoc;
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>Derives from ArgumentConvertingMethodInvoker, inheriting common
34  * configuration properties from MethodInvoker.
35  *
36  * <p>Useful to generically encapsulate a method invocation as timer task for
37  * <code>java.util.Timer</code>, in combination with a DelegatingTimerTask adapter.
38  * Can also be used with JDK 1.5's <code>java.util.concurrent.Executor</code>
39  * abstraction, which works with plain Runnables.
40  *
41  * <p>Extended by Spring's MethodInvokingTimerTaskFactoryBean adapter
42  * for <code>java.util.TimerTask</code>. Note that you can populate a
43  * ScheduledTimerTask object with a plain MethodInvokingRunnable instance
44  * as well, which will automatically get wrapped with a DelegatingTimerTask.
45  *
46  * @author Juergen Hoeller
47  * @since 1.2.4
48  * @see org.springframework.util.MethodInvoker
49  * @see org.springframework.beans.support.ArgumentConvertingMethodInvoker
50  * @see org.springframework.scheduling.timer.DelegatingTimerTask
51  * @see org.springframework.scheduling.timer.ScheduledTimerTask#setRunnable
52  * @see org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean
53  * @see java.util.Timer
54  * @see java.util.concurrent.Executor#execute(Runnable)
55  */

56 public class MethodInvokingRunnable extends ArgumentConvertingMethodInvoker
57         implements Runnable JavaDoc, BeanClassLoaderAware, InitializingBean {
58
59     protected final Log logger = LogFactory.getLog(getClass());
60
61     private ClassLoader JavaDoc beanClassLoader = ClassUtils.getDefaultClassLoader();
62
63
64     public void setBeanClassLoader(ClassLoader JavaDoc classLoader) {
65         this.beanClassLoader = classLoader;
66     }
67
68     protected Class JavaDoc resolveClassName(String JavaDoc className) throws ClassNotFoundException JavaDoc {
69         return ClassUtils.forName(className, this.beanClassLoader);
70     }
71
72     public void afterPropertiesSet() throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc {
73         prepare();
74     }
75
76
77     public void run() {
78         try {
79             invoke();
80         }
81         catch (InvocationTargetException JavaDoc ex) {
82             logger.error(getInvocationFailureMessage(), ex);
83             // Do not throw exception, else the main loop of the Timer will stop!
84
}
85         catch (Throwable JavaDoc ex) {
86             logger.error(getInvocationFailureMessage(), ex);
87             // Do not throw exception, else the main loop of the Timer will stop!
88
}
89     }
90
91     protected String JavaDoc getInvocationFailureMessage() {
92         return "Invocation of method '" + getTargetMethod() +
93                 "' on target object [" + getTargetObject() + "] failed";
94     }
95
96 }
97
Popular Tags