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"); } }
时间: 2024-10-27 09:37:18