quartz(2) -- 入门案例

第一步:添加jar,maven配置


 

<!-- quartz -->
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.1.7</version>
</dependency>

 

第二步:job代码


 

CycleJob

@DisallowConcurrentExecution
public class CycleJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        JobDataMap data = context.getJobDetail().getJobDataMap();
        System.out.println("这是一个周期性执行的job。参数:" + data.getString("key"));
    }
}

StartFixedTimeJob

public class StartFixedTimeJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDataMap data = context.getJobDetail().getJobDataMap();
        System.out.println("这是定时执行的job。参数:" + data.getString("key"));
    }
}

StartNowJob

public class StartNowJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        JobDataMap data = context.getJobDetail().getJobDataMap();
        System.out.println("这是一个立刻执行的job。参数:" + data.getString("key"));
    }
}

SchedulerManager

public class SchedulerManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(SchedulerManager.class);

    private static SchedulerFactory sf = new StdSchedulerFactory();
    private static Scheduler scheduler;
    static{
        try{
            LOGGER.info("------------------------- SchedulerManager.start----------------");
            // 通过SchedulerFactory来获取一个调度器scheduler
            scheduler = sf.getScheduler();
            scheduler.start();
        }
        catch(SchedulerException e){
            LOGGER.error("SchedulerManager.start.error", e);
        }
    }

    public static Scheduler getScheduler() {
        return scheduler;
    }

    public static String getSchedulerName() throws SchedulerException {
        return scheduler.getSchedulerName();
    }

    /**
     * 创建周期执行的job
     * 
     * @param jobId
     * @throws SchedulerException
     */
    public static void createCycleJob(Integer jobId) throws Exception {
        JobKey jobKey = JobKey.jobKey("cycleJob_" + jobId, getSchedulerName());
        if(scheduler.checkExists(jobKey)){
            throw new IllegalStateException("[周期任务JobKey: " + jobKey + "] 已经存在");
        }

        // 作业
        JobDetail job = newJob(CycleJob.class).withIdentity(jobKey).requestRecovery(true).build();

        // 设置job参数
        job.getJobDataMap().put("key", "jobId=" + jobId);

        // 计划表达式 cron 一秒执行一次
        String corn = "* */1 * * * ?";

        // 触发器
        CronTrigger trigger = newTrigger().withIdentity("cycleTrigger_" + jobId, getSchedulerName())
                .withSchedule(cronSchedule(corn)).build();

        // 作业和触发器设置到调度器中
        SchedulerManager.getScheduler().scheduleJob(job, trigger);
    }

    /**
     * 创建立刻执行的job
     * 
     * @param jobId
     * @throws SchedulerException
     */
    public static void createStartNowJob(Integer jobId) throws Exception {
        JobKey jobKey = JobKey.jobKey("startNowJob_" + jobId, getSchedulerName());
        if(scheduler.checkExists(jobKey)){
            throw new IllegalStateException("[立刻执行的JobKey: " + jobKey + "] 已经存在");
        }

        JobDetail job = newJob(StartNowJob.class).withIdentity(jobKey).requestRecovery(true).build();
        job.getJobDataMap().put("key", "jobId=" + jobId);

        Trigger trigger = newTrigger().withIdentity("startNowTrigger" + jobId, getSchedulerName()).startNow().build();

        SchedulerManager.getScheduler().scheduleJob(job, trigger);
    }

    /**
     * 创建定时执行的job
     * 
     * @param jobId
     * @throws SchedulerException
     */
    public static void createStartFixedTimeJob(Integer jobId) throws Exception {
        JobKey jobKey = JobKey.jobKey("startFixedTimeJob_" + jobId, getSchedulerName());
        if(scheduler.checkExists(jobKey)){
            throw new IllegalStateException("[定时执行的JobKey: " + jobKey + "] 已经存在");
        }

        JobDetail job = newJob(StartFixedTimeJob.class).withIdentity(jobKey).requestRecovery(true).build();
        job.getJobDataMap().put("key", "jobId=" + jobId);

        // 定时执行。2017-04-25 09:58:00分执行
        Date date = DateUtils.parseDate("2017-04-25 09:59:00", new String[]{"yyyy-MM-dd hh:mm:ss"});
        Trigger trigger = newTrigger().withIdentity("srartNowTrigger" + jobId, getSchedulerName()).startAt(date)
                .build();
        SchedulerManager.getScheduler().scheduleJob(job, trigger);
    }

    /**
     * 删除job
     * 
     * @param jobId
     * @throws SchedulerException
     */
    public static void stopCycleTaskJob(Integer jobId) throws SchedulerException {
        JobKey jobKey = JobKey.jobKey("cycleJob_" + jobId, getSchedulerName());
        if(scheduler.checkExists(jobKey)){
            scheduler.deleteJob(jobKey);
            LOGGER.info("------SchedulerManager.stopCycleTaskJob: delete the job jobKey [" + jobKey + "]");
        }
    }
}

 

 

第三步:测试类


QuartzTest

 

public class QuartzTest {
    public static void main(String[] args) throws Exception {
        // 测试周期性job执行
        // SchedulerManager.createCycleJob(1);

        // 测试立刻执行的job
        // SchedulerManager.createStartNowJob(2);

        // 测试定时执行的job
        SchedulerManager.createStartFixedTimeJob(3);
    }
}

 

 

测试周期性job

测试立刻执行的job

测试定时执行的job

 

posted on 2017-03-04 15:24  芹derella  阅读(418)  评论(0编辑  收藏  举报