Quartz_2.2.X学习系列七:Tutorials - Lesson 7: TriggerListeners and JobListeners

7课小结:

一、什么是Listener

侦听器(Listener)就是您创建的对象,该对象根据调度程序中发生的事件执行动作。它有两个Listener:TriggerListeners 和JobListeners 。

 

1.TriggerListeners

作用:TriggerListeners 接收与触发器相关的事件。

Trigger关联的事件包括:触发器触发、触发器错误触发(在本文档的“Triggers”部分中讨论)和触发器完成(触发器触发的作业已经完成)。

 

2.JobListeners

作用:JobListeners 接收与作业相关的事件。

Job关联的事件包括:作业被执行的通知,以及作业执行完成时的通知。

扫描二维码关注公众号,回复: 3107231 查看本文章

 

二、如何使用你自己的Listeners

1.创建侦听器,有两种方法:

a.创建一个实现了org.quartz.TriggerListener和/或org.quartz.JobListener接口的对象即可。

b.继承JobListenerSupport 或TriggerListenerSupport类,仅简单地override 您感兴趣的事件即可。

 

2.静态引用:

import static org.quartz.JobKey.*;

import static org.quartz.impl.matchers.KeyMatcher.*;

import static org.quartz.impl.matchers.GroupMatcher.*;

import static org.quartz.impl.matchers.AndMatcher.*;

import static org.quartz.impl.matchers.OrMatcher.*;

import static org.quartz.impl.matchers.EverythingMatcher.*;

 

 

三、Listener应用案例:

1.添加一个JobListener到一个指定的Job中(通过JobKey的name和group来指定唯一Job)

Adding a JobListener that is interested in a particular job:

scheduler.getListenerManager().addJobListener(myJobListener, KeyMatcher.jobKeyEquals(new JobKey("myJobName", "myJobGroup")));

 

 

2.添加一个JobListener到一个指定的Job Group中的所有Job(通过JobKey的group来指定属于这个group的Job)

Adding a JobListener that is interested in all jobs of a particular group:

scheduler.getListenerManager().addJobListener(myJobListener, jobGroupEquals("myJobGroup"));

 

3.添加一个JobListener到两个指定的Job Group的所有Job

Adding a JobListener that is interested in all jobs of two particular groups:

scheduler.getListenerManager().addJobListener(myJobListener, or(jobGroupEquals("myJobGroup"), jobGroupEquals("yourGroup")));

 

4.添加一个JobListener到所有的Job

Adding a JobListener that is interested in all jobs:

scheduler.getListenerManager().addJobListener(myJobListener, allJobs());

 

 

 

 

 

 

 

Lesson 7: TriggerListeners and JobListeners

Listeners are objects that you create to perform actions based on events occurring within the scheduler. As you can probably guess, TriggerListeners receive events related to triggers, and JobListeners receive events related to jobs.

 

 

侦听器就是您创建的对象,该对象根据调度程序中发生的事件执行动作。正如您可能猜到的那样,TriggerListeners 接收与触发器相关的事件,而JobListeners 接收与作业相关的事件。

 

Trigger-related events include: trigger firings, trigger mis-firings (discussed in the “Triggers” section of this document), and trigger completions (the jobs fired off by the trigger is finished).

 

Trigger关联的事件包括:触发器触发、触发器错误触发(在本文档的“Triggers”部分中讨论)和触发器完成(触发器触发的作业已经完成)。

 

The org.quartz.TriggerListener Interface
public interface TriggerListener {

public String getName();

public void triggerFired(Trigger trigger, JobExecutionContext context);

public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context);

public void triggerMisfired(Trigger trigger);

public void triggerComplete(Trigger trigger, JobExecutionContext context,
            int triggerInstructionCode);
}

 

Job-related events include: a notification that the job is about to be executed, and a notification when the job has completed execution.

 

Job关联的事件包括:作业被执行的通知,以及作业执行完成时的通知。

 

