论坛首页 入门技术论坛

Quartz的简单示例

浏览 1432 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-03-03  

  Quartz是开源任务调度框架中使用最为广泛的,最近开始学习quartz这方面的东西,看了看quartz的说明,写了个小例子:

  Job实现类:   

package com.quartz;

import java.util.*;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class SimpleJob implements Job {
	public void execute(JobExecutionContext jobCtx) throws JobExecutionException {
		System.out.println(jobCtx.getTrigger().getName() + " triggered.time is: " + (new Date()));
	}
}

 

 

   对Job实现调度:

 

  

package com.quartz;

import java.util.*;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

public class SimpleTriggerRunner {
	public static void main(String[] args) {
		try {
			JobDetail jobDetail = new JobDetail("job1_1", "jGroup1", SimpleJob.class);

			SimpleTrigger simpleTrigger = new SimpleTrigger("trigger1_1", "tgroup1");
			simpleTrigger.setJobGroup("jGroup1");
			simpleTrigger.setJobName("job1_1");
			simpleTrigger.setStartTime(new Date());
			simpleTrigger.setRepeatInterval(2000);
			simpleTrigger.setRepeatCount(10);

			SchedulerFactory schedulerFactory = new StdSchedulerFactory();
			Scheduler scheduler = schedulerFactory.getScheduler();

			scheduler.addJob(jobDetail, true);
			scheduler.scheduleJob(simpleTrigger);
			scheduler.start();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

 

任务调度运行结果:

  

trigger1_1 triggered.time is: Tue Mar 03 14:37:37 CST 2009
trigger1_1 triggered.time is: Tue Mar 03 14:37:37 CST 2009
trigger1_1 triggered.time is: Tue Mar 03 14:37:37 CST 2009
trigger1_1 triggered.time is: Tue Mar 03 14:37:39 CST 2009
trigger1_1 triggered.time is: Tue Mar 03 14:37:41 CST 2009
trigger1_1 triggered.time is: Tue Mar 03 14:37:37 CST 2009
trigger1_1 triggered.time is: Tue Mar 03 14:37:37 CST 2009
trigger1_1 triggered.time is: Tue Mar 03 14:37:37 CST 2009
trigger1_1 triggered.time is: Tue Mar 03 14:37:39 CST 2009
trigger1_1 triggered.time is: Tue Mar 03 14:37:41 CST 2009
trigger1_1 triggered.time is: Tue Mar 03 14:37:41 CST 2009

 

 

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics