package com.huatek.busi.service.impl.job;
import java.lang.reflect.Method;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;

/**
  * @ClassName: ScheduleRunnable
  * @FullClassPath:com.huatek.busi.service.impl.job.ScheduleRunnable
  * @Description: 执行定时任务
  * @author: Arno
  * @date: Feb 12, 2018 10:50:56 AM
  * @version: 1.0
  */

public class ScheduleRunnable implements Runnable {
	
	
	 protected final static Logger logger = LoggerFactory.getLogger(ScheduleRunnable.class);
	 
	private Object target;
	private Method method;
	private String params;
	
	public ScheduleRunnable(String beanName, String methodName, String params) throws NoSuchMethodException, SecurityException {
		this.target = SpringContextUtils.getBean(beanName);
		this.params = params;
		
		if(StringUtils.isNotBlank(params)){
		    this.method = ReflectionUtils.findMethod(target.getClass(), methodName, String.class);
		}else{
			this.method = ReflectionUtils.findMethod(target.getClass(), methodName);
		}
	}

	@Override
	public void run() {
		try {
			ReflectionUtils.makeAccessible(method);
			if(StringUtils.isNotBlank(params)){
				method.invoke(target, params);
			}else{
				method.invoke(target);
			}
		}catch (Exception e) {
			logger.error("执行定时任务失败---[{}]", e.getMessage());
			if(logger.isDebugEnabled()) {
			    logger.error(e.getMessage(), e);
			}
		}
	}

}
