`
thinktothings
  • 浏览: 782549 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Lesson 2: The Quartz API, and Introduction to Jobs And Triggers

阅读更多

Quartz Enterprise Job Scheduler Tutorial

Lesson 2: The Quartz API, and Introduction to Jobs And Triggers

The Quartz API

The key interfaces of the Quartz API are:

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

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, as shown in Lesson 1 .

Quartz provides "builder" classes that define a Domain Specific Language (or DSL, also sometimes referred to as a "fluent interface"). In the previous lesson you saw an example of it, which we present a portion of here again:

  // define the job and tie it to our HelloJob class
  JobDetail job = newJob(HelloJob.class)
      .withIdentity("myJob", "group1") // name "myJob", group "group1"
      .build();
        
  // Trigger the job to run now, and then every 40 seconds
  Trigger trigger = newTrigger()
      .withIdentity("myTrigger", "group1")
      .startNow()
      .withSchedule(simpleSchedule()
          .withIntervalInSeconds(40)
          .repeatForever())            
      .build();
        
  // Tell quartz to schedule the job using our trigger
  sched.scheduleJob(job, trigger);

The block of code that builds the job definition is using methods that were statically imported from the JobBuilder class. Likewise, the block of code that builds the trigger is using methods imported from the TriggerBuilder class - as well as from the SimpleScheduleBulder class.

The static imports of the DSL can be achieved through import statements such as these:

import static org.quartz.JobBuilder.*;
import static org.quartz.SimpleScheduleBuilder.*;
import static org.quartz.CronScheduleBuilder.*;
import static org.quartz.CalendarIntervalScheduleBuilder.*;
import static org.quartz.TriggerBuilder.*;
import static org.quartz.DateBuilder.*;

The various "ScheduleBuilder " classes have methods relating to defining different types of schedules.

The DateBuilder class contains various methods for easily constructing java.util.Date instances for particular points in time (such as a date that represents the next even hour - or in other words 10:00:00 if it is currently 9:43:27).

Jobs and Triggers

A Job is a class that implements the Job interface, which has only one simple method:

The Job Interface
  package org.quartz;

  public interface Job {

    public void execute(JobExecutionContext context)
      throws JobExecutionException;
  }

When the Job's trigger fires (more on that in a moment), the execute(..) method is invoked by one of the scheduler's worker threads. The JobExecutionContext object that is passed to this method provides the job instance with information about its "run-time" environment - a handle to the Scheduler that executed it, a handle to the Trigger that triggered the execution, the job's JobDetail object, and a few other items.

The JobDetail object is created by the Quartz client (your program) at the time the Job is added to the scheduler. It contains various property settings for the Job, as well as a JobDataMap , which can be used to store state information for a given instance of your job class. It is essentially the definition of the job instance, and is discussed in further detail in the next lesson.

Trigger objects are used to trigger the execution (or 'firing') of jobs. When you wish to schedule a job, you instantiate a trigger and 'tune' its properties to provide the scheduling you wish to have. Triggers may also have a JobDataMap associated with them - this is useful to passing parameters to a Job that are specific to the firings of the trigger. Quartz ships with a handful of different trigger types, but the most commonly used types are SimpleTrigger and CronTrigger.

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

Why Jobs AND Triggers? Many job schedulers do not have separate notions of jobs and triggers. Some define a 'job' as simply an execution time (or schedule) along with some small job identifier. Others are much like the union of Quartz's job and trigger objects. While developing Quartz, we decided that it made sense to create a separation between the schedule and the work to be performed on that schedule. This has (in our opinion) many benefits.

For example, Jobs can be created and stored in the job scheduler independent of a trigger, and many triggers can be associated with the same job. Another benefit of this loose-coupling is the ability to configure jobs that remain in the scheduler after their associated triggers have expired, so that that it can be rescheduled later, without having to re-define it. It also allows you to modify or replace a trigger without having to re-define its associated job.

Identities

Jobs and Triggers are given identifying keys 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". The name portion of the key of a job or trigger must be unique within the group - or in other words, the complete key (or identifier) of a job or trigger is the compound of the name and group.

You now have a general idea about what Jobs and Triggers are, you can learn more about them in Lesson 3: More About Jobs & JobDetails and Lesson 4: More About Triggers .

分享到:
评论

相关推荐

    Lesson 1: Using Quartz

    《Lesson 1: 使用Quartz》 在IT领域,任务调度是系统自动化的重要组成部分,而Quartz是一款功能强大且广泛使用的Java作业调度框架。本文将深入探讨如何利用Quartz进行任务管理和执行,以及其在实际项目中的应用。 ...

    Lesson 3: More About Jobs and Job Details

    "Lesson 3: More About Jobs and Job Details"这个主题聚焦于如何更深入地理解任务(Jobs)以及它们的相关细节。在这个教程中,我们将探讨任务管理的核心概念、相关的工具,以及如何通过源码分析来了解其工作原理。 ...

    quartz-2.3.0-API文档-中文版.zip

    包含翻译后的API文档:quartz-2.3.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.quartz-scheduler:quartz:2.3.0; 标签:scheduler、quartz、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用...

    Lesson 4: More About Triggers

    本课"Lesson 4: More About Triggers"深入探讨了触发器的使用和实现细节。 在关系型数据库中,触发器主要用于实现数据完整性、业务规则的强制和审计跟踪等功能。当数据发生变化时,它们可以执行复杂的操作,这些...

    QuartzAPI中文chm

    QuartzAPI是一个强大的任务调度库,广泛用于Java应用程序中,以实现定时任务的自动化执行。这个"QuartzAPI中文chm"文档是专门为中文用户设计的学习资源,旨在帮助开发者快速理解和掌握Quartz的核心功能和使用方法。 ...

    quartz-2.3.2-API文档-中文版.zip

    包含翻译后的API文档:quartz-2.3.2-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.quartz-scheduler:quartz:2.3.2; 标签:quartz、scheduler、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用...

    定时器quartz API文档

    这个API文档详细地介绍了如何使用Quartz进行任务的定时执行。Quartz的核心概念包括Job(作业)、Trigger(触发器)和Scheduler(调度器)。下面我们将深入探讨这些概念以及它们在实际开发中的应用。 1. **Job**:在...

    Introduction to Quartz(2)

    【标题】:“Introduction to Quartz(2)” 【描述】:这篇博客主要介绍了Quartz,一个开源的作业调度框架,用于在Java应用程序中实现任务调度。Quartz允许开发人员定义作业和触发器,以便在特定时间执行预定的任务...

    quartz-2.3.2-API文档-中英对照版.zip

    包含翻译后的API文档:quartz-2.3.2-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.quartz-scheduler:quartz:2.3.2; 标签:scheduler、quartz、jar包、java、中英对照文档; 使用方法:解压翻译后...

    Quartz_API_1.65.zip_Quartz API_Quartz a_quartz 1.6.3 api_quartz

    Quartz API的设计基于两个主要概念:Jobs(作业)和Triggers(触发器)。Jobs是实际要执行的工作单元,它们实现了`org.quartz.Job`接口,并通过`execute()`方法来定义其执行逻辑。而Triggers则定义了何时以及如何...

    quartz-2.3.0-API文档-中英对照版.zip

    包含翻译后的API文档:quartz-2.3.0-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.quartz-scheduler:quartz:2.3.0; 标签:scheduler、quartz、jar包、java、API文档、中英对照版; 使用方法:解压...

    quartz 2.0.2 API

    3. **Scheduler**:Scheduler是整个调度系统的管理者,它负责管理和执行Jobs与Triggers。通过Scheduler,你可以安排新的Job,暂停、恢复或删除已有的Job,以及控制Trigger的执行。 4. **Calendar**:Calendar是用于...

    quartzAPI-2.2.1参看文档

    Quartz API 是一个开源的 Java 任务调度框架,它允许开发者在应用程序中创建、调度和管理作业(Jobs)和触发器(Triggers)。这个“quartzAPI-2.2.1参看文档”包含了关于 Quartz 2.2.1 版本的详细信息,非常适合学习...

    Programming with Quartz 2D and PDF Graphics in MacOS

    2. **Carbon与Quartz**:Carbon是另一种开发MacOS应用的技术栈,虽然不如Cocoa流行,但依然可以使用Quartz 2D进行图形处理。 3. **跨平台移植**:如果开发者需要将代码从其他平台(如Windows、Linux)移植到MacOS,...

    开发工具:基于Quartz的定时任务服务系统

    5. **易用性**:Quartz提供了一套直观的API,使得开发者能够方便地创建、调度和管理任务。 在Java开发中,使用Quartz通常涉及以下步骤: 1. **定义Job类**:创建一个实现了`org.quartz.Job`接口的类,这个类包含了...

    quartz 1.8.6 API 文档

    2. **Recovering Jobs**:Quartz能够恢复因异常而未完成的Job。如果Job在执行时发生错误,Quartz可以尝试重新调度该Job。 3. **Clustered Schedulers**:在分布式环境中,Quartz支持集群,多个调度器可以在不同的...

    Quartz 2D Programming Guide

    The Quartz 2D API is easy to use and provides accessto powerful featuressuch astransparency layers, path-based drawing, offscreen rendering, advanced color management, anti-aliased rendering, and PDF ...

    quartzJAVA包和api

    2. **Scheduler**:Scheduler是Quartz的核心组件,负责管理和调度所有的Job和Trigger。你可以通过Scheduler实例来安排新的任务,或者修改已有的任务。 3. **CalendarIntervalTrigger**和**CronTrigger**:这两者是...

    Programming with Quartz 2D and PDF Graphics in Mac OS X

    - **代码重用**:利用Quartz 2D和Cocoa提供的API,可以编写高度可重用的代码模块。 - **跨平台兼容性**:虽然本书主要介绍在Mac OS X上的应用,但很多技术和概念也可以应用于其他平台,如iOS或Windows。 #### 六、...

    QuartzAPI以及例子

    Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中。它提供了巨大的灵活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的...

Global site tag (gtag.js) - Google Analytics