quartz入门(二)SimpleTrigger简单实例

此篇博客为quartz2.2.1第二个例子的解析,此例子主要对SimpleTrigger的使用进行详细说明,详细使用说明均在代码中以注释的形式体现。

  • SimpleTrigger:在某个时刻开始,然后按照某个时间间隔重复执行。
  • SimpleTrigger包括属性:开始时间,结束时间,重复次数,重复间隔。

SimpleJob

package com.secbro.job;

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

import java.util.Date;

/**
 */
public class SimpleJob implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        // JobExecutionContext类提供了调度上线问的各种信息,为JobDetail和Trigger提供必要的信息
        // JobKey是由name和group组成,并且name必须在group内是唯一的。如果只指定一组则将使用默认的组名。
        JobKey jobKey = context.getJobDetail().getKey();

        System.out.println("SimpleJob says: " + jobKey + " executing at " + new Date());

    }
}

SimpleTriggerExample

package com.secbro.test;

import com.secbro.job.SimpleJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

/**
 * SimpleTrigger:在某个时刻开始,然后按照某个时间间隔重复执行;<br>
 * SimpleTrigger包括属性:开始时间,结束时间,重复次数,重复间隔。
 */
public class SimpleTriggerExample {

    public void run() throws SchedulerException {
        // 通过SchedulerFactory获取一个调度器实例
        StdSchedulerFactory sf = new StdSchedulerFactory();
        // 代表一个Quartz的独立运行容器
        Scheduler scheduler = sf.getScheduler();
        // 获取当前时间15秒之后的时间
        Date startDate = DateBuilder.nextGivenSecondDate(null,15);

        // ------------------------------------------Job1 start-------------------------------------------------
        // 创建一个JobDetail实例,此版本JobDetail已经作为接口(interface)存在,通过JobBuilder创建
        // 并指定Job在Scheduler中所属组及名称
        JobDetail jobDetail = JobBuilder.newJob(SimpleJob.class).withIdentity("job1","group1").build();

        // SimpleTrigger实现Trigger接口的子接口。此处只指定了开始执行定时任务的时间,使用默认的重复次数(0次)和重复间隔(0秒)
        SimpleTrigger simpleTrigger = (SimpleTrigger) TriggerBuilder.newTrigger().withIdentity("trigger1","group1").
                startAt(startDate).build();
        // 添加JobDetail到Scheduler容器中,并且和Trigger进行关联,返回执行时间
        Date date = scheduler.scheduleJob(jobDetail,simpleTrigger);
        System.out.println(jobDetail.getKey() + " will run at: " + date + " and repeat: " + simpleTrigger.getRepeatCount()
                + " times, every " + simpleTrigger.getRepeatInterval() / 1000L + " seconds");
        // ------------------------------------------Job1 end-------------------------------------------------
        // ------------------------------------------Job2 start-------------------------------------------------
        // 与job1相同处理方式的job2,几乎同时执行两个job1和job2任务
        jobDetail = JobBuilder.newJob(SimpleJob.class).withIdentity("job2","group1").build();
        simpleTrigger = (SimpleTrigger) TriggerBuilder.newTrigger().withIdentity("trigger2","group1").startAt(startDate).build();
        date = scheduler.scheduleJob(jobDetail,simpleTrigger);
        System.out.println(jobDetail.getKey() + " will run at: " + date + " and repeat: " + simpleTrigger.getRepeatCount()
                + " times, every " + simpleTrigger.getRepeatInterval() / 1000L + " seconds");
        // ------------------------------------------Job2 end-------------------------------------------------
        // ------------------------------------------Job3 start-------------------------------------------------
        jobDetail = JobBuilder.newJob(SimpleJob.class).withIdentity("job3","group1").build();
        // 设置定时任务执行规则为在指定的开始时间进行执行,每个十秒执行一次,重复执行十次。
        // 当指定为SimpleScheduleBuilder时,会发现不用对结果进行强制转换为SimpleTrigger了。
        simpleTrigger = TriggerBuilder.newTrigger().withIdentity("trigger3", "group1").startAt(startDate)
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10).withRepeatCount(10)).build();
        System.out.println(jobDetail.getKey() + " will run at: " + date + " and repeat: " + simpleTrigger.getRepeatCount()
                + " times, every " + simpleTrigger.getRepeatInterval() / 1000L + " seconds");
        scheduler.scheduleJob(jobDetail,simpleTrigger);
        // forJob 指定trigger对应的定时任务,另外一种实现形式。此Trigger和上面的Trigger共同出发一个任务,执行各自均执行
        simpleTrigger = TriggerBuilder.newTrigger().withIdentity("trigger3","group2").startAt(startDate)
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10).withRepeatCount(2)).forJob(jobDetail).build();
        scheduler.scheduleJob(simpleTrigger);
        System.out.println(jobDetail.getKey() + " will [also] run at: " + date + " and repeat: " + simpleTrigger.getRepeatCount()
                + " times, every " + simpleTrigger.getRepeatInterval() / 1000L + " seconds");
        // ------------------------------------------Job3 end-------------------------------------------------
        // ------------------------------------------Job4 start-----------------------------------------------
        jobDetail = JobBuilder.newJob(SimpleJob.class).withIdentity("job4","group1").build();
        simpleTrigger = TriggerBuilder.newTrigger().withIdentity("trigger4", "group1").startAt(startDate).
                withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10).withRepeatCount(5)).build();
        date = scheduler.scheduleJob(jobDetail, simpleTrigger);
        System.out.println(jobDetail.getKey() + " will run at: " + date + " and repeat: " + simpleTrigger.getRepeatCount()
                + " times, every " + simpleTrigger.getRepeatInterval() / 1000L + " seconds");

        // ------------------------------------------Job4 end-------------------------------------------------
        // ------------------------------------------Job5 start-------------------------------------------------
        jobDetail = JobBuilder.newJob(SimpleJob.class).withIdentity("job5", "group1").build();
        simpleTrigger = (SimpleTrigger)TriggerBuilder.newTrigger().withIdentity("trigger5", "group1")
                .startAt(DateBuilder.futureDate(5, DateBuilder.IntervalUnit.MINUTE)).build();
        date = scheduler.scheduleJob(jobDetail, simpleTrigger);
        System.out.println(jobDetail.getKey() + " will run at: " + date + " and repeat: " + simpleTrigger.getRepeatCount()
                + " times, every " + simpleTrigger.getRepeatInterval() / 1000L + " seconds");
        // ------------------------------------------Job5 end-------------------------------------------------
        // ------------------------------------------Job6 start-----------------------------------------------
        jobDetail = JobBuilder.newJob(SimpleJob.class).withIdentity("job6", "group1").build();
        // 此定时任务的规则为每隔40秒执行一次,永远执行下去
        simpleTrigger = TriggerBuilder.newTrigger().withIdentity("trigger6", "group1").startAt(startDate)
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(40).repeatForever()).build();
        date = scheduler.scheduleJob(jobDetail, simpleTrigger);
        System.out.println(jobDetail.getKey() + " will run at: " + date + " and repeat: " + simpleTrigger.getRepeatCount() + " times, every " + simpleTrigger.getRepeatInterval() / 1000L + " seconds");
        // ------------------------------------------Job6 end-------------------------------------------------
        System.out.println("------- Starting Scheduler ----------------");
        // 在scheduler.start之后调用,可以重新定义trigger,重新注册
        scheduler.start();
        System.out.println("------- Started Scheduler -----------------");
        // ------------------------------------------Job7 start-----------------------------------------------
        jobDetail = JobBuilder.newJob(SimpleJob.class).withIdentity("job7", "group1").build();
        // 每隔五分钟执行一次,执行20次
        simpleTrigger = TriggerBuilder.newTrigger().withIdentity("trigger7", "group1").startAt(startDate)
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMinutes(5).withRepeatCount(20)).build();
        date = scheduler.scheduleJob(jobDetail, simpleTrigger);
        System.out.println(jobDetail.getKey() + " will run at: " + date + " and repeat: " + simpleTrigger.getRepeatCount()
                + " times, every " + simpleTrigger.getRepeatInterval() / 1000L + " seconds");
        // ------------------------------------------Job7 end-------------------------------------------------
        // ------------------------------------------Job8 start-----------------------------------------------
        // storeDurably(),没有触发器指向任务的时候,将任务保存在队列中,然后可以通过手动进行出发
        jobDetail = JobBuilder.newJob(SimpleJob.class).withIdentity("job8", "group1").storeDurably().build();
        // 任务立即执行
        scheduler.addJob(jobDetail, true);
        System.out.println("手动触发   triggering job8...");
        scheduler.triggerJob(JobKey.jobKey("job8", "group1"));
        // ------------------------------------------Job8 end-------------------------------------------------
        scheduler.start();
        try {
            Thread.sleep(60000L);
        } catch (InterruptedException e) {

        }

        // 对Job7进行重新安排
        System.out.println("------- Rescheduling... --------------------");
        simpleTrigger = TriggerBuilder.newTrigger().withIdentity("trigger7", "group1").startAt(startDate).
                withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMinutes(5).withRepeatCount(20)).build();
        date = scheduler.rescheduleJob(simpleTrigger.getKey(), simpleTrigger);
        System.out.println("job7 rescheduled to run at: " + date);
        System.out.println("------- Waiting five minutes... ------------");

        try {
            Thread.sleep(60000L);
        } catch (Exception ex) {
        }

        scheduler.shutdown(true);

        SchedulerMetaData metaData = scheduler.getMetaData();
        System.out.println("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");

    }

    public static void main(String[] args) throws SchedulerException {
        SimpleTriggerExample simpleTriggerExample = new SimpleTriggerExample();
        simpleTriggerExample.run();
    }
}

