SimpleTrigger、CronTrigger、SchedulerListener、TriggerListener、JobListener区别

SimpleTrigger的属性包括:开始时间、结束时间、重复次数以及重复的间隔   
CronTrigger是基于日历表示法的调度,而不是基于指定间隔的简单调度

listener是一个对象,用于监听scheduler中发生的事件,然后执行相应的操作,TriggerListeners接受与trigger相关的事件,JobListeners接受与jobs相关的事件。
trigger相关的事件包括:trigger的触发、trigger错过触发(mis-fire)以及trigger的完成(即trigger触发的job执行完成)。
job相关的事件包括:job即将执行的通知以及job执行完毕的通知。

SchedulerListener与TriggerListener、JobListener类似,但它仅接收来自Scheduler自身的消息,而不一定是某个具体的trigger或job的消息。

scheduler相关的消息包括:job/trigger的增加、job/trigger的删除、scheduler内部发生的严重错误以及scheduler关闭的消息等;

package com.quartz;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;

public class HelloJobListener implements JobListener {

	public static final String LISTENER_NAME = "dummyJobListenerName";

	@Override
	public String getName() {
		return LISTENER_NAME; // must return a name
//		return null; //Exception in thread "main" java.lang.IllegalArgumentException: JobListener name cannot be empty.
	}

	// Run this if job is about to be executed.
	@Override
	public void jobToBeExecuted(JobExecutionContext context) {

		String jobName = context.getJobDetail().getKey().toString();
		String triggerName = context.getTrigger().getKey().toString();
		System.out.println("jobToBeExecuted");
		System.out.println("Job : " + jobName+",triggerName:" +triggerName + " is going to start...");

	}

	// No idea when will run this?
	@Override
	public void jobExecutionVetoed(JobExecutionContext context) {
		System.out.println("jobExecutionVetoed");
	}

	// Run this after job has been executed
	@Override
	public void jobWasExecuted(JobExecutionContext context,
			JobExecutionException jobException) {
		System.out.println("jobWasExecuted");

		String jobName = context.getJobDetail().getKey().toString();
		System.out.println("Job : " + jobName + " is finished...");

		if (!jobException.getMessage().equals("")) {
			System.out.println("Exception thrown by: " + jobName
					+ " Exception: " + jobException.getMessage()+"\n\n");
		}

	}
}
package com.quartz;

import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.Trigger.CompletedExecutionInstruction;
import org.quartz.TriggerListener;

public class HelloTriggerListener implements TriggerListener {

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void triggerComplete(Trigger arg0, JobExecutionContext arg1,
			CompletedExecutionInstruction arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void triggerFired(Trigger arg0, JobExecutionContext arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void triggerMisfired(Trigger arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean vetoJobExecution(Trigger arg0, JobExecutionContext arg1) {
		// TODO Auto-generated method stub
		return false;
	}

}
package com.quartz;

import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.SchedulerException;
import org.quartz.SchedulerListener;
import org.quartz.Trigger;
import org.quartz.TriggerKey;

public class HelloSchedulerListener  implements SchedulerListener{

	@Override
	public void jobAdded(JobDetail arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void jobDeleted(JobKey arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void jobPaused(JobKey arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void jobResumed(JobKey arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void jobScheduled(Trigger arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void jobUnscheduled(TriggerKey arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void jobsPaused(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void jobsResumed(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void schedulerError(String arg0, SchedulerException arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void schedulerInStandbyMode() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void schedulerShutdown() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void schedulerShuttingdown() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void schedulerStarted() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void schedulerStarting() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void schedulingDataCleared() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void triggerFinalized(Trigger arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void triggerPaused(TriggerKey arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void triggerResumed(TriggerKey arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void triggersPaused(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void triggersResumed(String arg0) {
		// TODO Auto-generated method stub
		
	}

}
参考:

http://blog.csdn.net/u010002184/article/details/72585586

https://nkcoder.github.io/2014/08/17/quartz-tutorial-07-listener/ 




相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页