Quartz Tutorials 第二课 Quartz的API Jobs 和 Triggers
初次翻译如有不当请指正
Lesson 2: The Quartz API, Jobs And Triggers
The Quartz API
Quartz的API
The key interfaces of the Quartz API are:
关键的Quartz接口包括:
- Scheduler - the main API for interacting with the scheduler.
- 调度器——主要作用于调度器的API
- 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.
一个触发器的生命周期的界定是在创建时完成的,通过调度器工厂调用shutdown()方法完成调度器的关闭。一旦创建了调度器接口,可增删展示:任务/触发器/其他调度器相关的选项(比如暂停一个触发器)。然而只有当调度器调用了start()方法后才会开始执行这些任务,就像lesson1展示的那样。
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:
Quartz 提供了“创建”类,它可以定义特殊语言方式(流式语言编程),上一节已经展示了部分代码,让我们再看其中的一部分。
// 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.
这块代码创建了Job,通过静态类JobBuilder,同样的trigger也是用用TriggerBuilder类来创建的,包括schedule也是一样。
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).
时间构造器包含多种方法,可以很简单的构建java中的时间类型,可以创建特定的时间(比如当前的下一个小时 - 或其他时间如10:00)
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 JobExecutionContextobject 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.
当任务触发器出发(那一刻),这个execute()方法被调度器的工作线程进行调度执行。JobExecutionContextobject这个对象传递到方法,提供任务实例伴随着“运行”环境,还提供一个处理的触发器,还提供任务详情类和一些其他信息。
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.
任务详情,当任务加入到调度其中时被Quartz客户端创建的。它包含一些配置属性和任务数据Map,它可以用来存储状态信息,它本质上是定义了一个任务实体,我们将在下一课探讨它的细节。
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.
触发器也与任务数据Map有关联,这可以用来向任务传递数据,特别是启动触发器。Quartz有一小撮不同类型的触发器,但常用的是SimpleTrigger和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.”
SimpleTrigger是为了方便那些只需要执行一次的任务而设计的,或者你想在特定的时间执行任务,再重复N次在等待多久之后,并且在什么时候之间。当使用calendar-like调度器时,应用CronTigger是很有效的,比如每周五中午或者每个月的什么时候。
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.
为什么是任务和出发器的组合?很多任务调度器没有分开任务和触发器。一些定义任务把时间和创建者等一同定义。还有些就像Quartz方式。在应用Quartz开发时我们决定将二者分开,这样做在我们看来有很多益处。
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.
任务和触发器被标识出来关键词在调度器中。任务和触发器的关键词可以放置在‘groups’,它可以用来对任务进行分类,名称和分组名称必须是唯一的。在组中唯一,唯一组。
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.
对任务和调度器已经有了初步了解,详情可以继续学习lesson3和lesson4.
延伸阅读:
领域特定语言:domain-specific language (DSL)
https://blog.csdn.net/hzbooks/article/details/8721757

浙公网安备 33010602011771号