quartz 实现调度任务 SchedulerManager
package cn.com.do1.component.common.util;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import java.text.ParseException;
/**
* Created by ao.ouyang on 2015/8/3.
* 任务调度管理
*/
public class SchedulerManager {
private static SchedulerFactory sf = new StdSchedulerFactory();
private static String JOB_GROUP_NAME = "group1";
private static String TRIGGER_GROUP_NAME = "trigger1";
/** *//**
* 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
* @param jobName 任务名
* @param job 任务
* @param time 时间设置,参考quartz说明文档
* @throws SchedulerException
* @throws ParseException
*/
public static void addJob(String jobName,Job job,String time)
throws SchedulerException, ParseException {
Scheduler sched = sf.getScheduler();
JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, job.getClass());//任务名,任务组,任务执行类
//触发器
CronTrigger trigger =
new CronTrigger(jobName, TRIGGER_GROUP_NAME);//触发器名,触发器组
trigger.setCronExpression(time);//触发器时间设定
sched.scheduleJob(jobDetail,trigger);
//启动
if(!sched.isShutdown())
sched.start();
}
/** *//**
* 添加一个定时任务
* @param jobName 任务名
* @param jobGroupName 任务组名
* @param triggerName 触发器名
* @param triggerGroupName 触发器组名
* @param job 任务
* @param time 时间设置,参考quartz说明文档
* @throws SchedulerException
* @throws ParseException
*/
public static void addJob(String jobName,String jobGroupName,
String triggerName,String triggerGroupName,
Job job,String time)
throws SchedulerException, ParseException{
Scheduler sched = sf.getScheduler();
JobDetail jobDetail = new JobDetail(jobName, jobGroupName, job.getClass());//任务名,任务组,任务执行类
//触发器
CronTrigger trigger =
new CronTrigger(triggerName, triggerGroupName);//触发器名,触发器组
trigger.setCronExpression(time);//触发器时间设定
sched.scheduleJob(jobDetail,trigger);
if(!sched.isShutdown())
sched.start();
}
/** *//**
* 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)
* @param jobName
* @param time
* @throws SchedulerException
* @throws ParseException
*/
public static void modifyJobTime(String jobName,String time)
throws SchedulerException, ParseException{
Scheduler sched = sf.getScheduler();
Trigger trigger = sched.getTrigger(jobName,TRIGGER_GROUP_NAME);
if(trigger != null){
CronTrigger ct = (CronTrigger)trigger;
ct.setCronExpression(time);
sched.resumeTrigger(jobName,TRIGGER_GROUP_NAME);
}
}
/** *//**
* 修改一个任务的触发时间
* @param triggerName
* @param triggerGroupName
* @param time
* @throws SchedulerException
* @throws ParseException
*/
public static void modifyJobTime(String triggerName,String triggerGroupName,
String time)
throws SchedulerException, ParseException{
Scheduler sched = sf.getScheduler();
Trigger trigger = sched.getTrigger(triggerName,triggerGroupName);
if(trigger != null){
CronTrigger ct = (CronTrigger)trigger;
//修改时间
ct.setCronExpression(time);
//重启触发器
sched.resumeTrigger(triggerName,triggerGroupName);
}
}
/** *//**
* 移除一个任务(使用默认的任务组名,触发器名,触发器组名)
* @param jobName
* @throws SchedulerException
*/
public static void removeJob(String jobName)
throws SchedulerException{
Scheduler sched = sf.getScheduler();
sched.pauseTrigger(jobName,TRIGGER_GROUP_NAME);//停止触发器
sched.unscheduleJob(jobName,TRIGGER_GROUP_NAME);//移除触发器
sched.deleteJob(jobName,JOB_GROUP_NAME);//删除任务
}
/** *//**
* 移除一个任务
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
* @throws SchedulerException
*/
public static void removeJob(String jobName,String jobGroupName,
String triggerName,String triggerGroupName)
throws SchedulerException{
Scheduler sched = sf.getScheduler();
sched.pauseTrigger(triggerName,triggerGroupName);//停止触发器
sched.unscheduleJob(triggerName,triggerGroupName);//移除触发器
sched.deleteJob(jobName,jobGroupName);//删除任务
}
}
Job.class
package cn.com.do1.component.common.job;
import cn.com.do1.component.common.jdbc.ISimpleJdbcTemplateDao;
import cn.com.do1.component.common.util.Constant;
import cn.com.do1.component.yopin.service.ICouponsService;
import cn.com.do1.dqdp.core.ConfigLoadExcetion;
import cn.com.do1.dqdp.core.DqdpAppContext;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.List;
import java.util.Map;
/**
* Created by ao.ouyang on 2015/8/3.
* 订单job 1.定时更新代付订单的超时状态
*/
public class OrderJob implements Job {
private ISimpleJdbcTemplateDao simpleJdbcTemplateDao;
private ICouponsService couponsService;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
simpleJdbcTemplateDao = (ISimpleJdbcTemplateDao) DqdpAppContext.getSpringContext().getBean("simpleJdbcDao");
couponsService = (ICouponsService) DqdpAppContext.getSpringContext().getBean("iCouponsService");
try {
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() + ")");
if(list.size()>0){
for (Map map : list){
String usrId = map.get("USR_ID").toString();
couponsService.refundCash( map.get("ORDER_CODE").toString(),usrId);
//更新超过5天未代付完成的订单为已超时
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()+"'");
}
}
} catch (Exception e) {
} catch (ConfigLoadExcetion configLoadExcetion) {
configLoadExcetion.printStackTrace();
}
}
}
posted on 2017-03-21 17:40 signheart 阅读(1132) 评论(0) 编辑 收藏 举报