简单示例

1. maven 依赖

<dependencies>
<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> <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
<scope>provided</scope>
</dependency>
</dependencies>

2. quarzt.properties(可选)

org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

3. Job

package com.huey.hello.quartz;

import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; import com.huey.hello.quartz.utils.DateUtils; public class HelloJob implements Job { /**
* 实 现 org.quartz.Job 接口,并实现 execute 方法,在此方法执行业务逻辑
*/
public void execute(JobExecutionContext context) throws JobExecutionException {
Date fireTime = context.getFireTime();
System.out.println("Hello Quartz Scheduler! " + DateUtils.dateToStr(fireTime));
} }

4. Simple Code

package com.huey.hello.quartz;

import org.quartz.CronExpression;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory; public class MainApp { public static void main(String[] args) throws Exception { // 创建 SchedulerFactory 并获取 Scheduler 对象实例
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler(); // 通过 JobBuilder 创建 JobDetail 对象实例
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
.withIdentity("helloJob", Scheduler.DEFAULT_GROUP)
.build(); // 通过 TriggerBuilder 创建 Trigger 对象实例,设置每 5 秒调度一次任务
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("helloTrigger", Scheduler.DEFAULT_GROUP)
.withSchedule(CronScheduleBuilder.cronSchedule(new CronExpression("0/5 * * * * ?")))
.build(); // 排定任务
scheduler.scheduleJob(jobDetail, trigger); // 启动调度器
scheduler.start();
//
Thread.sleep(20L * 1000L);
// 关闭调度器
scheduler.shutdown(true); } }

Key Interface

Scheduler - the main API for interacting with the Scheduler.

Job - an interface to be implemented by components that you want the Scheduler to execute.

JobDetail - used to define instances of Jobs.

Trigger - a component that defines the schedule upon which a given Job will be executed.

JobBuilder - used to define/build JobDetail instances, which define instances of Jobs.

TriggerBuilder - used to define/build Trigger instances

SimpleTrigger - it is handy if you need 'one-shot' execution (just single execution of a job at a given moment in time), or if you need to fire a job at a given time, and have it repeat N times, with a delay of T between executions.

CronTrigger - it is useful if you wish to have triggering based on calendar-like schedules such as "every Friday, at noon" or "at 10:15 on the 10th day of every month."

Quartz Scheduler(2.2.1) - hello world的更多相关文章

  1. spring集成quartz scheduler

    创建项目 有两种创建quart配置作业 1.使用MethodInvokingJobDetailFactoryBean  Quartz Scheduler 配置作业(MethodInvokingJobD ...

  2. Table of Contents - Quartz Scheduler

    Getting Started Hello World Integration with Spring Quartz Scheduler Developer Guide Usage of JobDat ...

  3. Quartz Scheduler(2.2.1) - Working with JobStores

    About Job Stores JobStores are responsible for keeping track of all the work data you give to the sc ...

  4. Quartz Scheduler 开发指南(1)

    Quartz Scheduler 开发指南(1) 原文地址:http://www.quartz-scheduler.org/generated/2.2.2/html/qtz-all/ 实例化调度程序( ...

  5. 最新 Spring 4.2.2 集成 Quartz Scheduler 2.2.2 任务调度示例

    参考http://blog.csdn.net/defonds/article/details/49496895 本文将演示如何通过 Spring 使用 Quartz Scheduler 进行任务调度. ...

  6. 整合shiro出现【Correct the classpath of your application so that it contains a single, compatible version of org.quartz.Scheduler】

    跑的时候出现错误: Description: An attempt was made to call the method org.quartz.Scheduler.getListenerManage ...

  7. Quartz Scheduler(2.2.1) - Integration with Spring

    1. maven 依赖: <properties> <spring.version>3.2.3.RELEASE</spring.version> <quart ...

  8. Quartz Scheduler(2.2.1) - Usage of JobDataMap

    The JobDataMap can be used to hold any amount of (serializable) data objects which you wish to have ...

  9. Quartz Scheduler(2.2.1) - Usage of Calendars

    Quartz Calendar objects (not java.util.Calendar objects) can be associated with triggers at the time ...

随机推荐

  1. 转载 SQL Server中索引管理之六大铁律

    转载原地址 http://jingyan.baidu.com/article/48a42057c03bd7a924250429.html 索引是以表列为基础的数据库对象.索引中保存着表中排序的索引列, ...

  2. 电脑蓝屏分析教程,附工具WinDbg(x86 x64)6.12.0002.633下载

    我们常常在使用电脑中,有时会碰到电脑蓝屏,我们经常束手无策,不知道为什么会蓝屏?有些蓝屏后自动重启能正常进入系统,那么我们就可以借助工具进行分析.而有些可能需要进入到安全模式或者pe系统才会正常,那么 ...

  3. CKEditor与CKFinder整合并实现文件上传功能

    事先说明:此整合的是java版本的, 用到的有:jsp + ckeditor + ckfinder (没有servlet 及其它框架技术) 一.需要的资源: 用到的网站,文件自己下载: a) cked ...

  4. CentOS 7安装iptables服务,以及常用命令

    之前使用的是CentOS6.5,并且学艺不精,用啥查啥,用完就忘.并且网上大部分资料是基于CentOS7之前的版本. 在CentOS7中,默认的防火墙不是iptables,而是firewalld.而且 ...

  5. PostgreSQL中,database,schema,table之间关系

    从逻辑上看,schema,table,都是位于database之下. 首先,在postgres数据库下建立表(相当于建立在public schema下): [pgsql@localhost bin]$ ...

  6. cmp排序hdoj 1106排序

    上班之余抽点时间出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下cmp排序 /*标题还是比拟的水吧,但是花的时间还是比拟的多,心不够静*/ #include <iostrea ...

  7. python的一些总结2

    第一篇 写了下 基本的环境搭建和一个hello world 程序 下面 介绍接下 怎么使用 python 搭建一个网站.(中间的语法教学 请参考->http://www.liaoxuefeng. ...

  8. [AngularJS] Directive using another directive by 'require'

    Directive can use another directive though 'require' keyword. angular.module('docsTabsExample', []) ...

  9. [AngularJS] ui-router: named views

    The ui-router library for AngularJS provides the ability to name views within your application. This ...

  10. HelloSpark.scala

    /** * Created by root on 9/6/15. */ import org.apache.spark.SparkContext import org.apache.spark.Spa ...