quartz 实现调度任务 SchedulerManager

  1. package cn.com.do1.component.common.util;
  2. import org.quartz.*;
  3. import org.quartz.impl.StdSchedulerFactory;
  4. import java.text.ParseException;
  5. /**
  6. * Created by ao.ouyang on 2015/8/3.
  7. * 任务调度管理
  8. */
  9. public class SchedulerManager {
  10. private static SchedulerFactory sf = new StdSchedulerFactory();
  11. private static String JOB_GROUP_NAME = "group1";
  12. private static String TRIGGER_GROUP_NAME = "trigger1";
  13. /** *//**
  14. * 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
  15. * @param jobName 任务名
  16. * @param job 任务
  17. * @param time 时间设置,参考quartz说明文档
  18. * @throws SchedulerException
  19. * @throws ParseException
  20. */
  21. public static void addJob(String jobName,Job job,String time)
  22. throws SchedulerException, ParseException {
  23. Scheduler sched = sf.getScheduler();
  24. JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, job.getClass());//任务名,任务组,任务执行类
  25. //触发器
  26. CronTrigger trigger =
  27. new CronTrigger(jobName, TRIGGER_GROUP_NAME);//触发器名,触发器组
  28. trigger.setCronExpression(time);//触发器时间设定
  29. sched.scheduleJob(jobDetail,trigger);
  30. //启动
  31. if(!sched.isShutdown())
  32. sched.start();
  33. }
  34. /** *//**
  35. * 添加一个定时任务
  36. * @param jobName 任务名
  37. * @param jobGroupName 任务组名
  38. * @param triggerName 触发器名
  39. * @param triggerGroupName 触发器组名
  40. * @param job 任务
  41. * @param time 时间设置,参考quartz说明文档
  42. * @throws SchedulerException
  43. * @throws ParseException
  44. */
  45. public static void addJob(String jobName,String jobGroupName,
  46. String triggerName,String triggerGroupName,
  47. Job job,String time)
  48. throws SchedulerException, ParseException{
  49. Scheduler sched = sf.getScheduler();
  50. JobDetail jobDetail = new JobDetail(jobName, jobGroupName, job.getClass());//任务名,任务组,任务执行类
  51. //触发器
  52. CronTrigger trigger =
  53. new CronTrigger(triggerName, triggerGroupName);//触发器名,触发器组
  54. trigger.setCronExpression(time);//触发器时间设定
  55. sched.scheduleJob(jobDetail,trigger);
  56. if(!sched.isShutdown())
  57. sched.start();
  58. }
  59. /** *//**
  60. * 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)
  61. * @param jobName
  62. * @param time
  63. * @throws SchedulerException
  64. * @throws ParseException
  65. */
  66. public static void modifyJobTime(String jobName,String time)
  67. throws SchedulerException, ParseException{
  68. Scheduler sched = sf.getScheduler();
  69. Trigger trigger = sched.getTrigger(jobName,TRIGGER_GROUP_NAME);
  70. if(trigger != null){
  71. CronTrigger ct = (CronTrigger)trigger;
  72. ct.setCronExpression(time);
  73. sched.resumeTrigger(jobName,TRIGGER_GROUP_NAME);
  74. }
  75. }
  76. /** *//**
  77. * 修改一个任务的触发时间
  78. * @param triggerName
  79. * @param triggerGroupName
  80. * @param time
  81. * @throws SchedulerException
  82. * @throws ParseException
  83. */
  84. public static void modifyJobTime(String triggerName,String triggerGroupName,
  85. String time)
  86. throws SchedulerException, ParseException{
  87. Scheduler sched = sf.getScheduler();
  88. Trigger trigger = sched.getTrigger(triggerName,triggerGroupName);
  89. if(trigger != null){
  90. CronTrigger ct = (CronTrigger)trigger;
  91. //修改时间
  92. ct.setCronExpression(time);
  93. //重启触发器
  94. sched.resumeTrigger(triggerName,triggerGroupName);
  95. }
  96. }
  97. /** *//**
  98. * 移除一个任务(使用默认的任务组名,触发器名,触发器组名)
  99. * @param jobName
  100. * @throws SchedulerException
  101. */
  102. public static void removeJob(String jobName)
  103. throws SchedulerException{
  104. Scheduler sched = sf.getScheduler();
  105. sched.pauseTrigger(jobName,TRIGGER_GROUP_NAME);//停止触发器
  106. sched.unscheduleJob(jobName,TRIGGER_GROUP_NAME);//移除触发器
  107. sched.deleteJob(jobName,JOB_GROUP_NAME);//删除任务
  108. }
  109. /** *//**
  110. * 移除一个任务
  111. * @param jobName
  112. * @param jobGroupName
  113. * @param triggerName
  114. * @param triggerGroupName
  115. * @throws SchedulerException
  116. */
  117. public static void removeJob(String jobName,String jobGroupName,
  118. String triggerName,String triggerGroupName)
  119. throws SchedulerException{
  120. Scheduler sched = sf.getScheduler();
  121. sched.pauseTrigger(triggerName,triggerGroupName);//停止触发器
  122. sched.unscheduleJob(triggerName,triggerGroupName);//移除触发器
  123. sched.deleteJob(jobName,jobGroupName);//删除任务
  124. }
  125. }

Job.class

  1. package cn.com.do1.component.common.job;
  2. import cn.com.do1.component.common.jdbc.ISimpleJdbcTemplateDao;
  3. import cn.com.do1.component.common.util.Constant;
  4. import cn.com.do1.component.yopin.service.ICouponsService;
  5. import cn.com.do1.dqdp.core.ConfigLoadExcetion;
  6. import cn.com.do1.dqdp.core.DqdpAppContext;
  7. import org.quartz.Job;
  8. import org.quartz.JobExecutionContext;
  9. import org.quartz.JobExecutionException;
  10. import java.util.List;
  11. import java.util.Map;
  12. /**
  13. * Created by ao.ouyang on 2015/8/3.
  14. * 订单job 1.定时更新代付订单的超时状态
  15. */
  16. public class OrderJob implements Job {
  17. private ISimpleJdbcTemplateDao simpleJdbcTemplateDao;
  18. private ICouponsService couponsService;
  19. @Override
  20. public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
  21. simpleJdbcTemplateDao = (ISimpleJdbcTemplateDao) DqdpAppContext.getSpringContext().getBean("simpleJdbcDao");
  22. couponsService = (ICouponsService) DqdpAppContext.getSpringContext().getBean("iCouponsService");
  23. try {
  24. List<Map<String, Object>> list = simpleJdbcTemplateDao.queryForList("select * from tb_91_order_info where datediff(NOW(),CREATE_TIME) >5 and ORDER_STATUS in (" + cn.com.do1.component.yopin.util.Constant.ORDER_STATUS.NO_PAY.value() + "," + cn.com.do1.component.yopin.util.Constant.ORDER_STATUS.IN.value() + ")");
  25. if(list.size()>0){
  26. for (Map map : list){
  27. String usrId = map.get("USR_ID").toString();
  28. couponsService.refundCash( map.get("ORDER_CODE").toString(),usrId);
  29. //更新超过5天未代付完成的订单为已超时
  30. simpleJdbcTemplateDao.update("update tb_91_order_info set ORDER_STATUS = " + cn.com.do1.component.yopin.util.Constant.ORDER_STATUS.TIME_OUT.value() + " where ORDER_CODE='"+ map.get("ORDER_CODE").toString()+"'");
  31. }
  32. }
  33. } catch (Exception e) {
  34. } catch (ConfigLoadExcetion configLoadExcetion) {
  35. configLoadExcetion.printStackTrace();
  36. }
  37. }
  38. }

posted on 2017-03-21 17:40  signheart  阅读(1132)  评论(0编辑  收藏  举报

导航