ㄓㄤㄑㄧㄤ

Quartz调用大全

Quartz调用大全

 

1.Quartz应用范围广泛,可单独执行也可在spring中嵌入执行。

   类似的定时任务在linux下可以用crontab执行

 

 

2.实现代码:

QuartzTest :主要执行类

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);
        }
    }
}

QuartzListener :监听器,可设置在jobDetail ,trigger, scheduler三个层次

                          主要对job执行之前及之后的进行操作,context可负责通信及异常,类似于servlet的context功能

package com.taobao.terminator.allen.QuartzTest;
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;
    }
}

转自:http://blog.csdn.net/flyingpig4/article/details/6263560

posted @ 2014-10-24 16:58  ㄓㄤㄑㄧㄤ  阅读(348)  评论(0编辑  收藏  举报
哈哈,页脚部分。