Quartz2.1.x案例


创建一个Quartz2.1.x案例

1.添加jar包,mvn配置

<dependency>
	<groupId>org.quartz-scheduler</groupId>
	<artifactId>quartz</artifactId>
	<version>2.1.6</version>
</dependency>


2.创建Job

package com.gbsoft.rpt;

import java.text.SimpleDateFormat;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * @description HelloJob.java
 * @class HelloJob
 * @author 不了了之
 * @QQ 2320055803
 * @date 2012-10-26
 */
public class HelloJob implements Job {
	
	public HelloJob() {
	}

	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("打印:" + sdf.format(new java.util.Date()));
	}
}

2.测试java代码

package com.gbsoft.rpt;

import org.quartz.DateBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @description QuartzJobTest.java
 * @class QuartzJobTest
 * @author 不了了之
 * @QQ 2320055803
 * @date 2012-10-25
 */
public class QuartzJobTest {

	public static void main(String[] args) {
		new QuartzJobTest().testOne();
	}
 
	private void testOne() {
 		try {
 			// 创建一个定时器工厂
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.start(); // 启动定时器
			
			// 创建一个简单的定时器生成器,该生成器根据一定的条件生成需要的定时器,比如按指定的时间间隔重复执行的简单定时器
			SimpleScheduleBuilder schedBuilder = SimpleScheduleBuilder
					.simpleSchedule().withIntervalInSeconds(1).repeatForever();
			
			// 创建一个触发器,DateBuilder负责创建复杂的任务
			Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")
					.startAt(DateBuilder.nextGivenSecondDate(null, 2))
					.withSchedule(schedBuilder).build();
			
			// 创建job明细,我们要执行的任务必须实现org.quartz.Job接口
			JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("job1", "group1").build();
			
			// 获取定时器
			// 添加job到定时器,需要添加JobDetail和trigger
		
			scheduler.scheduleJob(jobDetail, trigger);
			
			// 启动定时器
//			scheduler.shutdown();
			
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
}

案例二:

定义多个Trigger和JobDetail实例

	// 1.1 创建一个JobDetail实例,指定SimpleJob  
        JobDetail jobDetail1 = JobBuilder.newJob(SimpleJob.class).withIdentity("job1-1", "group1").build();  
        // 1.2 通过SimpleTrigger定义调度规则:马上启动,每1秒运行一次,共运行10次  
        Trigger simpleTrigger1 = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")
        .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).withRepeatCount(10)).startAt(new java.util.Date()).build();  

     
        
        // 2.1 创建一个JobDetail实例,指定SimpleJob  
        JobDetail jobDetail2 = JobBuilder.newJob(SimpleJob.class).withIdentity("job1_2", "group1").build();  

        // 2.2 通过SimpleTrigger定义调度规则:马上启动,每2秒运行一次,共运行3次  
        Trigger simpleTrigger2 = TriggerBuilder.newTrigger().withIdentity("trigger1_2", "group1")
        .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).withRepeatCount(3)).startNow().build();  
 
        
        
        // 3.0 通过SchedulerFactory获取一个调度器实例  
        Scheduler scheduler;
		try {
			scheduler = StdSchedulerFactory.getDefaultScheduler();
			
			scheduler.scheduleJob(jobDetail1, simpleTrigger1); // 注册并进行调度  
			scheduler.scheduleJob(jobDetail2, simpleTrigger2); // 注册并进行调度  
			
			scheduler.start(); // 调度启动  
		} catch (SchedulerException e) {
			e.printStackTrace();
		}  



访问地址:http://www.quartz-scheduler.org/documentation/quartz-2.1.x/quick-start


你可能感兴趣的:(Quartz2.1.x案例)