ScheduleRunnable.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package com.kmall.schedule.utils;
  2. import com.kmall.common.utils.RRException;
  3. import com.kmall.common.utils.SpringContextUtils;
  4. import org.apache.commons.lang.StringUtils;
  5. import org.springframework.util.ReflectionUtils;
  6. import java.lang.reflect.Method;
  7. /**
  8. * 执行定时任务
  9. *
  10. * @author Scott
  11. * @email
  12. * @date 2016年11月30日 下午12:49:33
  13. */
  14. public class ScheduleRunnable implements Runnable {
  15. private Object target;
  16. private Method method;
  17. private String params;
  18. public ScheduleRunnable(String beanName, String methodName, String params) throws NoSuchMethodException, SecurityException {
  19. this.target = SpringContextUtils.getBean(beanName);
  20. this.params = params;
  21. if (StringUtils.isNotBlank(params)) {
  22. this.method = target.getClass().getDeclaredMethod(methodName, String.class);
  23. } else {
  24. this.method = target.getClass().getDeclaredMethod(methodName);
  25. }
  26. }
  27. @Override
  28. public void run() {
  29. try {
  30. ReflectionUtils.makeAccessible(method);
  31. if (StringUtils.isNotBlank(params)) {
  32. method.invoke(target, params);
  33. } else {
  34. method.invoke(target);
  35. }
  36. } catch (Exception e) {
  37. throw new RRException("执行定时任务失败", e);
  38. }
  39. }
  40. }