日志信息

group1.job1 will run at: Mon Jun 15 13:25:15 CST 2015 and repeat: 0 times, every 0 seconds
group1.job2 will run at: Mon Jun 15 13:25:15 CST 2015 and repeat: 0 times, every 0 seconds
group1.job3 will run at: Mon Jun 15 13:25:15 CST 2015 and repeat: 10 times, every 10 seconds
group1.job3 will [also] run at: Mon Jun 15 13:25:15 CST 2015 and repeat: 2 times, every 10 seconds
group1.job4 will run at: Mon Jun 15 13:25:15 CST 2015 and repeat: 5 times, every 10 seconds
group1.job5 will run at: Mon Jun 15 13:30:12 CST 2015 and repeat: 0 times, every 0 seconds
group1.job6 will run at: Mon Jun 15 13:25:15 CST 2015 and repeat: -1 times, every 40 seconds
------- Starting Scheduler ----------------
------- Started Scheduler -----------------
group1.job7 will run at: Mon Jun 15 13:25:15 CST 2015 and repeat: 20 times, every 300 seconds
手动触发   triggering job8...
SimpleJob says: group1.job8 executing at Mon Jun 15 13:25:12 CST 2015
SimpleJob says: group1.job1 executing at Mon Jun 15 13:25:15 CST 2015
SimpleJob says: group1.job2 executing at Mon Jun 15 13:25:15 CST 2015
SimpleJob says: group1.job3 executing at Mon Jun 15 13:25:15 CST 2015
SimpleJob says: group1.job4 executing at Mon Jun 15 13:25:15 CST 2015
SimpleJob says: group1.job6 executing at Mon Jun 15 13:25:15 CST 2015
SimpleJob says: group1.job7 executing at Mon Jun 15 13:25:15 CST 2015
SimpleJob says: group1.job3 executing at Mon Jun 15 13:25:15 CST 2015
SimpleJob says: group1.job3 executing at Mon Jun 15 13:25:25 CST 2015
SimpleJob says: group1.job4 executing at Mon Jun 15 13:25:25 CST 2015
SimpleJob says: group1.job3 executing at Mon Jun 15 13:25:25 CST 2015
SimpleJob says: group1.job3 executing at Mon Jun 15 13:25:35 CST 2015
SimpleJob says: group1.job4 executing at Mon Jun 15 13:25:35 CST 2015
SimpleJob says: group1.job3 executing at Mon Jun 15 13:25:35 CST 2015
SimpleJob says: group1.job3 executing at Mon Jun 15 13:25:45 CST 2015
SimpleJob says: group1.job4 executing at Mon Jun 15 13:25:45 CST 2015
SimpleJob says: group1.job3 executing at Mon Jun 15 13:25:55 CST 2015
SimpleJob says: group1.job4 executing at Mon Jun 15 13:25:55 CST 2015
SimpleJob says: group1.job6 executing at Mon Jun 15 13:25:55 CST 2015
SimpleJob says: group1.job3 executing at Mon Jun 15 13:26:05 CST 2015
SimpleJob says: group1.job4 executing at Mon Jun 15 13:26:05 CST 2015
------- Rescheduling... --------------------
job7 rescheduled to run at: Mon Jun 15 13:25:15 CST 2015
------- Waiting five minutes... ------------
SimpleJob says: group1.job7 executing at Mon Jun 15 13:26:12 CST 2015
SimpleJob says: group1.job3 executing at Mon Jun 15 13:26:15 CST 2015
SimpleJob says: group1.job3 executing at Mon Jun 15 13:26:25 CST 2015
SimpleJob says: group1.job3 executing at Mon Jun 15 13:26:35 CST 2015
SimpleJob says: group1.job6 executing at Mon Jun 15 13:26:35 CST 2015
SimpleJob says: group1.job3 executing at Mon Jun 15 13:26:45 CST 2015
SimpleJob says: group1.job3 executing at Mon Jun 15 13:26:55 CST 2015
Executed 28 jobs.
时间: 2024-10-20 15:24:41

