简单示例

1. maven 依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.quartz-scheduler</groupId>
  4. <artifactId>quartz</artifactId>
  5. <version>2.2.1</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.quartz-scheduler</groupId>
  9. <artifactId>quartz-jobs</artifactId>
  10. <version>2.2.1</version>
  11. </dependency>
  12.  
  13. <dependency>
  14. <groupId>log4j</groupId>
  15. <artifactId>log4j</artifactId>
  16. <version>1.2.16</version>
  17. <scope>provided</scope>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.slf4j</groupId>
  21. <artifactId>slf4j-log4j12</artifactId>
  22. <version>1.6.6</version>
  23. <scope>provided</scope>
  24. </dependency>
  25. </dependencies>

2. quarzt.properties(可选)

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

3. Job

  1. package com.huey.hello.quartz;
  2.  
  3. import java.util.Date;
  4.  
  5. import org.quartz.Job;
  6. import org.quartz.JobExecutionContext;
  7. import org.quartz.JobExecutionException;
  8.  
  9. import com.huey.hello.quartz.utils.DateUtils;
  10.  
  11. public class HelloJob implements Job {
  12.  
  13. /**
  14. * 实 现 org.quartz.Job 接口,并实现 execute 方法,在此方法执行业务逻辑
  15. */
  16. public void execute(JobExecutionContext context) throws JobExecutionException {
  17. Date fireTime = context.getFireTime();
  18. System.out.println("Hello Quartz Scheduler! " + DateUtils.dateToStr(fireTime));
  19. }
  20.  
  21. }

4. Simple Code

  1. package com.huey.hello.quartz;
  2.  
  3. import org.quartz.CronExpression;
  4. import org.quartz.CronScheduleBuilder;
  5. import org.quartz.JobBuilder;
  6. import org.quartz.JobDetail;
  7. import org.quartz.Scheduler;
  8. import org.quartz.SchedulerFactory;
  9. import org.quartz.Trigger;
  10. import org.quartz.TriggerBuilder;
  11. import org.quartz.impl.StdSchedulerFactory;
  12.  
  13. public class MainApp {
  14.  
  15. public static void main(String[] args) throws Exception {
  16.  
  17. // 创建 SchedulerFactory 并获取 Scheduler 对象实例
  18. SchedulerFactory schedulerFactory = new StdSchedulerFactory();
  19. Scheduler scheduler = schedulerFactory.getScheduler();
  20.  
  21. // 通过 JobBuilder 创建 JobDetail 对象实例
  22. JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
  23. .withIdentity("helloJob", Scheduler.DEFAULT_GROUP)
  24. .build();
  25.  
  26. // 通过 TriggerBuilder 创建 Trigger 对象实例,设置每 5 秒调度一次任务
  27. Trigger trigger = TriggerBuilder.newTrigger()
  28. .withIdentity("helloTrigger", Scheduler.DEFAULT_GROUP)
  29. .withSchedule(CronScheduleBuilder.cronSchedule(new CronExpression("0/5 * * * * ?")))
  30. .build();
  31.  
  32. // 排定任务
  33. scheduler.scheduleJob(jobDetail, trigger);
  34.  
  35. // 启动调度器
  36. scheduler.start();
  37. //
  38. Thread.sleep(20L * 1000L);
  39. // 关闭调度器
  40. scheduler.shutdown(true);
  41.  
  42. }
  43.  
  44. }

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. 应用完全启动后, Spring执行自定义初始化

    项目中做敏感词过滤, 因为前台ajax校验要走service ,而后台统一过滤器要走interceptor , 所以把检查器提到一个工具类(HeXieWordFinder)里 这个工具类理应缓存数据库 ...

  2. oracle备份恢复之rman恢复到异机

    注意事项: 1 此处实验环境为同平台,同字节序,同版本,源机器和目标机器相同的目录结构. 2 目标机器只需要安装oracle数据库软件即可. 3 第一次利用备份恢复测试环境,之后从源机器拷贝备份到目标 ...

  3. android SoundPool播放音效

    MediaPlayer的缺点: 资源占用量高,延时时间较长 不支持多个音效同一时候播放 SoundPool主要用于播放一些较短的声音片段,CPU资源占用率低和反应延时小,还支持自行色设置声音的品质,音 ...

  4. UOJ 08 Quine 是在下输了

    #8. Quine Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/8 Description 写一个程序,使其能 ...

  5. Codeforces Gym 100733A Shitália 计算几何

    ShitáliaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.acti ...

  6. HDU 4593 H - Robot 水题

    H - RobotTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.act ...

  7. HTTP协议报文、工作原理及Java中的HTTP通信技术详解

    一.web及网络基础       1.HTTP的历史            1.1.HTTP的概念:                 HTTP(Hyper Text Transfer Protocol ...

  8. yii2 rbac 设计

    tbl_auth_item 根据type存储认证项目...role.task.operation 游客... 不能操作任何模块 普通用户 ..biz_rule..需要登录 只有这里返回true,才能进 ...

  9. Ruby on Rails Tutorial 第一章 之 Heroku部署

    1.目的:用Heroku将开发环境部署到生产环境中.Heroku专门用于部署Rails和其他Web应用,部署Rails应用的过程非常简单——只要源码纳入Git版本控制系统就好. 2.搭建Heroku部 ...

  10. C#_Fileuploadify_notMvc

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.c ...