The org.quartz.JobListener Interface
public interface JobListener {

public String getName();

public void jobToBeExecuted(JobExecutionContext context);

public void jobExecutionVetoed(JobExecutionContext context);

public void jobWasExecuted(JobExecutionContext context,
            JobExecutionException jobException);

}

 

Using Your Own Listeners

To create a listener, simply create an object that implements the org.quartz.TriggerListener and/or org.quartz.JobListener interface. Listeners are then registered with the scheduler during run time, and must be given a name (or rather, they must advertise their own name via their getName() method).

For your convenience, tather than implementing those interfaces, your class could also extend the class JobListenerSupport or TriggerListenerSupport and simply override the events you’re interested in.

 

Listeners are registered with the scheduler’s ListenerManager along with a Matcher that describes which Jobs/Triggers the listener wants to receive events for.

 

Listeners are registered with the scheduler during run time, and are NOT stored in the JobStore along with the jobs and triggers. This is because listeners are typically an integration point with your application. Hence, each time your application runs, the listeners need to be re-registered with the scheduler.

 

使用你自己的Listeners

要创建侦听器,只需创建一个实现了org.quartz.TriggerListener和/或org.quartz.JobListener接口的对象即可。在运行时,监听器会在调度程序中注册,并且必须给它一个名称(或者更确切地说,它们必须通过getName()方法来宣传自己的名字)。

为了方便你,比实现这些接口更方便的方法是,您的类可以继承JobListenerSupport 或TriggerListenerSupport类,仅简单地override 您感兴趣的事件即可。

 

监听器是在调度程序的ListenerManager中注册的,连同一个Matcher,它描述了侦听器想要为Job还是Trigger接收事件。

 

在运行期间,Listeners 在调度程序中注册,它不存储在JobStore,以及作业和触发器中。这是因为Listeners 通常是应用程序的集成点。因此,每次应用程序运行时,侦听器都需要重新注册到调度程序。

 

Adding a JobListener that is interested in a particular job:

scheduler.getListenerManager().addJobListener(myJobListener, KeyMatcher.jobKeyEquals(new JobKey("myJobName", "myJobGroup")));

 

You may want to use static imports for the matcher and key classes, which will make your defining the matchers cleaner:

你可能想为Matcher和关键的类使用静态引入,这些引入会让你更整洁的定义matcher。

 

import static org.quartz.JobKey.*;
import static org.quartz.impl.matchers.KeyMatcher.*;
import static org.quartz.impl.matchers.GroupMatcher.*;
import static org.quartz.impl.matchers.AndMatcher.*;
import static org.quartz.impl.matchers.OrMatcher.*;
import static org.quartz.impl.matchers.EverythingMatcher.*;

...etc.

 

Which turns the above example into this:
scheduler.getListenerManager().addJobListener(myJobListener, jobKeyEquals(jobKey("myJobName", "myJobGroup")));

 

Adding a JobListener that is interested in all jobs of a particular group:
scheduler.getListenerManager().addJobListener(myJobListener, jobGroupEquals("myJobGroup"));

 

Adding a JobListener that is interested in all jobs of two particular groups:
scheduler.getListenerManager().addJobListener(myJobListener, or(jobGroupEquals("myJobGroup"), jobGroupEquals("yourGroup")));

 

Adding a JobListener that is interested in all jobs:
scheduler.getListenerManager().addJobListener(myJobListener, allJobs());

…Registering TriggerListeners works in just the same way.

 

Listeners are not used by most users of Quartz, but are handy when application requirements create the need for the notification of events, without the Job itself having to explicitly notify the application.

 

大多数Quartz的用户都不使用侦听器,但是当应用程序需要基于事件触发时,就很方便了,而不需要Job自己明确的通知应用程序。

 

Pasted from <http://www.quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/tutorial-lesson-07.html>

 

猜你喜欢

转载自blog.csdn.net/arnolian/article/details/82528080