ScheduleRunnable.java 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package com.ruoyi.quartz.util;
  2. import java.lang.reflect.Method;
  3. import org.springframework.util.ReflectionUtils;
  4. import com.ruoyi.common.exception.BusinessException;
  5. import com.ruoyi.common.utils.StringUtils;
  6. import com.ruoyi.common.utils.spring.SpringUtils;
  7. /**
  8. * 执行定时任务
  9. *
  10. * @author ruoyi
  11. *
  12. */
  13. public class ScheduleRunnable implements Runnable
  14. {
  15. private Object target;
  16. private Method method;
  17. private String params;
  18. public ScheduleRunnable(String beanName, String methodName, String params)
  19. throws NoSuchMethodException, SecurityException
  20. {
  21. this.target = SpringUtils.getBean(beanName);
  22. this.params = params;
  23. if (StringUtils.isNotEmpty(params))
  24. {
  25. this.method = target.getClass().getDeclaredMethod(methodName, String.class);
  26. }
  27. else
  28. {
  29. this.method = target.getClass().getDeclaredMethod(methodName);
  30. }
  31. }
  32. @Override
  33. public void run()
  34. {
  35. try
  36. {
  37. ReflectionUtils.makeAccessible(method);
  38. if (StringUtils.isNotEmpty(params))
  39. {
  40. method.invoke(target, params);
  41. }
  42. else
  43. {
  44. method.invoke(target);
  45. }
  46. }
  47. catch (Exception e)
  48. {
  49. throw new BusinessException("执行定时任务失败", e);
  50. }
  51. }
  52. }