在线二区人妖系列_国产亚洲欧美日韩在线一区_国产一级婬片视频免费看_精品少妇一区二区三区在线

鍍金池/ 教程/ Java/ Quartz2作業(yè)監(jiān)聽(tīng)
Quartz教程
Quartz2作業(yè)監(jiān)聽(tīng)
Quartz特點(diǎn)
Quartz執(zhí)行多作業(yè)
Quartz2第一個(gè)程序
Quartz列出調(diào)度器所有作業(yè)

Quartz2作業(yè)監(jiān)聽(tīng)

在本教程中,我們將展示/介紹如何創(chuàng)建一個(gè)JobListener,跟蹤運(yùn)行工作狀態(tài)在作業(yè)完成等。

P.S 這個(gè)例子是Quartz 2.1.5

1. Quartz 作業(yè)

作業(yè) - 用于打印一個(gè)簡(jiǎn)單的信息,并拋出一個(gè)JobExecutionException進(jìn)行測(cè)試。

File : HelloJob.java

package com.yiibai;
 
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
public class HelloJob implements Job
{
	public void execute(JobExecutionContext context)
	throws JobExecutionException {
 
		System.out.println("Hello Quartz! - by yiibai.com");	
 
		//Throw exception for testing
		throw new JobExecutionException("Testing Exception");
	}
 
}

2. JobListener

創(chuàng)建一個(gè)JobListener,只是實(shí)現(xiàn)了JobListener接口,并覆蓋所有的接口的方法。

File : HelloJobListener.java

package com.yiibai.quartz.listener;
 
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
	}
 
	// Run this if job is about to be executed.
	@Override
	public void jobToBeExecuted(JobExecutionContext context) {
 
		String jobName = context.getJobDetail().getKey().toString();
		System.out.println("jobToBeExecuted");
		System.out.println("Job : " + jobName + " 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());
		}
 
	}
 
}
注意:
不知道什么是“jobExecutionVetoed”,并會(huì)在何時(shí)觸發(fā)?

3. CronTrigger

例如上面HelloJobListener連接到調(diào)度和監(jiān)控作業(yè)的狀態(tài)。

File : CronTriggerExample.java

package com.yiibai.quartz;
 
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.KeyMatcher;
 
import com.yiibai.quartz.listener.HelloJobListener;
 
public class CronTriggerExample {
    public static void main( String[] args ) throws Exception
    {
 
	JobKey jobKey = new JobKey("dummyJobName", "group1");
    	JobDetail job = JobBuilder.newJob(HelloJob.class)
		.withIdentity(jobKey).build();
 
    	Trigger trigger = TriggerBuilder
		.newTrigger()
		.withIdentity("dummyTriggerName", "group1")
		.withSchedule(
			CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
		.build();
 
    	Scheduler scheduler = new StdSchedulerFactory().getScheduler();
 
    	//Listener attached to jobKey
    	scheduler.getListenerManager().addJobListener(
    		new HelloJobListener(), KeyMatcher.keyEquals(jobKey)
    	);
 
    	//Listener attached to group named "group 1" only.
    	//scheduler.getListenerManager().addJobListener(
    	//	new HelloJobListener(), GroupMatcher.jobGroupEquals("group1")
    	//);
 
    	scheduler.start();
    	scheduler.scheduleJob(job, trigger);
 
    }
}

運(yùn)行CronTriggerExample.java, 這里是輸出結(jié)果:

jobToBeExecuted
Job : group1.dummyJobName is going to start...
Hello Quartz! - by yiibai.com
jobWasExecuted
Job : group1.dummyJobName is started and finished...
Exception thrown by: group1.dummyJobName Exception: Testing Exception
 
jobToBeExecuted
Job : group1.dummyJobName is going to start...
Hello Quartz! - by yiibai.com
jobWasExecuted
Job : group1.dummyJobName is started and finished...
Exception thrown by: group1.dummyJobName Exception: Testing Exception
以下代碼下載地址:http://pan.baidu.com/s/1pJ6xiwb