package com.qmth.boot.redis.schedule;

import org.springframework.util.ReflectionUtils;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;

public class SpringTaskRunnable implements Runnable, Serializable {

    private static final long serialVersionUID = -5132210791656110937L;

    private Class<?> beanType;

    private String methodName;

    public Class<?> getBeanType() {
        return beanType;
    }

    public void setBeanType(Class<?> beanType) {
        this.beanType = beanType;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    @Override
    public void run() {
        try {
            Object bean = RedisTaskScheduler.applicationContext.getBean(beanType);
            Method method = bean.getClass().getMethod(methodName);
            ReflectionUtils.makeAccessible(method);
            method.invoke(bean);
        } catch (InvocationTargetException e1) {
            ReflectionUtils.rethrowRuntimeException(e1.getTargetException());
        } catch (IllegalAccessException e2) {
            throw new UndeclaredThrowableException(e2);
        } catch (Exception e3) {
            ReflectionUtils.rethrowRuntimeException(e3);
        }
    }
}