quartz入门(二)SimpleTrigger简单实例的相关文章

Quartz入门 (二) SimpleTrigger

Trigger 就是触发器的意思,用来指定什么时间开始触发,触发多少次,每隔多久触发一次 SimpleTrigger 可以方便的实现一系列的触发机制. 1.下一个8秒的倍数开始运行: public class SimpleTriggerDemo { public static void main(String[] args) throws Exception{ Scheduler scheduler= StdSchedulerFactory.getDefaultScheduler(); Date

AJAX学习整理二之简单实例

做了几个简单的实例,加载txt文本内容.加载xml文件内容,把xml文本内容转换成html表格显示.废话不多说,直接贴代码: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/html"> <head>     <title>通过ajax获取文本内容</title>     <meta charset="utf-8">     <scr

mybatis快速入门,mybatis简单实例, 如何使用mybatis

目录结构: 1. 导入所需要的包 2. 创建数据库 create database mybatis; use mybatis; CREATE TABLE users(id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(20), age INT); INSERT INTO users(NAME, age) VALUES('Tom', 12); INSERT INTO users(NAME, age) VALUES('Jack', 11); 3. 建立相应

MyBatis(一) 入门使用和简单实例

Mybatis是一个著名的轻量级持久层框架. 在这篇文章中,使用Mybatis框架,实现一个简单的查询事例.后面的文章中会再详细介绍其他用法. 首先,需要导入一些使用的jar,包括:mybatis.commons-logging.mysql-connector-java.log4j等. 在pom.xml中添加dependencies(上边几个项目的依赖): <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&

quartz入门(一)下载及简单实例

下载 官网下载地址:http://www.quartz-scheduler.org/ 下载步骤: 1.点击橘黄色的download连接. 2.点击Direct Download下面的Quartz 2.2.1 full distribution (release notes) 3.点击quartz-2.2.1-distribution.tar.gz进行下载,选择两个中的任何一个即可. 4.Maven项目引入 <dependency> <groupId>org.quartz-sched

quartz入门实例

一 Quarta介绍 1 Quartz是什么 Quartz就是一个纯 Java 实现的作业调度工具,相当于数据库中的 Job.Windows 的计划任务.Unix/Linux 下的 Cron,但 Quartz 可以把排程控制的更精细. 2 Quartz 框架的发展历程 Quartz项目是由James House创立的,在1998年就有该框架最初的构思,包括作业队列的概念,使用线程池来处理作业,在2001年春天在SourceForge 上创立了该项目. Quartz 的目录结构和内容 Docs Q

Flex入门(二)——Flex+BlazeDs+J2ee小实例

首先来简单介绍一下BlazeDS. BlaseDS的核心功能包括RPC Services(远程过程调用服务) 和Messaging Service(消息服务).BlazeDS是一个基于服务器的Java远程调用(remoting)和web消息传递(messaging)技术,使得后台的Java应用程序可以和运行在浏览器上的Flex应用程序能够互相通信.简单来说一个BlazeDS应用包括客户端(Flex或AIR应用程序)和一个服务端(J2EE程序).BlazeDS在期间起着承上启下的作用,Flex和B

Quartz入门实例13-spirng4和quartz2实现动态任务调用

pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <model

ZabbixAPI+django+nginx简单的二次开发实例(三)

接上一篇博文 ZabbixAPI+django+nginx简单的二次开发实例(二) 步骤三,站点架构部分 本部分用到的软件 1,Nginx:接受访问请求,应答静态页面,转发动态请求至uwsgi 2,uwsgi:应答动态请求 3,Django:处理后台数据 4,supervisor:管理进程 首先安装Nginx yum install epel-release yum install python-devel nginx 修改配置文件 vim /etc/ngnix/ngnix.conf user