`
ponlya
  • 浏览: 164409 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Quartz 笔记

 
阅读更多

从http://www.quartz-scheduler.org/documentation 习来的笔记,多是文档上面的原话,很清楚,也不想去译了,记下来。

Quartz Scheduler is licensed under the Apache License, Version 2.0 (the "License")

用处

Driving Process Workflow:(schedule a Job to fire in exactly 2 hours)

System Maintenance

Providing reminder services within an application.

Job Scheduling 的方式

at a certain time of day (to the millisecond)

on certain days of the week

on certain days of the month

on certain days of the year

not on certain days listed within a registered Calendar (such as business holidays)

repeated a specific number of times

repeated until a specific time/date

repeated indefinitely

repeated with a delay interval 

Clustering

Fail-over.

Load balancing.

Quartz's built-in clustering features rely upon database persistence via JDBCJobStore (described above).

Terracotta extensions to Quartz provide clustering capabilities without the need for a backing database.

主要API

Scheduler - the main API for interacting with the scheduler.

Job - an interface to be implemented by components that you wish to have executed by the scheduler.

A Scheduler's life-cycle is bounded by it's creation, via a SchedulerFactory and a call to its shutdown() method. Once created the Scheduler interface can be used add, remove, and list Jobs and Triggers, and perform other scheduling-related operations (such as pausing a trigger). However, the Scheduler will not actually act on any triggers (execute jobs) until it has been started with the start() method,

JobDetail - used to define instances of Jobs.

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

SimpleTrigger 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.

on the exactly specified intervals of SimpleTrigger. 

CronTrigger 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."

With CronTrigger, you can specify firing-schedules such as "every Friday at noon", or "every weekday and 9:30 am", or even "every 5 minutes between 9:00 am and 10:00 am on every Monday, Wednesday and Friday during January".

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

TriggerBuilder - used to define/build Trigger instances.

使用

1.quartz.properties  不是必须的,但是可以提供一些基本的配置.

2.many triggers can be associated with the same job. 

3.Jobs and Triggers are given identifying keys(unique within the group) as they are registered with the Quartz scheduler. The keys of Jobs and Triggers (JobKey and TriggerKey) allow them to be placed into 'groups' which can be useful for organizing your jobs and triggers into categories such as "reporting jobs" and "maintenance jobs". 

4.Quartz needs to be informed about various attributes that you may wish an instance of that job to have. This is done via the JobDetail class

5.JobDetail instances are built using the JobBuilder class. import static org.quartz.JobBuilder.*;

JobDataMap, which is part of the JobDetail object.

6.The JobDataMap can be used to hold any amount of (serializable) data objects which you wish to have made available to the job instance when it executes. JobDataMap is an implementation of the Java Map interface, and has some added convenience methods for storing and retrieving data of primitive types.

triggers contain a variety of customizable options that you need to be aware of and understand before you can make full use of Quartz. 

Cron Triggers.

like SimpleTrigger, CronTrigger has a startTime which specifies when the schedule is in force, and an (optional) endTime that specifies when the schedule should be discontinued.

Cron-Expressions are used to configure instances of CronTrigger.

Cron-Expressions are strings that are actually made up of seven sub-expressions, separated with white-space

Seconds numbers 0 to 59 for seconds and minutes

Minutes numbers 0 to 59 for seconds and minutes

Hours values 0 to 23 for hours.

Day-of-Month Day-of-Month can be any value 1-31

Month Months can be specified as values between 0 and 11, or by using the strings JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and DEC

Day-of-Week values between 1 and 7 (1 = Sunday) or by using the strings SUN, MON, TUE, WED, THU, FRI and SAT.

Year (optional field)

"0 0 12 ? * WED" -  means "every Wednesday at 12:00:00 pm".  WED->"MON-FRI", "MON,WED,FRI", or even "MON-WED,SAT".

The '/' character can be used to specify increments to values. 

0/15' in the Minutes field, it means 'every 15th minute of the hour, starting at minute zero'. 

'3/20' in the Minutes field, it would mean 'every 20th minute of the hour, starting at minute three' - or in other words it is the same as specifying '3,23,43' in the Minutes field. 

"/35" does *not mean "every 35 minutes" - it mean "every 35th minute of the hour, starting at minute zero" - or in other words the same as specifying '0,35'.

The '?' character is allowed for the day-of-month and day-of-week fields. It is used to specify "no specific value". 

The 'L' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "last",

"L" in the day-of-month field means "the last day of the month" - day 31 for January, day 28 for February on non-leap years.

day-of-week field by itself, it simply means "7" or "SAT".

"6L" or "FRIL" both mean "the last friday of the month". 

"L-3" which would mean the third-to-last day of the calendar month. 

'W' is used to specify the weekday (Monday-Friday) 

"15W" as the value for the day-of-month field, the meaning is: "the nearest weekday to the 15th of the month".

'#' is used to specify "the nth" XXX weekday of the month. 

"6#3" or "FRI#3" in the day-of-week field means "the third Friday of the month".

 

"0 0/5 * * * ?" create a trigger that simply fires every 5 minutes

"10 0/5 * * * ?" create a trigger that fires every 5 minutes, at 10 seconds after the minute (i.e. 10:00:10 am, 10:05:10 am, etc

"0 30 10-13 ? * WED,FRI" create a trigger that fires at 10:30, 11:30, 12:30, and 13:30, on every Wednesday and Friday.

"0 0/30 8-9 5,20 * ?" fires every half hour between the hours of 8 am and 10 am on the 5th and 20th of every month. Note that the trigger will NOT fire at 10:00 am, just at 8:00, 8:30, 9:00 and 9:30

 

Field Name     Mandatory      Allowed Values      Allowed Special Characters

Seconds           YES 0-59 , - * /

Minutes YES 0-59 , - * /

Hours YES 0-23 , - * /

Day of month YES 1-31 , - * ? / L W

Month YES 1-12 or JAN-DEC , - * /

Day of week YES 1-7 or SUN-SAT , - * ? / L #

Year NO empty, 1970-2099 , - * /

So cron expressions can be as simple as this: * * * * ? *

 

or more complex, like this: 0/5 14,18,3-39,52 * ? JAN,MAR,SEP MON-FRI 2002-2010

 

* ("all values") - used to select all values within a field. For example, "" in the minute field means *"every minute".

? ("no specific value") - useful when you need to specify something in one of the two fields in which the character is allowed, but not the other. For example, if I want my trigger to fire on a particular day of the month (say, the 10th), but don't care what day of the week that happens to be, I would put "10" in the day-of-month field, and "?" in the day-of-week field. See the examples below for clarification.

- - used to specify ranges. For example, "10-12" in the hour field means "the hours 10, 11 and 12".

, - used to specify additional values. For example, "MON,WED,FRI" in the day-of-week field means "the days Monday, Wednesday, and Friday".

/ - used to specify increments. For example, "0/15" in the seconds field means "the seconds 0, 15, 30, and 45". And "5/15" in the seconds field means "the seconds 5, 20, 35, and 50". You can also specify '/' after the '' character - in this case '' is equivalent to having '0' before the '/'. '1/3' in the day-of-month field means "fire every 3 days starting on the first day of the month".

 

**Expression** **Meaning**

0 0 12 * * ? Fire at 12pm (noon) every day

0 15 10 ? * * Fire at 10:15am every day

0 15 10 * * ? Fire at 10:15am every day

0 15 10 * * ? * Fire at 10:15am every day

0 15 10 * * ? 2005 Fire at 10:15am every day during the year 2005

0 * 14 * * ? Fire every minute starting at 2pm and ending at 2:59pm, every day

0 0/5 14 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day

0 0/5 14,18 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day

0 0-5 14 * * ? Fire every minute starting at 2pm and ending at 2:05pm, every day

0 10,44 14 ? 3 WED Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.

0 15 10 ? * MON-FRI Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday

0 15 10 15 * ? Fire at 10:15am on the 15th day of every month

0 15 10 L * ? Fire at 10:15am on the last day of every month

0 15 10 L-2 * ? Fire at 10:15am on the 2nd-to-last last day of every month

0 15 10 ? * 6L Fire at 10:15am on the last Friday of every month

0 15 10 ? * 6L Fire at 10:15am on the last Friday of every month

0 15 10 ? * 6L 2002-2005 Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005

0 15 10 ? * 6#3 Fire at 10:15am on the third Friday of every month

0 0 12 1/5 * ? Fire at 12pm (noon) every 5 days every month, starting on the first day of the month.

0 11 11 11 11 ? Fire every November 11th at 11:11am.

 

jobKey

startTime

endTime

Priority default priority of 5(same fire time. )

 

Quartz Calendar objects (not java.util.Calendar objects) can be associated with triggers at the time the trigger is defined and stored in the scheduler. Calendars are useful for excluding blocks of time from the the trigger's firing schedule. 

Calendars must be instantiated and registered with the scheduler via the addCalendar(..) method.

 

RAMJobStore is the simplest JobStore to use, it is also the most performant (in terms of CPU time). RAMJobStore gets its name in the obvious way: it keeps all of its data in RAM. 

JDBCJobStore is also aptly named - it keeps all of its data in a database via JDBC. 

JDBCJobStore works with nearly any database, it has been used widely with Oracle, PostgreSQL, MySQL, MS SQLServer, HSQLDB, and DB2. 

TerracottaJobStore is new with Quartz 1.7.

 

最佳实践

FROM:http://www.quartz-scheduler.org/documentation/best-practices

Skip Update Check

(Quartz contains an "update check" feature that connects to a server to check if there is a new version of Quartz available for download. )

org.quartz.scheduler.skipUpdateCheck: true   org.quartz.scheduler.skipUpdateCheck: true

Only Store Primitive Data Types (including Strings) In the JobDataMap

Use TriggerUtils

TriggerUtils:offers a simpler way to create triggers (schedules)

Has various methods for creating triggers with schedules that meet particular descriptions, 

Offers a simple way to create Dates (for start/end dates)

Offers helpers for analyzing triggers (e.g. calculating future fire times)

 

JDBC JobStore

Never Write Directly To Quartz's Tables

Never Point A Non-Clustered Scheduler At the Same Database As Another Scheduler With The Same Scheduler Name

Ensure Adequate Datasource Connection Size

Daylight Savings Time

Avoid Scheduling Jobs Near the Transition Hours of Daylight Savings Time

SimpleTriggers are not affected by Daylight Savings Time as they always fire at an exact millisecond in time, and repeat an exact number of milliseconds apart.

Because CronTriggers fire at given hours/minutes/seconds, they are subject to some oddities when DST transitions occur.

 

Waiting For Conditions

Throwing Exceptions

If a job throws an exception, Quartz will typically immediately re-execute it (and it will likely throw the same exception again). It's better if the job catches all exception it may encounter, handle them, and reschedule itself, or other jobs. to work around the issue.

 

org.quartz.scheduler.instanceName = Job-Scheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.scheduler.skipUpdateCheck=true

 

import java.util.Date;

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

public class HelloSchedulerJob implements Job
{
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException
	{
		JobKey key = context.getJobDetail().getKey();
		JobDataMap dataMap = context.getJobDetail().getJobDataMap();
		String key1 = dataMap.getString("key1");
		float key2 = dataMap.getFloat("key2");
		System.out.println(key+"_"+key1 + "_" + key2 + new Date());
	}
}

 

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

public class HelloScheduler
{

	public static void main(String[] args) throws SchedulerException
	{
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
		//StdSchedulerFactory.getScheduler();

		JobKey jk = new JobKey("Job-Hello");
		// define the job and tie it to our HelloJob class
		JobDetail job = newJob(HelloSchedulerJob.class)
				.withIdentity(jk)
				.withIdentity("job1", "group1") // name "job1", group "group1"
				.usingJobData("key1", "Hello World!")
			    .usingJobData("key2", 3.141f)
				.build();

		// Trigger the job to run now, and then repeat every 5 seconds
		Trigger trigger = newTrigger()
				.withIdentity("trigger1", "group1")
				.startNow()
				.withSchedule(simpleSchedule()
						.withIntervalInSeconds(5)
						.repeatForever())
				.build();

		// Tell quartz to schedule the job using our trigger
		scheduler.scheduleJob(job, trigger);
		scheduler.start(); //the Scheduler will not actually act on any triggers (execute jobs) until it has been started with the start() method, 
		 
		JobDetail jd =  scheduler.getJobDetail(jk);
		System.out.println(jd);
		//scheduler.shutdown();
	}

}

 

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

public class HelloSchedulerCron
{

	public static void main(String[] args) throws SchedulerException
	{
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); 
		JobDetail job = newJob(HelloSchedulerJob.class).withIdentity("job1", "group1").build();
		Trigger trigger = newTrigger()
			    .withIdentity("trigger3", "group1")
			    .withSchedule(cronSchedule("0 0/5 * * * ?"))
			    .forJob("job1", "group1")
			    .build();
		  
		// .withSchedule(dailyAtHourAndMinute(10, 42))
		// .withSchedule(cronSchedule("0 42 10 * * ?"))
		// .withSchedule(weeklyOnDayAndHourAndMinute(DateBuilder.WEDNESDAY, 10, 42))
		// .inTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))
		
		scheduler.scheduleJob(job, trigger);
		scheduler.start();
		// scheduler.shutdown();
	}

}

 

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import org.quartz.DateBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

public class HelloSchedulerSimpleTrigger
{

	public static void main(String[] args) throws SchedulerException
	{
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
		// define the job and tie it to our HelloJob class
		JobDetail job = newJob(HelloSchedulerJob.class).withIdentity("job1", "group1").build();

		/*--
		 // 方式一 Build a trigger for a specific moment in time, with no repeats:
		 // SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger1", "group1").startAt(new Date()).forJob("job1", "group1").build();

		// 方式二 Build a trigger for a specific moment in time, then repeating every ten seconds ten times:
		SimpleTrigger trigger = newTrigger().withIdentity("trigger3", "group1").startAt(new Date()).withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(10)).forJob(job).build();

		//方式三  Build a trigger that will fire once, five minutes in the future:
		SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger5", "group1").startAt(DateBuilder.futureDate(5, IntervalUnit.MINUTE)).forJob(job) .build();
		
		//方式四  Build a trigger that will fire now, then repeat every five minutes, until the hour 22:00:
		SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger5", "group1").withSchedule(simpleSchedule().withIntervalInMinutes(5).repeatForever()).endAt(DateBuilder.dateOf(22, 0, 0)).build();
		
		//方式五Build a trigger that will fire at the top of the next hour, then repeat every 2 hours, forever:
		SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger5", "group1").startAt(DateBuilder.evenHourDate(null)).withSchedule(simpleSchedule().withIntervalInMinutes(2).repeatForever()).build();
		
		 */
		
		//Build a trigger that will fire at the top of the next hour, then repeat every 2 hours, forever:
		SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger5", "group1").startAt(DateBuilder.evenHourDate(null)).withSchedule(simpleSchedule().withIntervalInMinutes(2).repeatForever()).build();
		
		// Tell quartz to schedule the job using our trigger
		scheduler.scheduleJob(job, trigger);
		scheduler.start();

		// scheduler.shutdown();
	}

}

 

分享到:
评论

相关推荐

    quartz 笔记 任务调度

    Quartz 是一个开源的任务调度框架,它允许开发者创建、安排和管理后台作业,这些作业可以在预定义的时间点或按照特定的触发器执行。在Java应用中,Quartz 提供了一个强大的工具来处理定时任务,无需依赖于操作系统...

    Spring调度器Quartz笔记

    1. **Quartz 简介**:Quartz 不仅仅是一个简单的定时任务库,它提供了一整套的调度服务,包括作业(Job)、触发器(Trigger)、计划器(Scheduler)等核心概念。通过这些组件,你可以精确地控制任务何时何地执行。...

    Quartz学习笔记

    本学习笔记将深入探讨Quartz的核心概念、配置以及如何使用其强大的调度功能。 首先,Quartz的核心组件包括Job(工作)、Trigger(触发器)和Scheduler(调度器)。Job代表了要执行的任务,Trigger定义了任务的执行...

    quartz自学笔记

    quartz自学笔记 建立 Hello Quartz 工程 创建一个 Quartz Scheduler

    Quartz调度学习笔记

    Quartz 是一个强大的任务调度框架,由 OpenSymphony 开源组织开发,主要目的是提供一个用于在 Java 应用程序中安排任务执行的解决方案。Quartz 支持与 J2EE 和 J2SE 平台的无缝集成,允许开发者在各种类型的项目中...

    Quartz入门学习(真丶入门)

    Quartz是一款开源的作业调度框架,它允许开发者创建和管理定时任务,以便在特定时间执行。在Java世界中,Quartz被广泛应用于自动化任务、批处理以及后台服务的定时触发等场景。本教程将带你逐步了解Quartz的基础知识...

    spring quartz实现触发器demo(笔记)

    这篇笔记将深入探讨如何使用Spring与Quartz结合来实现触发器的示例。 首先,Quartz是一个开源的作业调度框架,它允许开发者定义任务并在预定的时间点执行。Spring则是一个强大的应用框架,提供了依赖注入(DI)和...

    学习笔记_Quartz作业调度框架,使用corn表达式

    本学习笔记主要围绕Quartz的使用,特别是如何利用corn表达式来设置定时任务。 Quartz的核心概念包括Job(任务)、Trigger(触发器)和Scheduler(调度器)。Job是你要执行的实际业务逻辑,Trigger定义了Job何时被...

    Spring.NET学习笔记25——整合Quartz.NET例子

    刘冬编写Spring.NET学习笔记25——整合Quartz.NET例子。 原文: http://www.cnblogs.com/GoodHelper/archive/2009/11/20/SpringNet_QuartzNet.html

    quartz教程 quartz学习手册 quartz帮助手册

    Quartz是一款开源的作业调度框架,它允许开发者在Java应用程序中定义和执行复杂的定时任务。这个教程、学习手册和帮助手册旨在帮助用户深入理解和有效利用Quartz进行任务调度。 一、Quartz简介 Quartz是CMT...

    Quartz1.8任务进程管理系统学习笔记一:版本1.8和之前版本的不同

    NULL 博文链接:https://jaykuen.iteye.com/blog/911106

    springboot+quartz多任务

    Quartz则是一个强大的开源作业调度库,用于在Java应用中执行定时任务。本资源结合了Spring Boot与Quartz,提供了在Spring Boot环境中实现多任务调度的解决方案。 首先,我们需要了解Spring Boot的核心特性。Spring ...

    若依RuoYi框架剖析笔记,该笔记是在学习江南一点雨所录课程再结合自己的理解所写

    《若依RuoYi框架剖析笔记》是基于江南一点雨的课程并结合个人理解整理而成,涵盖了项目改造、框架结构分析以及多个关键功能模块的深入探讨。以下将逐一解析这些知识点。 1、**项目改造**:首先,从源代码仓库获取...

    springboot-quartz:springboot整合quartz

    在项目根目录下的`README.md`文件中,可能包含了更详细的笔记和示例代码,可以进一步学习和理解。 总的来说,SpringBoot与Quartz的整合为Java开发者提供了一种高效、灵活的方式来管理定时任务,提高了应用的自动化...

    尚硅谷spring boot 代码及笔记

    它集成了大量常用的第三方库配置,如 JDBC、MongoDB、RabbitMQ、Quartz 等,让开发者可以“开箱即用”。在“尚硅谷spring boot 代码及笔记”中,你将找到一系列关于 Spring Boot 的实践代码和学习笔记,这对于初学者...

    itlan_quartz:黑马程序员精品详解Quartz视频教程源代码

    最近学习“黑马程序员”的“精品详解石英视频”教程,顺便将课程的笔记整理出来,发现网上根本搜不到视频的笔记资料,遂将此资料分享出来,若有侵权,请联系本人 教程 一,石英概念 Quartz是OpenSymphony开放式组织...

    spring课堂笔记的相关知识

    方便解耦,简化开发 Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring...Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持

    spring2.0学习笔记+spring定时任务

    标题中的“spring2.0学习笔记+spring定时任务”表明了这个压缩包文件包含的是关于Spring框架2.0版本的学习资料,特别是关于Spring的定时任务功能。Spring是Java开发中最广泛应用的轻量级框架之一,它提供了一整套...

Global site tag (gtag.js) - Google Analytics