直播平台搭建,Scheduler 动态定时任务
直播平台搭建,Scheduler 动态定时任务
/**
* 定时任务管理类
*
* @author
*
*/
public class QuartzManager {
static Logger logger = Logger.getLogger("QuartzManager");
// 创建一个SchedulerFactory工厂实例
private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
// 任务组
private static String JOB_GROUP_NAME = "JOBGROUP_NAME";
// 触发器组
private static String TRIGGER_GROUP_NAME = "TRIGGERGROUP_NAME";
/**
* 添加定时周期任务。时间:秒
*
* @param JOBID
* @param jobClass
* @param seconds
* @param repeatCount
* @param delayed
* @param parameter
*/
public static void addJob(String JOBID, Class<? extends Job> jobClass, int seconds, int repeatCount, int delayed,Map<String, Object> parameter) {
try {
//开始时间
Date startTime = new Date(System.currentTimeMillis() + delayed * 1000);
JobKey jobkey = new JobKey(JOBID, TRIGGER_GROUP_NAME);
Scheduler sched = gSchedulerFactory.getScheduler();
boolean exists = sched.checkExists(jobkey);
if(exists){
logger.info(JOBID+" 任务已存在");
return;
}
// 任务名,任务组,任务执行类
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(JOBID, TRIGGER_GROUP_NAME).requestRecovery().build();
// 传参数
jobDetail.getJobDataMap().put("parameterList", parameter);
// 触发器
SimpleScheduleBuilder builder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(seconds).withRepeatCount(repeatCount);
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(JOBID, TRIGGER_GROUP_NAME).startAt(startTime).withSchedule(builder).build();
sched.scheduleJob(jobDetail, trigger);
if (!sched.isShutdown()) {
// 延迟启动
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 添加定时一次性任务
*
* @param JOBID
* @param jobClass
* @param time
* @param parameter
*/
public static void addOnetimeJob(String JOBID, Class<? extends Job> jobClass, String time,Map<String, Object> parameter) {
try {
//开始时间
Date startTime = DateUtil.fomatDateSs(time);
JobKey jobkey = new JobKey(JOBID, TRIGGER_GROUP_NAME);
Scheduler sched = gSchedulerFactory.getScheduler();
boolean exists = sched.checkExists(jobkey);
if(exists){
logger.info(JOBID+" 任务已存在1");
return;
}
// 任务名,任务组,任务执行类
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(JOBID, TRIGGER_GROUP_NAME).requestRecovery().build();
// 传参数
jobDetail.getJobDataMap().put("parameterList", parameter);
// 触发器
SimpleScheduleBuilder builder = SimpleScheduleBuilder.simpleSchedule();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(JOBID, TRIGGER_GROUP_NAME).startAt(startTime).withSchedule(builder).build();
sched.scheduleJob(jobDetail, trigger);
if (!sched.isShutdown()) {
// 延迟启动
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 移除一个任务(使用默认的任务组名,触发器名,触发器组名)
*
* @param jobName
* 任务名称
*/
public static void removeJob(String jobName) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
// 通过触发器名和组名获取TriggerKey
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, TRIGGER_GROUP_NAME);
// 通过任务名和组名获取JobKey
JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME);
sched.pauseJob(jobKey);
// 停止触发器
sched.pauseTrigger(triggerKey);
// 移除触发器
sched.unscheduleJob(triggerKey);
// 删除任务
sched.deleteJob(jobKey);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
以上就是 直播平台搭建,Scheduler 动态定时任务,更多内容欢迎关注之后的文章