001/*
002 * Copyright 2002-2018 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;
020import java.lang.reflect.Method;
021import java.lang.reflect.UndeclaredThrowableException;
022
023import org.springframework.util.ReflectionUtils;
024
025/**
026 * Variant of {@link MethodInvokingRunnable} meant to be used for processing
027 * of no-arg scheduled methods. Propagates user exceptions to the caller,
028 * assuming that an error strategy for Runnables is in place.
029 *
030 * @author Juergen Hoeller
031 * @since 3.0.6
032 * @see org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor
033 */
034public class ScheduledMethodRunnable implements Runnable {
035
036        private final Object target;
037
038        private final Method method;
039
040
041        /**
042         * Create a {@code ScheduledMethodRunnable} for the given target instance,
043         * calling the specified method.
044         * @param target the target instance to call the method on
045         * @param method the target method to call
046         */
047        public ScheduledMethodRunnable(Object target, Method method) {
048                this.target = target;
049                this.method = method;
050        }
051
052        /**
053         * Create a {@code ScheduledMethodRunnable} for the given target instance,
054         * calling the specified method by name.
055         * @param target the target instance to call the method on
056         * @param methodName the name of the target method
057         * @throws NoSuchMethodException if the specified method does not exist
058         */
059        public ScheduledMethodRunnable(Object target, String methodName) throws NoSuchMethodException {
060                this.target = target;
061                this.method = target.getClass().getMethod(methodName);
062        }
063
064
065        /**
066         * Return the target instance to call the method on.
067         */
068        public Object getTarget() {
069                return this.target;
070        }
071
072        /**
073         * Return the target method to call.
074         */
075        public Method getMethod() {
076                return this.method;
077        }
078
079
080        @Override
081        public void run() {
082                try {
083                        ReflectionUtils.makeAccessible(this.method);
084                        this.method.invoke(this.target);
085                }
086                catch (InvocationTargetException ex) {
087                        ReflectionUtils.rethrowRuntimeException(ex.getTargetException());
088                }
089                catch (IllegalAccessException ex) {
090                        throw new UndeclaredThrowableException(ex);
091                }
092        }
093
094        @Override
095        public String toString() {
096                return this.method.getDeclaringClass().getName() + "." + this.method.getName();
097        }
098
099}