定时任务-灵活时间
1,action(创建任务)
//任务名:使用MD5(字符串appid+当前时间毫秒数)生成一个16位字符串
String jobName = MD5Util.getMD5(r+""+new Date().getTime()).substring(8,24);
//格式化quartz时间格式
String jobTime = DateUtil.date2Quartz(isDelayLine, DateUtil.DATE_TIME_FORMAT);
//添加定时任务
JobDataMap map = new JobDataMap();
map.put("appService", appService);
map.put("appService", appService);
QuartzManager.addJob(jobName, AppCronJob.class, jobTime,map);
//任务记录在数据库
appService.addCronJob(r, app.getAppname(), jobName, jobTime);
2,任务体(任务内容逻辑)
/**
- @Description:
- @Title: QuartzJob.java
- @Package com.joyce.quartz
- @Copyright: Copyright (c) 2014
- @author Comsys-LZP
- @date 2014-6-26 下午03:37:11
- @version V2.0
*/
package com.emind.app.entity;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.emind.app.dao.EstoreAppInfoDao;
import com.emind.app.dao.EstoreCronJobDao;
import com.emind.app.util.SendEmail;
import com.emind.app.util.SendMessage;
/**
@Description: 自动上线app任务执行类
-
@ClassName: AppCronJob
*/
public class AppCronJob implements Job {private String jobName;
private EstoreCronJobDao cronDao;
private EstoreAppInfoDao appInfoDao;@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
//初始化本类
setCronJob(context);//找到任务 EstoreCronJob job = cronDao.queryOneJob(jobName); if(job == null) return; //判断app是否审核通过 boolean pass = appInfoDao.isAuditPass(job.getAppid()); //上架app(设置app发布状态) if(pass){ appInfoDao.publishAppStatus(job.getAppid(),job.getAppid());
// SendEmail.sendAppOnlineEmail(email, date, proName, appName, sdkService, emindEmail);
// SendMessage.sendAppOnlineMessage(phone, proName, appName);
}
//数据库删除任务记录
cronDao.deleteOneJob(jobName);
//删除定时任务
QuartzManager.removeJob(jobName);
}
protected void setCronJob(JobExecutionContext context){
JobDetail job = context.getJobDetail();
JobDataMap map = job.getJobDataMap();
this.jobName = job.getName();
this.cronDao = (EstoreCronJobDao) map.get("cronDao");
this.appInfoDao = (EstoreAppInfoDao) map.get("appInfoDao");
}
}
3,工具类
/**
- @Description:
- @Title: QuartzManager.java
- @Package com.joyce.quartz
- @Copyright: Copyright (c) 2014
- @author Comsys-LZP
- @date 2014-6-26 下午03:15:52
- @version V2.0
*/
package com.emind.app.entity;
import java.util.HashMap;
import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
/**
@Description: 定时任务管理类
@ClassName: QuartzManager
@Copyright: Copyright (c) 2014
@author Comsys-LZP
@date 2014-6-26 下午03:15:52
-
@version V2.0
*/
public class QuartzManager {
private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME";
private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME";/**
@Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
@param jobName
任务名
@param cls
任务
@param time
时间设置,参考quartz说明文档
@Title: QuartzManager.java
@Copyright: Copyright (c) 2014
@author Comsys-LZP
@date 2014-6-26 下午03:47:44
-
@version V2.0
*/
public static void addJob(String jobName, Class cls, String time,JobDataMap map) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, cls);// 任务名,任务组,任务执行类jobDetail.setJobDataMap(map); // 触发器 CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);// 触发器名,触发器组 trigger.setCronExpression(time);// 触发器时间设定 sched.scheduleJob(jobDetail, trigger); // 启动 if (!sched.isShutdown()) { sched.start(); }
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
- @Description: 添加一个定时任务
- @param jobName
任务名
- @param jobGroupName
任务组名
- @param triggerName
触发器名
- @param triggerGroupName
触发器组名
- @param jobClass
任务
- @param time
时间设置,参考quartz说明文档
- @Title: QuartzManager.java
- @Copyright: Copyright (c) 2014
- @author Comsys-LZP
- @date 2014-6-26 下午03:48:15
- @version V2.0
*/
@SuppressWarnings("unchecked")
public static void addJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName, Class jobClass,
String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass);// 任务名,任务组,任务执行类
// 触发器
CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 触发器名,触发器组
trigger.setCronExpression(time);// 触发器时间设定
sched.scheduleJob(jobDetail, trigger);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
- @Description: 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)
- @param jobName
- @param time
- @Title: QuartzManager.java
- @Copyright: Copyright (c) 2014
- @author Comsys-LZP
- @date 2014-6-26 下午03:49:21
- @version V2.0
*/
@SuppressWarnings("unchecked")
public static void modifyJobTime(String jobName, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
CronTrigger trigger = (CronTrigger) sched.getTrigger(jobName,TRIGGER_GROUP_NAME);
if (trigger == null) {
return;
}
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
JobDetail jobDetail = sched.getJobDetail(jobName,JOB_GROUP_NAME);
Class objJobClass = jobDetail.getJobClass();
removeJob(jobName);
addJob(jobName, objJobClass, time,new JobDataMap());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
- @Description: 修改一个任务的触发时间
- @param triggerName
- @param triggerGroupName
- @param time
- @Title: QuartzManager.java
- @Copyright: Copyright (c) 2014
- @author Comsys-LZP
- @date 2014-6-26 下午03:49:37
- @version V2.0
*/
public static void modifyJobTime(String triggerName,
String triggerGroupName, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName,triggerGroupName);
if (trigger == null) {
return;
}
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
CronTrigger ct = (CronTrigger) trigger;
// 修改时间
ct.setCronExpression(time);
// 重启触发器
sched.resumeTrigger(triggerName, triggerGroupName);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
- @Description: 移除一个任务(使用默认的任务组名,触发器名,触发器组名)
- @param jobName
- @Title: QuartzManager.java
- @Copyright: Copyright (c) 2014
- @author Comsys-LZP
- @date 2014-6-26 下午03:49:51
- @version V2.0
*/
public static void removeJob(String jobName) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.pauseTrigger(jobName, TRIGGER_GROUP_NAME);// 停止触发器
sched.unscheduleJob(jobName, TRIGGER_GROUP_NAME);// 移除触发器
sched.deleteJob(jobName, JOB_GROUP_NAME);// 删除任务
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
- @Description: 移除一个任务
- @param jobName
- @param jobGroupName
- @param triggerName
- @param triggerGroupName
- @Title: QuartzManager.java
- @Copyright: Copyright (c) 2014
- @author Comsys-LZP
- @date 2014-6-26 下午03:50:01
- @version V2.0
*/
public static void removeJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.pauseTrigger(triggerName, triggerGroupName);// 停止触发器
sched.unscheduleJob(triggerName, triggerGroupName);// 移除触发器
sched.deleteJob(jobName, jobGroupName);// 删除任务
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
- @Description:启动所有定时任务
- @Title: QuartzManager.java
- @Copyright: Copyright (c) 2014
- @author Comsys-LZP
- @date 2014-6-26 下午03:50:18
- @version V2.0
*/
public static void startJobs() {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
System.out.println(sched.toString());
sched.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
- @Description:关闭所有定时任务
- @Title: QuartzManager.java
- @Copyright: Copyright (c) 2014
- @author Comsys-LZP
- @date 2014-6-26 下午03:50:26
- @version V2.0
*/
public static void shutdownJobs() {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
if (!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}