001/*
002 * Copyright 2002-2013 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.scheduling.support;
018
019import java.lang.reflect.InvocationTargetException;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023
024import org.springframework.beans.factory.BeanClassLoaderAware;
025import org.springframework.beans.factory.InitializingBean;
026import org.springframework.beans.support.ArgumentConvertingMethodInvoker;
027import org.springframework.util.ClassUtils;
028
029/**
030 * Adapter that implements the {@link Runnable} interface as a configurable
031 * method invocation based on Spring's MethodInvoker.
032 *
033 * <p>Inherits common configuration properties from
034 * {@link org.springframework.util.MethodInvoker}.
035 *
036 * @author Juergen Hoeller
037 * @since 1.2.4
038 * @see java.util.concurrent.Executor#execute(Runnable)
039 */
040public class MethodInvokingRunnable extends ArgumentConvertingMethodInvoker
041                implements Runnable, BeanClassLoaderAware, InitializingBean {
042
043        protected final Log logger = LogFactory.getLog(getClass());
044
045        private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
046
047
048        @Override
049        public void setBeanClassLoader(ClassLoader classLoader) {
050                this.beanClassLoader = classLoader;
051        }
052
053        @Override
054        protected Class<?> resolveClassName(String className) throws ClassNotFoundException {
055                return ClassUtils.forName(className, this.beanClassLoader);
056        }
057
058        @Override
059        public void afterPropertiesSet() throws ClassNotFoundException, NoSuchMethodException {
060                prepare();
061        }
062
063
064        @Override
065        public void run() {
066                try {
067                        invoke();
068                }
069                catch (InvocationTargetException ex) {
070                        logger.error(getInvocationFailureMessage(), ex.getTargetException());
071                        // Do not throw exception, else the main loop of the scheduler might stop!
072                }
073                catch (Throwable ex) {
074                        logger.error(getInvocationFailureMessage(), ex);
075                        // Do not throw exception, else the main loop of the scheduler might stop!
076                }
077        }
078
079        /**
080         * Build a message for an invocation failure exception.
081         * @return the error message, including the target method name etc
082         */
083        protected String getInvocationFailureMessage() {
084                return "Invocation of method '" + getTargetMethod() +
085                                "' on target class [" + getTargetClass() + "] failed";
086        }
087
088}