Quartz任务调度服务基础——创建简单任务

Quartz是一款全功能的开源的任务调度服务。

官方网址:http://www.quartz-scheduler.org/

文中使用的Quartz是1.8.6版本,基础依赖包有:
Quartz任务调度服务基础——创建简单任务

具体使用见代码:

/**
 * @author yanlei
 * 任务执行类,必须实现Job接口
 */
public class FirstJob implements Job {

	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		// 通过任务执行体获取任务数据
		JobDetail jobDetail = context.getJobDetail();
		
		// 输出任务的一些信息
		System.out.println("Job's information:");
		System.out.println("Name:" + jobDetail.getName());
		System.out.println("GroupName:" + jobDetail.getGroup());
	}

}
/**
 * Quartz任务实例
 * @author yanlei
 */
public class JobTest {
	private static final String jobName = "firstJob";
	private static final String jobGroupName = "GROUP_1";
	private static final String triggerName = "trigger_1";
	private static final String triggerGroupName = "trigger_group_1";

	public static void main(String[] args) {

		try {
			// 获取调度器
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

			// 创建任务数据,FirstJob是任务的执行类
			JobDetail jobDetail = new JobDetail(jobName, jobGroupName,
					FirstJob.class);

			/*
			 * 将任务放入调度器,并设置触发器 SimpleTrigger是Quartz支持的四类触发器之一,具体信息参考API
			 */
			scheduler.scheduleJob(jobDetail, new SimpleTrigger(triggerName,
					triggerGroupName));

			// 开启任务调度器
			scheduler.start();

			System.out.println("Job is running");

			// 停止任务调度器
			scheduler.shutdown();
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

}
输出信息:

[INFO] [2013-07-03 18:19:55,300] [org.quartz.impl.StdSchedulerFactory.instantiate:1179] - Using default implementation for ThreadExecutor [INFO] [2013-07-03 18:19:55,327] [org.quartz.simpl.SimpleThreadPool.initialize:270] - Job execution threads will use class loader of thread: main [INFO] [2013-07-03 18:19:55,390] [org.quartz.core.SchedulerSignalerImpl.<init>:60] - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl [INFO] [2013-07-03 18:19:55,392] [org.quartz.core.QuartzScheduler.<init>:229] - Quartz Scheduler v.1.8.6 created. [INFO] [2013-07-03 18:19:55,394] [org.quartz.simpl.RAMJobStore.initialize:139] - RAMJobStore initialized. [INFO] [2013-07-03 18:19:55,411] [org.quartz.core.QuartzScheduler.initialize:255] - Scheduler meta-data: Quartz Scheduler (v1.8.6) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED' Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally. NOT STARTED. Currently in standby mode. Number of jobs executed: 0 Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads. Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. [INFO] [2013-07-03 18:19:55,412] [org.quartz.impl.StdSchedulerFactory.instantiate:1324] - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties' [INFO] [2013-07-03 18:19:55,418] [org.quartz.impl.StdSchedulerFactory.instantiate:1328] - Quartz scheduler version: 1.8.6 [INFO] [2013-07-03 18:19:55,422] [org.quartz.core.QuartzScheduler.start:519] - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started. Job is running [INFO] [2013-07-03 18:19:55,423] [org.quartz.core.QuartzScheduler.shutdown:635] - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down. [INFO] [2013-07-03 18:19:55,423] [org.quartz.core.QuartzScheduler.standby:556] - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused. [INFO] [2013-07-03 18:19:55,446] [org.quartz.core.QuartzScheduler.shutdown:707] - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete. Job's information: Name:firstJob GroupName:GROUP_1


你可能感兴趣的:(Quartz任务调度服务基础——创建简单任务)