quartz 立即执行+定时+防止并发+监听Job是否运行状态

这文章没有新手教程,只有个人一些笔记,新手请下载附件教程:

SimpleRun:

package cn.wa8.qweb.extract.quartz;


import java.util.Date;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.impl.StdScheduler;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import cn.wa8.qweb.extract.action.Extract2DB;
public class SimpleRun {

	private static Logger log = LoggerFactory.getLogger(SimpleRun.class);

	public void run() throws Exception {
		SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
    	Scheduler sched = schedFact.getScheduler();
    	JobDetail jobDetail = new JobDetail("myJob",null,SimpleJob.class);
    	SimpleTrigger trigger = new SimpleTrigger("myTrigger",
    			null,
    			new Date(),
    			null,
    			SimpleTrigger.REPEAT_INDEFINITELY,
    			30L * 1000L);
    	
    	sched.scheduleJob(jobDetail, trigger);
    	//sched.addJobListener(new MyTriggerListener());
    	SimpleJob.preDate = new Date();
    	sched.start();
    	System.out.println("starting");
 /**
  * 
		state的值代表该任务触发器的状态:
		STATE_BLOCKED 	4 // 运行
		STATE_COMPLETE 	2  //完成那一刻,不过一般不用这个判断Job状态
		STATE_ERROR 	3  // 错误
		STATE_NONE 	-1		//未知
		STATE_NORMAL 	0   //正常无任务,用这个判断Job是否在运行
		STATE_PAUSED 	1	//暂停状态
 */
    while (true){
    	if(4 == sched.getTriggerState("myTrigger", null)){
    		System.out.println("running");
    	}else if(0 == sched.getTriggerState("myTrigger", null)){
    		System.out.println("ending");
    	}else {
    		System.out.println("error state:"+sched.getTriggerState("myTrigger", null));
    	}
    	try {
			Thread.sleep(5*1000);
		} catch (Exception e) {
			// TODO: handle exception
		}
    }
}

	public static void main(String[] args)  {
		
		SimpleRun simpleRun = new SimpleRun();
		
		try {
			simpleRun.run();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Simple Job:


import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.wa8.qweb.extract.action.Extract2DB;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;
/*Extract2DB extract2db = new Extract2DB();
	extract2db.CommonBaseExtract();*/

public class SimpleJob  implements StatefulJob{
	public static Date preDate ;
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		System.out.println("into Job");
		Date currentDate = new Date();
		Long s = (currentDate.getTime()-preDate.getTime())/1000;
		try {
			Thread.sleep(10*1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(s);
		System.out.println("leave Job:"+Thread.currentThread().toString());
		preDate =currentDate;
	}

}






你可能感兴趣的:(项目管理工具)