欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

quartz定时器

程序员文章站 2024-02-01 23:37:04
...
package com.wangxuegang.quartz;

import java.util.ArrayList;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.calendar.HolidayCalendar;

public class QuartzTest implements Job{

	public static void main(String[] args) throws Exception {
		
		
		/** 实例 1 start */
		case1();
		/** 实例 1 end */
		
		/** 实例 2 start */
		case2();
		/** 实例 2 end */
		
		/** 实例 3 start */
		case3();
		/** 实例 3 end */
		
	}
	
	//实例1
	public static void case1() throws Exception{
		
		//获取默认调度实例化对象
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
		//启动
		scheduler.start();
		//关闭
		scheduler.shutdown();
	}
	
	//实例2
	public static void case2() throws Exception{
		
		//创建调度器,并启动调度器
		StdSchedulerFactory ssf = new StdSchedulerFactory();
		Scheduler scheduler = ssf.getScheduler();
		scheduler.start();
		
		//创建任务,QuartzTest类实现Job接口
		JobDetail jobDetail = new JobDetail("myJob",null,QuartzTest.class);
		
		//JobDataMap集合实现map接口,用于存数据,JobExecutionContext对象接受数据
		jobDetail.getJobDataMap().put("jobSays", "Hello World!");
		jobDetail.getJobDataMap().put("myFloatValue", 3.141f);
		jobDetail.getJobDataMap().put("myStateData", new ArrayList()); 
		
		
		//每秒激活一次
		Trigger trigger = TriggerUtils.makeSecondlyTrigger();
		//在下一秒启动
		trigger.setStartTime(TriggerUtils.getEvenSecondDate(new Date()));
		trigger.setName("myTrigger");
		
		//调度任务
		scheduler.scheduleJob(jobDetail, trigger);
	}
	
	//实例3
	public static void case3() throws Exception{
		
		//创建日历对象
		HolidayCalendar cal = new HolidayCalendar();
		
		//排除节假日时间
		cal.addExcludedDate(new Date("Thu Mar 21 10:59:44 CST 2020"));
		
		//获取默认调度实例化对象
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
		//启动
		scheduler.start();
		
		//将排除节假日时间注册到调度器中
		scheduler.addCalendar("myHolidays", cal, false, false);
		
		//每秒激活一次
		Trigger trigger1 = TriggerUtils.makeSecondlyTrigger();
		//在下一秒启动
		trigger1.setStartTime(TriggerUtils.getEvenSecondDate(new Date()));
		trigger1.setName("myTrigger1");
		
		//用trigger1来安排任务
		trigger1.setCalendarName("myHolidays");
		
		//每天 8:00 触发
		Trigger trigger2 = TriggerUtils.makeDailyTrigger(8, 0); 
		//开始时间
		trigger2.setStartTime(new Date()); 
		trigger2.setName("myTrigger2");
		//用trigger2 来安排任务
		trigger2.setCalendarName("myHolidays");
		
		//创建任务,QuartzTest类实现Job接口
		JobDetail jobDetail1 = new JobDetail("myJob1",null,QuartzTest.class);
		JobDetail jobDetail2 = new JobDetail("myJob2",null,QuartzTest.class);
		
		//JobDataMap集合实现map接口,用于存数据,JobExecutionContext对象接受数据
		jobDetail1.getJobDataMap().put("jobSays", "Hello World!");
		jobDetail1.getJobDataMap().put("myFloatValue", 3.141f);
		jobDetail1.getJobDataMap().put("myStateData", new ArrayList()); 
		
		//JobDataMap集合实现map接口,用于存数据,JobExecutionContext对象接受数据
		jobDetail2.getJobDataMap().put("jobSays", "Hello World!");
		jobDetail2.getJobDataMap().put("myFloatValue", 3.141f);
		jobDetail2.getJobDataMap().put("myStateData", new ArrayList()); 
		
		//调度任务
		scheduler.scheduleJob(jobDetail1, trigger1);
		scheduler.scheduleJob(jobDetail2, trigger2);
		System.out.println(new Date());
	}

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		System.out.println("任务开始   strat");
		String instName = context.getJobDetail().getName();
		String instGroup = context.getJobDetail().getGroup();
		JobDataMap dataMap = context.getJobDetail().getJobDataMap(); 
		String jobSays = dataMap.getString("jobSays");
		float myFloatValue = dataMap.getFloat("myFloatValue");
		ArrayList state = (ArrayList)dataMap.get("myStateData");
		state.add(new Date());
		System.err.println("Instance " + instName + " of DumbJob says: " + jobSays); 
		System.out.println("任务开始   end");
	}

}

 

### 设置日志打印优先级 OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL ###
log4j.rootLogger = debug,console,debug,error,all

### 输出信息到控制抬 ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### 输出INFO 级别以上的日志到=C://logs/info.log ###
log4j.appender.debug = org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.File = C://logs/info.log
log4j.appender.debug.Append = true
log4j.appender.debug.Threshold = INFO 
log4j.appender.debug.layout = org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 输出ERROR 级别以上的日志到=C://logs/error.log ### 
log4j.appender.error = org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.File =C://logs/error.log 
log4j.appender.error.Append = true
log4j.appender.error.Threshold = ERROR 
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 输出ALL 级别以上的日志到=C://logs/all.log ### 
log4j.appender.all = org.apache.log4j.DailyRollingFileAppender
log4j.appender.all.File =C://logs/all.log 
log4j.appender.all.Append = true
log4j.appender.all.Threshold = ALL 
log4j.appender.all.layout = org.apache.log4j.PatternLayout
log4j.appender.all.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n