Quartz使用自己定义的listener和trigger调用

package com.taobao.terminator.allen.QuartzTest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.JobListener;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzTest {
	private final static Log logger = LogFactory.getLog(QuartzTest.class);
	public static void main(String[] args) {
		System.out.println("this is test for Quartz");
		//初始化scheduler工厂类
		SchedulerFactory factory = new StdSchedulerFactory();
		
		try {
			//DirectSchedulerFactory生成一个实例,可create一个带名字的shceduler
			Scheduler shceduler = factory.getScheduler();
			
			//Quartz监听器,分为全局和局部的。局部的需要在特定的jobDetail中重回一次,全局不需要
			JobListener jobListener = new QuartzListener("quartz - test");
			shceduler.addJobListener(jobListener);
			
			//Shceduler的Context类似于servlet的context,功能相似.Context会传递给监听器
			shceduler.getContext().put("coreName", "coreName");
			
			//同样可以保存context的key-value ,jobDetail.getJobDataMap()
			JobDetail jobDetail = new JobDetail("Full-Detail", "Full-Detail-Group", QuartzJob.class);
			jobDetail.addJobListener("quartz - test");
			jobDetail.getJobDataMap().put("jobDetail-data", "jobDetail");
			
			//生成触发器
			CronTrigger trigger = new CronTrigger("Full-Trigger" , "Full-Trigger-Group");
			trigger.setCronExpression("0/30 * * * * ?");
			
			//添加job
			shceduler.scheduleJob(jobDetail, trigger);
			
			//开始执行shceduler
			shceduler.start();
			
			while(true) {
				Thread.sleep(1000 * 10);
				System.out.println("触发定时任务");
				
				shceduler.triggerJob("Full-Detail", "Full-Detail-Group");
				
				Thread.sleep(1000 * 40);
				
				//终止正在运行的job
				shceduler.interrupt("Full-Detail", "Full-Detail-Group");
				System.out.println("the schedule is over");
				
				//关闭定时器
				shceduler.shutdown();
				
				break;
			}
		} catch (Exception e) {
			logger.error("生成调试器失败" , e);
		}
	}
}
<span style="font-family: Arial, Helvetica, sans-serif;">package com.taobao.terminator.allen.QuartzTest;</span>
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
public class QuartzListener implements JobListener{
	private String listenerName = "quartz-listener";
	
	public QuartzListener(String listenerName) {
		this.listenerName = listenerName;
	}
	
	public String getName() {
		return this.listenerName;
	}
	public void jobToBeExecuted(JobExecutionContext context) {
		System.out.println("the job listener is start");
		
	}
	public void jobExecutionVetoed(JobExecutionContext context) {
	}
	public void jobWasExecuted(JobExecutionContext context,
			JobExecutionException jobException) {
		System.out.println("the job listener is end");
	}
}

QuartzJob:注意中断方法的实现,可用于停止当前job,也可用shcedule.deleteJob(name,groupName)进行直接删除

import org.quartz.InterruptableJob;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.UnableToInterruptJobException;
public class QuartzJob implements InterruptableJob{
	private boolean stop = false ;
	public void execute(JobExecutionContext context) throws JobExecutionException {
		if(!this.stop) {
			System.out.println("this is ok");
		}
	}
	public void interrupt() throws UnableToInterruptJobException {
		this.stop = true ;
		System.out.println("it is over");
	}
	
	public boolean isStop() {
		return stop;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值