Quartz-第四篇 常规quartz的使用

Posted zeromz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Quartz-第四篇 常规quartz的使用相关的知识,希望对你有一定的参考价值。

1、目录结构

   技术图片

 

2、主要文件 

  1》引入的jar包,quartz-2.2.2解压后lib下所有的jar包

  2》quartz.properties

org.quartz.threadPool.threadCount: 2
org.quartz.jobStore.misfireThreshold: 4000

  3》SchedulerUtil.java

package com.lfy.scheduler;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;

public class SchedulerUtil {
    
    private static Scheduler scheduler;
    
    private static CronTrigger cronTrigger;
    
    private static JobDetail jobDetail;

    public static Scheduler getScheduler() {
        return scheduler;
    }

    public static void setScheduler(Scheduler scheduler) {
        SchedulerUtil.scheduler = scheduler;
    }

    public static CronTrigger getCronTrigger() {
        return cronTrigger;
    }

    public static void setCronTrigger(CronTrigger cronTrigger) {
        SchedulerUtil.cronTrigger = cronTrigger;
    }

    public static JobDetail getJobDetail() {
        return jobDetail;
    }

    public static void setJobDetail(JobDetail jobDetail) {
        SchedulerUtil.jobDetail = jobDetail;
    }
}

   4》HelloJob.java

package com.lfy.jobdetail;


import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class HelloJob implements Job {

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("现在的时间是:"+ sf.format(date));
        //具体的业务逻辑
        System.out.println("Hello Quartz");
    }
}

   5》CronTriggerScheduleApp.java

package com.lfy.controller;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lfy.jobdetail.HelloJob;
import com.lfy.scheduler.SchedulerUtil;

@Controller
@RequestMapping("/App")
public class CronTriggerScheduleApp {

    @RequestMapping(value="/startQuartz")
    public String startQuartz() throws SchedulerException {
        
        SchedulerUtil.setScheduler(new StdSchedulerFactory().getScheduler());
        
        SchedulerUtil.setJobDetail(JobBuilder.newJob(HelloJob.class)
                                             .withIdentity("cronJob","group1")
                                             .build());
        
        CronScheduleBuilder cronScheduleBuilder=CronScheduleBuilder.cronSchedule("/6 * * * * ? *");
        cronScheduleBuilder.withMisfireHandlingInstructionDoNothing();
        SchedulerUtil.setCronTrigger(TriggerBuilder.newTrigger()
                                                   .withIdentity("cronTrigger")
                                                   .withSchedule(cronScheduleBuilder)
                                                   .build());
        SchedulerUtil.getScheduler().start();
        SchedulerUtil.getScheduler().scheduleJob(SchedulerUtil.getJobDetail(), 
                                                 SchedulerUtil.getCronTrigger());
        return "index";
    }
}

   6》PauseController.java

package com.lfy.controller;

import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lfy.scheduler.SchedulerUtil;

@Controller
@RequestMapping("/PauseController")
public class PauseController {

    @RequestMapping(value="/pause")
    public void pauseCronTrigger() {
        
        JobKey jobkey=JobKey.jobKey("cronJob","group1");
        Scheduler scheduler=SchedulerUtil.getScheduler();
        try {
            scheduler.pauseJob(jobkey);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}

   7》ResumeController.java

package com.lfy.controller;

import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lfy.scheduler.SchedulerUtil;

@Controller
@RequestMapping("/ResumeController")
public class ResumeController {

    @RequestMapping(value="/resume")
    public void resumeCronTrigger() {
        
        JobKey jobkey=JobKey.jobKey("cronJob","group1");
        Scheduler scheduler=SchedulerUtil.getScheduler();
        try {
            scheduler.resumeJob(jobkey);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}

   8》AlterTriggerTimeController.java

package com.lfy.controller;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.quartz.TriggerKey;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lfy.scheduler.SchedulerUtil;

@Controller
@RequestMapping("/RescheduleController")
public class AlterTriggerTimeController {

    @RequestMapping(value="/reschedule")
    public void rescheduleCronTrigger() {
        
        String newCron="/3 * * * * ? *";
        TriggerKey triggerKey = TriggerKey.triggerKey("cronJob","group1");
         
        try {
            Scheduler sched = SchedulerUtil.getScheduler();
            CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey);
            if (trigger == null) {
                return;
            }
            String oldTime = trigger.getCronExpression();
            if (!oldTime.equalsIgnoreCase(newCron)) {
                CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(newCron);
                // 按新的cronExpression表达式重新构建trigger
                trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
                // 按新的trigger重新设置job执行
                sched.rescheduleJob(triggerKey, trigger);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

   9》DeleteController.java

package com.lfy.controller;

import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.TriggerKey;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lfy.scheduler.SchedulerUtil;

@Controller
@RequestMapping("/DeleteController")
public class DeleteController {

    @RequestMapping(value="/delete")
    public void deleteCronTrigger() {
        
        TriggerKey triggerKey = TriggerKey.triggerKey("cronJob","group1");
        JobKey jobKey = JobKey.jobKey("cronJob","group1");
        try {
            Scheduler sched = SchedulerUtil.getScheduler();
            sched.pauseTrigger(triggerKey);// 停止触发器
            sched.unscheduleJob(triggerKey);// 移除触发器
            sched.deleteJob(jobKey);// 删除任务
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

 

以上是关于Quartz-第四篇 常规quartz的使用的主要内容,如果未能解决你的问题,请参考以下文章

Spring 3整合Quartz 2实现定时任务一:常规整合 (基于maven构建) - mjorcen(转)

Spring+Quartz 整合一:常规整合

Quartz 框架 教程(中文版)2.2.x

Quartz定时任务调度机制解析(CronTirggerSimpleTrigger )

Quartz使用一

我在java项目中加了spring支持,加了quartz-1.6.1.jar包,在使用quartz实现任务调用时的代码如下: