实现Quartz的动态增删改查

Posted 勿忘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实现Quartz的动态增删改查相关的知识,希望对你有一定的参考价值。

1. Maven依赖

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
2. 业务处理类:
import org.quartz.*;
import java.text.SimpleDateFormat;
/**
*
* Created by l1 on 2016/10/8.
*/
class MyJob implements Job{
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
// job 的名字
String jobName = context.getJobDetail().getKey().getName();

// 任务执行的时间
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒");
String jobRunTime = dateFormat.format(java.util.Calendar.getInstance().getTime());

// 输出任务执行情况
System.out.println("任务 : " + jobName + " 在 " +jobRunTime + " 执行了 ");
}
}
 
3. Quartz管理类:
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.concurrent.TimeUnit;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

/**
*
* Created by l1 on 2016/10/8.
*/
public class QuartzManager {

private static final String JOB_GROUP_NAME = "JOB_GROUP_NAME";
private static final String TRIGGER_GROUP_NAME = "TRIGGER_GROUP_NAME";

/**
* 添加定时任务
*/
public static void addJob(String jobName,String jobGroupName,String triggerName,String triggerGroupName,Class cls,String time){
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
JobDetail jobDetail = newJob(cls).withIdentity(jobName, jobGroupName).build();
CronTrigger cronTrigger
= newTrigger().withIdentity(triggerName, triggerGroupName).withSchedule(cronSchedule(time)).build();
scheduler.scheduleJob(jobDetail,cronTrigger);

} catch (SchedulerException e) {
e.printStackTrace();
}
}
public static void addJob(String jobName,String triggerName,String groupName,Class cls,String time){
addJob(jobName,groupName,triggerName,groupName,cls,time);
}
public static void addJob(String jobName,String triggerName,Class cls,String time){
addJob(jobName,JOB_GROUP_NAME,triggerName,TRIGGER_GROUP_NAME,cls,time);
}

/**
* 删除定时任务
*/
public static boolean deleteJob(String jobName,String groupName){
boolean result = false;
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobKey jobKey = new JobKey(jobName,groupName);
if(scheduler.checkExists(jobKey)){
result = scheduler.deleteJob(jobKey);
}
} catch (SchedulerException e) {
e.printStackTrace();
}
return result;
}
public static void deleteJob(String jobName){
deleteJob(jobName,JOB_GROUP_NAME);
}

/**
* 停止任务
*/
public static void pauseJob(String jobName,String groupName){
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobKey jobKey = new JobKey(jobName,groupName);
if(scheduler.checkExists(jobKey)){
scheduler.pauseJob(jobKey);
}
} catch (SchedulerException e) {
e.printStackTrace();
}
}
public static void pauseJob(String jobName){
pauseJob(jobName,JOB_GROUP_NAME);
}
/**
* 再次启动Job
* @param jobName
* @param groupName
*/
public static void resumeJob(String jobName,String groupName){
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobKey jobKey = new JobKey(jobName,groupName);
if(scheduler.checkExists(jobKey)){
scheduler.resumeJob(jobKey);
}
} catch (SchedulerException e) {
e.printStackTrace();
}
}
public static void resumeJob(String jobName){
resumeJob(jobName,JOB_GROUP_NAME);
}
public static void main(String[] args) {
QuartzManager.addJob("job1","trigger1",MyJob.class,"0/15 * * * * ?");
try {
TimeUnit.SECONDS.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
QuartzManager.deleteJob("job1");
}

}
 
 

以上是关于实现Quartz的动态增删改查的主要内容,如果未能解决你的问题,请参考以下文章

spring boot 整合 quartz 集群环境 实现 动态定时任务配置原

基于JdbcTemplate 的动态数据,增删改查功能实现

动态实现类(对数据库的增删改查)

Quartz+Spring Boot实现动态管理定时任务

C语言顺序表的动态存储:增删改查的实现

基于mybatis的动态表数据增删改查方法实现