ScheduleRunnable.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Copyright 2018
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. * <p>
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.warewms.job.utils;
  17. import com.warewms.common.exception.RRException;
  18. import com.warewms.common.utils.SpringContextUtils;
  19. import org.apache.commons.lang.StringUtils;
  20. import org.springframework.util.ReflectionUtils;
  21. import java.lang.reflect.Method;
  22. /**
  23. * 执行定时任务
  24. *
  25. * @author Mark sunlightcs@gmail.com
  26. * @since 1.2.0 2016-11-28
  27. */
  28. public class ScheduleRunnable implements Runnable {
  29. private Object target;
  30. private Method method;
  31. private String params;
  32. public ScheduleRunnable(String beanName, String methodName, String params) throws NoSuchMethodException, SecurityException {
  33. this.target = SpringContextUtils.getBean(beanName);
  34. this.params = params;
  35. if(StringUtils.isNotBlank(params)){
  36. this.method = target.getClass().getDeclaredMethod(methodName, String.class);
  37. }else{
  38. this.method = target.getClass().getDeclaredMethod(methodName);
  39. }
  40. }
  41. @Override
  42. public void run() {
  43. try {
  44. ReflectionUtils.makeAccessible(method);
  45. if(StringUtils.isNotBlank(params)){
  46. method.invoke(target, params);
  47. }else{
  48. method.invoke(target);
  49. }
  50. }catch (Exception e) {
  51. throw new RRException("执行定时任务失败", e);
  52. }
  53. }
  54. }