简单示例

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. 转载Repository 和Unit of work的使用说明

    利用Repository and Unit of Work重构项目 文章索引和简介 项目最基础的东西已经结束了,但是现在我们的项目还不健全  不利于测试 重复性代码多   层与层之间耦合性高  不利于 ...

  2. UVA10361 - Automatic Poetry

    “Oh God”, Lara Croft exclaims, “it’s one of these dumb riddles again!” In Tomb Raider XIV, Lara is, ...

  3. ActiveMQ学习笔记(二) JMS与Spring

    上文可见,JMS Native API使用起来不是特别方便.好在Spring提供了很好的JMS支持. (一)配置ConnectionFactory 如果使用连接池的话,不要忘记activemq-poo ...

  4. MySQL BINARY和BIT类型

    BINARY和VARBINARY类型 BINARY和VARBIANRY类型同CHAR和VARCHAR类型相似,除了BIANARY和VARBINARY类型只包含二进制字符串,即它们只包含byte串而非字 ...

  5. ACID CAP BASE介绍

    ACID ACID,是指数据库管理系统(DBMS)在写入/更新资料的过程中,为保证事务(transaction)是正确可靠的,所必须具备的四个特性:原子性(atomicity,或称不可分割性).一致性 ...

  6. Selenium IDE 工具总结

    基本介绍: Selenium工具专门为WEB应用程序编写的一个验收测试工具. Selenium的核心:browser bot,是用JavaScript编写的. Selenium工具有4种:Seleni ...

  7. 推荐十款非常优秀的 HTML5 在线设计工具

    网络有很多优秀的设计和开发工具可能大家都不知道,因此这篇文章就向设计师推荐十款优秀 HTML5 在线工具,这些工具能够帮助设计师们设计出更有创意的作品.随着 HTML5 技术的不断成熟,网络上涌现出越 ...

  8. Zabbix3.0完整部署

    0. 准备工作 0.1 初始化 #!/bin/sh yum clean all systemctl stop firewalld.service systemctl disable firewalld ...

  9. Cocos2d-x中由sprite来驱动Box2D的body运动(用来制作平台游戏中多变的机关)

    好久都没写文章了,就来一篇吧.这种方法是在制作<胖鸟大冒险>时用到的.<胖鸟大冒险>中使用Box2D来进行物理模拟和碰撞检測,因此对每一个机关须要创建一个b2body.然后&l ...

  10. VS里面如何设置环境默认的开发语言