Quartz教程:Scheduler及事件机制

(1)     Scheduler

在这之前,我们创建Scheduler都是通过下面的语句:

Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

这使用的是默认的调度器,它会依据quartz.properties配置文件为我们设置好相关的属性,包括使用的调度器、线程池、线程池中线程数、采用的JobStore等等。如果我们在自己应用的classpath中写了quartz.properties,则SchedulerFactory会使用它的配置,否则它会使用Quartz的jar包中的配置文件。

Jar包中提供的quartz.properties如:

# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#

org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false

org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true

org.quartz.jobStore.misfireThreshold: 60000

org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore

通过查看前面部分的示例程序,查看其输出的日志信息,也可说明这一点:

288 [main] INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor
293 [main] INFO org.quartz.simpl.SimpleThreadPool - Job execution threads will use class loader of thread: main
345 [main] INFO org.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
347 [main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.1.7 created.
358 [main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized.
359 [main] INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.1.7) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

359 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
360 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.1.7
362 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.

如果要使用特殊的配置文件(如命名不同等),可通过StdSchedulerFactory的initialize()函数进行初始化,如:

StdSchedulerFactory factory = new StdSchedulerFactory();
factory.initialize("jmatrix_quartz.properties");
Scheduler scheduler = factory.getScheduler();

 

(2)     事件机制

Quartz为Job,Trigger和Scheduler都提供了对应的事件接口,它们分别为:

public abstract interface JobListener
{
  public abstract String getName();

  public abstract void jobToBeExecuted(JobExecutionContext paramJobExecutionContext);

  public abstract void jobExecutionVetoed(JobExecutionContext paramJobExecutionContext);

  public abstract void jobWasExecuted(JobExecutionContext paramJobExecutionContext, JobExecutionException paramJobExecutionException);
}

可监听的事件有,Job开始执行、执行完成、停止。

public abstract interface TriggerListener
{
  public abstract String getName();

  public abstract void triggerFired(Trigger paramTrigger, JobExecutionContext paramJobExecutionContext);

  public abstract boolean vetoJobExecution(Trigger paramTrigger, JobExecutionContext paramJobExecutionContext);

  public abstract void triggerMisfired(Trigger paramTrigger);

  public abstract void triggerComplete(Trigger paramTrigger, JobExecutionContext paramJobExecutionContext, Trigger.CompletedExecutionInstruction paramCompletedExecutionInstruction);
}

可监听的事件有:trigger触发,trigger错过执行,trigger执行完成和停止。

public abstract interface SchedulerListener
{
  public abstract void jobScheduled(Trigger paramTrigger);

  public abstract void jobUnscheduled(TriggerKey paramTriggerKey);

  public abstract void triggerFinalized(Trigger paramTrigger);

  public abstract void triggerPaused(TriggerKey paramTriggerKey);

  public abstract void triggersPaused(String paramString);

  public abstract void triggerResumed(TriggerKey paramTriggerKey);

  public abstract void triggersResumed(String paramString);

  public abstract void jobAdded(JobDetail paramJobDetail);

  public abstract void jobDeleted(JobKey paramJobKey);

  public abstract void jobPaused(JobKey paramJobKey);

  public abstract void jobsPaused(String paramString);

  public abstract void jobResumed(JobKey paramJobKey);

  public abstract void jobsResumed(String paramString);

  public abstract void schedulerError(String paramString, SchedulerException paramSchedulerException);

  public abstract void schedulerInStandbyMode();

  public abstract void schedulerStarted();

  public abstract void schedulerShutdown();

  public abstract void schedulerShuttingdown();

  public abstract void schedulingDataCleared();
}

Scheduler可监听的事件就多了:Job/Trigger的添加与移除、Scheduler错误的发生、Scheduler的关闭等等。

想要创建一个自己的监听器,只需创建一个类并让它实现对应的Listener接口即可,不过由于直接实现接口需要实现所有的接口方法,所以更推荐让自己的监听器类继承各个Listener接口对应的Support类:JobListenerSupport,TriggerListenerSupport和SchedulerListenerSupport,这些Support类提供所有接口方法的默认实现(即不做任何事情),因此我们创建的监听器只需实现自己感兴趣的事件相关方法即可。

一个简单的listener示例:

public class SimpleJobListener extends JobListenerSupport {

	public String getName() {
		return SimpleJobListener.class.getName();
	}

	public void jobToBeExecuted(JobExecutionContext context) {
		System.out.println("开始执行SimpleJob");
	}

	public void jobWasExecuted(JobExecutionContext context,
			JobExecutionException jobException) {
		System.out.println("SimpleJob执行完成");
	}
}

创建好自己的listener后,我们需要将它添加到Schedule中,这个可以通过ListenerManager的addJobListener()函数进行:

// 创建Scheduler
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

scheduler.start();

//添加Listener
SimpleJobListener listener = new SimpleJobListener();
scheduler.getListenerManager().addJobListener(listener, KeyMatcher.keyEquals(JobKey.jobKey("simpleJob","group1")));

需要注意的是,这里addJobListener()函数的第二个参数是Matcher接口,通过它来决定此Listener是与那个Job,那个group,亦或所有的Job关联,上面的例子是直接关联到simpleJob。关于Quartz提供的一系列Matcher实现,可以查看org.quartz.impl.matchers包。

 

如果未说明,本Blog中文章皆为原创文章,请尊重他人劳动,转载请注明: 转载自jmatrix

本文链接地址: Quartz教程:Scheduler及事件机制

(注:一般引用了,我都会添加引用,如果有侵权的请联系我)



This entry was posted in Java and tagged . Bookmark the permalink. Follow any comments here with the RSS feed for this post. Trackbacks are closed, but you can post a comment.