- quartz是开发经常使用的一种调度框架,具体介绍可以自行搜索。
- 本文介绍quartz的简单使用如下:
首先创建一个基本的Job类
//quartz定义了一种Job,用于作为调度执行的方法
public class BaseJob implements Job{
Logger log = LoggerFactory.getLogger(Job.class);
public BaseJob() {
}
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");
//实现Job的execute方法,入参为JobExecutionContext对象,本文不做此对象的介绍
public void execute(JobExecutionContext context) throws JobExecutionException {
log.info(simpleDateFormat.format(new Date()) + ":");
log.info(context.getTrigger().getKey().getGroup());
log.info(context.getTrigger().getKey().getName());
}
}
然后创建一个实际调度的类:
//具体的调度执行的类
public class BaseUse {
Logger log = LoggerFactory.getLogger(BaseUse.class);
public static void main(String[] args) throws Exception {
//首先要通过工厂方法schedulerFactory的一个实现获取一个Scheduler对象作为调度器
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
//获取一个下一分钟的调度时间
Date runTime = evenMinuteDate(new Date());
//每个job都归于一个name和一个group作为任务的坐标,创建一个BaseJob的JobDetail
JobDetail detail = newJob(BaseJob.class).withIdentity("job1", "group1").build();
//每个trigger都归于一个name和一个group作为的触发器的坐标,创建触发器Trigger
Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();
//调度器按JobDetail 、Trigger初始化调度器,相当于加入调度队列
scheduler.scheduleJob(detail, trigger);
//开启调度
scheduler.start();
TimeUnit.MINUTES.sleep(1);
//结束调度
scheduler.shutdown();
}
}
- 执行过程:
StdSchedulerFactory使用了一个ThreadExecutor的缺省实现,Job执行线程将使用main线程的ClassLoader,创建完成Quartz Scheduler v.2.2.1,RAMJobStore (Job内存存储)初始化完成,调度器元数据为:非集群模式的DefaultQuartzScheduler,未开始并处于待命状态,执行过的任务数0,使用SimpleThreadPool线程池维护10个线程,使用RAMJobStore任务存储(不支持持久化,同时非集群),DefaultQuartzScheduler使用quartz.properties配置文件初始化完成,调度启动,有一个触发器,触发执行group1.job1任务,执行完毕后关闭调度,然后关闭线程池,退出。
15:33:54,972 INFO StdSchedulerFactory:1184 - Using default implementation for ThreadExecutor
15:33:54,976 INFO SimpleThreadPool:268 - Job execution threads will use class loader of thread: main
15:33:54,985 INFO SchedulerSignalerImpl:61 - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
15:33:54,986 INFO QuartzScheduler:240 - Quartz Scheduler v.2.2.1 created.
15:33:54,987 INFO RAMJobStore:155 - RAMJobStore initialized.
15:33:54,988 INFO QuartzScheduler:305 - Scheduler meta-data: Quartz Scheduler (v2.2.1) '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.
15:33:54,988 INFO StdSchedulerFactory:1339 - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
15:33:54,988 INFO StdSchedulerFactory:1343 - Quartz scheduler version: 2.2.1
15:33:54,993 INFO QuartzScheduler:575 - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
15:33:54,994 DEBUG QuartzSchedulerThread:276 - batch acquisition of 1 triggers
15:33:55,986 DEBUG UpdateChecker:54 - Checking for available updated version of Quartz...
15:34:00,006 DEBUG PropertySettingJobFactory:51 - Producing instance of Job 'group1.job1', class=com.wqw.study.quartz.practice.BaseJob
15:34:00,019 DEBUG QuartzSchedulerThread:276 - batch acquisition of 0 triggers
15:34:00,020 DEBUG JobRunShell:201 - Calling execute on job group1.job1
15:34:00,021 INFO Job:24 - 2016-04-105 03:34:00:
15:34:00,021 INFO Job:25 - group1
15:34:00,025 INFO Job:26 - trigger1
15:34:29,310 DEBUG QuartzSchedulerThread:276 - batch acquisition of 0 triggers
15:34:54,966 DEBUG QuartzSchedulerThread:276 - batch acquisition of 0 triggers
15:34:54,993 INFO QuartzScheduler:694 - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down.
15:34:54,993 INFO QuartzScheduler:613 - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused.
15:34:54,994 DEBUG SimpleThreadPool:328 - Shutting down threadpool...
15:34:54,994 DEBUG SimpleThreadPool:395 - Shutdown of threadpool complete.
15:34:54,994 INFO QuartzScheduler:771 - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete.
15:34:55,030 DEBUG SimpleThreadPool:612 - WorkerThread is shut down.
15:34:55,480 DEBUG SimpleThreadPool:612 - WorkerThread is shut down.
15:34:55,480 DEBUG SimpleThreadPool:612 - WorkerThread is shut down.
15:34:55,480 DEBUG SimpleThreadPool:612 - WorkerThread is shut down.
15:34:55,480 DEBUG SimpleThreadPool:612 - WorkerThread is shut down.
15:34:55,480 DEBUG SimpleThreadPool:612 - WorkerThread is shut down.
15:34:55,481 DEBUG SimpleThreadPool:612 - WorkerThread is shut down.
15:34:55,481 DEBUG SimpleThreadPool:612 - WorkerThread is shut down.
15:34:55,481 DEBUG SimpleThreadPool:612 - WorkerThread is shut down.
15:34:55,482 DEBUG SimpleThreadPool:612 - WorkerThread is shut down.