Quartz使用

阅读更多

 

官网: http://quartz-scheduler.org/downloads/catalog?activated=true

 

Quartz核心的概念scheduler任务调度、Job任务、Trigger触发器、JobDetail任务细节

 

1 创建一个实现接口Job的类,其中的方法execute是每个任务需要执行的事情

2 创建 Scheduler

3 创建 JobDetail,这个类要注册 1)Job名称 2)Job 组别   3) 实现了Job接口的类  

4 创建 Trigger,这个Trigger有两种 1)简单Trigger 2) 基于日历级别的CronTrigger

CronTrigger 中表达式的使用: http://username2.iteye.com/admin/blogs/1825347

5 在Scheeduler中注册 JobDetail和Trigger  

 

代码:

1 Job接口的实现类

 

package Quartz;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * @author root
 */
public class ScanDirectoryJob implements Job{

	//记录日志 
	static Log logger = LogFactory.getLog( ScanDirectoryJob.class);
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		 
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		logger.info( "Thread ==="+Thread.currentThread() );
		//Every job has its own job detail 
		JobDetail jobDetail = context.getJobDetail();
		// 名字被定义在job定义的时候 
		String jobName = jobDetail.getName();
		
		logger.info(jobName+"  fired at   "+new Date());
		JobDataMap dataMap = jobDetail.getJobDataMap();
		
		 String dirName = dataMap.getString("SCAN_DIR");
		 logger.info("dirname ===  "+dirName);
	}
}

 

2 Cron trigger 例子 

package Quartz;

import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

/**
 * 使用     Cron trigger 
 * @author root
 *
 */
public class CronQuartz {

	public static void main(String[] args) {
		CronQuartz cq = new CronQuartz();
		try {
			cq.task();
		} catch ( Exception e) {
			e.printStackTrace();
		}
	}

    public void task() throws  Exception
    {
        // Initiate a Schedule Factory
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        // Retrieve a scheduler from schedule factory
        Scheduler scheduler = schedulerFactory.getScheduler();
        
        // Initiate JobDetail with job name, job group, and executable job class
        JobDetail jobDetail = 
        	new JobDetail("jobDetail2", "jobDetailGroup2", ScanDirectoryJob.class );
        // Configure the directory to scan       
        jobDetail.getJobDataMap().put("SCAN_DIR", "tempDir");  
        
        // Initiate CronTrigger with its name and group name
        CronTrigger cronTrigger = new CronTrigger("cronTrigger", "triggerGroup2");
        // setup CronExpression
        CronExpression cexp = new CronExpression("0/5 * * * * ?");
        // Assign the CronExpression to CronTrigger
        cronTrigger.setCronExpression(cexp);
        
        
        // schedule a job with JobDetail and Trigger
        scheduler.scheduleJob(jobDetail, cronTrigger);
        // start the scheduler
        scheduler.start();
    }
    
}
 

 3 简单Trigger,通常不用

package Quartz;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;
//编程式任务处理
/**
 * 
 * 使用    simple trigger 
 * http://liuzidong.iteye.com/blog/1121683
 * @author root
 *1 创建 Scheduler
 *2 创建 JobDetail,这个类要注册 1)Job名称 2)Job 组别   3) 实现了Job接口的类 
 *3 创建 Trigger
 *4 在Scheeduler中注册 JobDetail和Trigger  
 */
public class SimpleScheduler {

	static Log logger = LogFactory.getLog( SimpleScheduler.class);
	public static void main(String[] args) throws SchedulerException {
		
		SimpleScheduler simple = new SimpleScheduler();
		Scheduler  scheduler = StdSchedulerFactory.getDefaultScheduler();
		scheduler.start();
		
		logger.info(" Scheduler started at "+new Date());

        // Schedule the first Job       
        simple.scheduleJob(scheduler, "ScanDirectory1",ScanDirectoryJob.class,"D:\\conf1", 10);          
        // Schedule the second Job       
        simple.scheduleJob(scheduler, "ScanDirectory2",ScanDirectoryJob.class,"D:\\conf2 ", 15);  
        
	}

    private void scheduleJob(Scheduler scheduler, String jobName,Class jobClass, String scanDir, int scanInterval) throws SchedulerException 
    {         

        // Create a JobDetail for the Job       
       JobDetail jobDetail = new JobDetail(jobName,Scheduler.DEFAULT_GROUP, jobClass);         

       // Configure the directory to scan       
       jobDetail.getJobDataMap().put("SCAN_DIR", scanDir);         

       // Trigger that repeats every "scanInterval" secs forever       

       Trigger trigger = TriggerUtils.makeSecondlyTrigger(scanInterval);         
       trigger.setName(jobName + "-Trigger");         
       // Start the trigger firing from now       
       trigger.setStartTime(new Date());         

       // Associate the trigger with the job in the scheduler       
       scheduler.scheduleJob(jobDetail, trigger);       

  }  
}

 

 

你可能感兴趣的:(Quartz使用)