Quartz的Simple Triggers和Cron Triggers使用

实现Job接口创建Job

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;

/**
 * <p>
 * This is just a simple job that gets fired off many times by example 1
 * </p>
 * 
 * @author Bill Kratzer
 */
public class SimpleJob implements Job {

    private static Logger _log = LoggerFactory.getLogger(SimpleJob.class);

    /**
     * Empty constructor for job initialization
     */
    public SimpleJob() {
    }
    /**
     * <p>
     * Called by the <code>{@link org.quartz.Scheduler}</code> when a
     * <code>{@link org.quartz.Trigger}</code> fires that is associated with
     * the <code>Job</code>.
     * </p>
     * 
     * @throws JobExecutionException
     *             if there is an exception while executing the job.
     */
    public void execute(JobExecutionContext context)
        throws JobExecutionException {
        // This job simply prints out its job name and the
        // date and time that it is running
        JobKey jobKey = context.getJobDetail().getKey();
        _log.info("SimpleJob says: " + jobKey + " executing at " + new Date());
    }
}


Simple Triggers 的使用简介

Job在15秒后执行
    // get a "nice round" time a few seconds in the future...
    Date startTime = DateBuilder.nextGivenSecondDate(null, 15);

    // job1 will only fire once at date/time "ts"
    JobDetail job = newJob(SimpleJob.class).withIdentity("job1", "group1").build();

    SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger1", "group1").startAt(startTime).build();

    // schedule it to run!
    Date ft = sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every "
             + trigger.getRepeatInterval() / 1000 + " seconds");

Job在重复执行11次
 // job3 will run 11 times (run once and repeat 10 more times)
    // job3 will repeat every 10 seconds
    job = newJob(SimpleJob.class).withIdentity("job3", "group1").build();

    trigger = newTrigger().withIdentity("trigger3", "group1").startAt(startTime)
        .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(10)).build();

    ft = sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every "
             + trigger.getRepeatInterval() / 1000 + " seconds");

Job在五分钟后执行
    // job5 will run once, five minutes in the future
    job = newJob(SimpleJob.class).withIdentity("job5", "group1").build();

    trigger = (SimpleTrigger) newTrigger().withIdentity("trigger5", "group1")
        .startAt(futureDate(5, IntervalUnit.MINUTE)).build();

    ft = sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every "
             + trigger.getRepeatInterval() / 1000 + " seconds");

Job每40秒执行一次,无限执行
   // job6 will run indefinitely, every 40 seconds
    job = newJob(SimpleJob.class).withIdentity("job6", "group1").build();

    trigger = newTrigger().withIdentity("trigger6", "group1").startAt(startTime)
        .withSchedule(simpleSchedule().withIntervalInSeconds(40).repeatForever()).build();

    ft = sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every "
             + trigger.getRepeatInterval() / 1000 + " seconds");


当所有的Job和Trigger添加到scheduler里面去后调用    sched.start(); 便可启动触发器,开始检测任务,随时触发Job执行,

当然,当调用完    sched.start(); 之后还可以往scheduler里面添加Job和Trigger。


Job也可以不需要触发器触发,直接执行

// jobs can be fired directly... (rather than waiting for a trigger)
    job = newJob(SimpleJob.class).withIdentity("job8", "group1").storeDurably().build();

    sched.addJob(job, true);

关闭Scheduler调用 sched.shutdown(true);方法

  


CronTrigger的简单使用

Job每20秒执行一次
// job 1 will run every 20 seconds
    JobDetail job = newJob(SimpleJob.class).withIdentity("job1", "group1").build();

    CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule("0/20 * * * * ?"))
        .build();

    Date ft = sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "
             + trigger.getCronExpression());

Job每两分钟的第15秒执行
    // job 2 will run every other minute (at 15 seconds past the minute)
    job = newJob(SimpleJob.class).withIdentity("job2", "group1").build();

    trigger = newTrigger().withIdentity("trigger2", "group1").withSchedule(cronSchedule("15 0/2 * * * ?")).build();

    ft = sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "
             + trigger.getCronExpression());

Job每两分钟执行一次,在每天8点到17点
    // job 3 will run every other minute but only between 8am and 5pm
    job = newJob(SimpleJob.class).withIdentity("job3", "group1").build();

    trigger = newTrigger().withIdentity("trigger3", "group1").withSchedule(cronSchedule("0 0/2 8-17 * * ?")).build();

    ft = sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "
             + trigger.getCronExpression());


Job每个月的1号到15号每天早上十点执行
    // job 5 will run at 10am on the 1st and 15th days of the month
    job = newJob(SimpleJob.class).withIdentity("job5", "group1").build();

    trigger = newTrigger().withIdentity("trigger5", "group1").withSchedule(cronSchedule("0 0 10am 1,15 * ?")).build();

    ft = sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "
             + trigger.getCronExpression());

更多表达式例子

cron表达式                  含义
0 0 12 * * ?                   每天12点整触发一次
0 15 10 ? * *                  每天10点15分触发一次
0 15 10 * * ?                  每天10点15分触发一次
0 15 10 * * ? *                每天10点15分触发一次
0 15 10 * * ? 2005             2005年内每天10点15分触发一次
0 * 14 * * ?                   每天的2点整至2点59分,每分钟触发一次
0 0/5 14 * * ?                 每天的2点整至2点55分,每5分钟触发一次
0 0/5 14,18 * * ?              每天的2点整至2点55分以及18点整至18点55分,每5分钟触发一次
0 0-5 14 * * ?                 每天的2点整至2点5分,每分钟触发一次
0 10,44 14 ? 3 WED             每年3月的每个星期三的2点10分以及2点44分触发一次
0 15 10 ? * MON-FRI            每月周一、周二、周三、周四、周五的10点15分触发一次
0 15 10 15 * ?                 每月15的10点15分触发一次
0 15 10 L * ?                  每月最后一天的10点15分触发一次
0 15 10 ? * 6L                 每月最后一个周五的10点15分触发一次
0 15 10 ? * 6L                 每月最后一个周五的10点15分触发一次
0 15 10 ? * 6L 2002-2005       2002年至2005年间,每月最后一个周五的10点15分触发一次
0 15 10 ? * 6#3                每月第三个周五的10点15触发一次
0 0 12 1/5 * ?                 每月1号开始,每5天的12点整触发一次

0 11 11 11 11 ?                每年11月11日11点11分触发一次





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值