001/*
002 * Copyright 2002-2017 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.lang.Nullable;
028import org.springframework.util.ClassUtils;
029
030/**
031 * Adapter that implements the {@link Runnable} interface as a configurable
032 * method invocation based on Spring's MethodInvoker.
033 *
034 * <p>Inherits common configuration properties from
035 * {@link org.springframework.util.MethodInvoker}.
036 *
037 * @author Juergen Hoeller
038 * @since 1.2.4
039 * @see java.util.concurrent.Executor#execute(Runnable)
040 */
041public class MethodInvokingRunnable extends ArgumentConvertingMethodInvoker
042                implements Runnable, BeanClassLoaderAware, InitializingBean {
043
044        protected final Log logger = LogFactory.getLog(getClass());
045
046        @Nullable
047        private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
048
049
050        @Override
051        public void setBeanClassLoader(ClassLoader classLoader) {
052                this.beanClassLoader = classLoader;
053        }
054
055        @Override
056        protected Class<?> resolveClassName(String className) throws ClassNotFoundException {
057                return ClassUtils.forName(className, this.beanClassLoader);
058        }
059
060        @Override
061        public void afterPropertiesSet() throws ClassNotFoundException, NoSuchMethodException {
062                prepare();
063        }
064
065
066        @Override
067        public void run() {
068                try {
069                        invoke();
070                }
071                catch (InvocationTargetException ex) {
072                        logger.error(getInvocationFailureMessage(), ex.getTargetException());
073                        // Do not throw exception, else the main loop of the scheduler might stop!
074                }
075                catch (Throwable ex) {
076                        logger.error(getInvocationFailureMessage(), ex);
077                        // Do not throw exception, else the main loop of the scheduler might stop!
078                }
079        }
080
081        /**
082         * Build a message for an invocation failure exception.
083         * @return the error message, including the target method name etc
084         */
085        protected String getInvocationFailureMessage() {
086                return "Invocation of method '" + getTargetMethod() +
087                                "' on target class [" + getTargetClass() + "] failed";
088        }
089
090}