Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制。今天我们就来学习一下它的使用,这里会分篇章对它进行介绍。只是希望能有个人,在我说没事的时候,知道我不是真的没事;能有个人,在我强颜欢笑的时候,知道我不是真的开心。
quartz的使用案例
我的测试环境用的是maven,这次的测试代码是作为一个maven模块编写的(可能引用了父模块的jar依赖),首先添加quartz的依赖。
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.3.0</version> </dependency>
项目结构如下:
一、quartz的配置,这个不是必须的但是比较有用
quartz是可以灵活配置的,而最好的方式就是在应用的classpath上创建quartz.properties文件进行配置。详细的配置项,可以参考:http://www.quartz-scheduler.org/documentation/quartz-2.1.x/configuration/。下面的内容是我们本次测试的配置。
# This scheduler’s name will be “MyScheduler”. org.quartz.scheduler.instanceName = MyScheduler # There are 3 threads in the thread pool, which means that a maximum of 3 jobs can be run simultaneously. org.quartz.threadPool.threadCount = 3 # All of Quartz’s data, such as details of jobs and triggers, is held in memory (rather than in a database) org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
二、开始我们的程序的编写
package com.linux.huhx.example1; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.impl.StdSchedulerFactory; /** * @Author: huhx * @Date: 2017-11-23 上午 8:59 */ public class QuartzTest { public static void main(String[] args) { try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.start(); scheduler.shutdown(); } catch (SchedulerException e) { e.printStackTrace(); } } }
运行上述的代码,如果已经配置了日志,可以看到如下的控制台输出。
09:23:18.593 [main] INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor 09:23:18.624 [main] INFO o.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl 09:23:18.624 [main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.3.0 created. 09:23:18.640 [main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized. 09:23:18.640 [main] INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.3.0) 'MyScheduler' 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 3 threads. Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. 09:23:18.640 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'MyScheduler' initialized from default resource file in Quartz package: 'quartz.properties' 09:23:18.640 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.3.0 09:23:18.640 [MyScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers 09:23:18.640 [main] INFO org.quartz.core.QuartzScheduler - Scheduler MyScheduler_$_NON_CLUSTERED started. 09:23:18.640 [main] INFO org.quartz.core.QuartzScheduler - Scheduler MyScheduler_$_NON_CLUSTERED shutting down. 09:23:18.640 [main] INFO org.quartz.core.QuartzScheduler - Scheduler MyScheduler_$_NON_CLUSTERED paused. 09:23:18.640 [main] DEBUG org.quartz.simpl.SimpleThreadPool - Shutting down threadpool... 09:23:18.640 [main] DEBUG org.quartz.simpl.SimpleThreadPool - Shutdown of threadpool complete. 09:23:18.640 [main] INFO org.quartz.core.QuartzScheduler - Scheduler MyScheduler_$_NON_CLUSTERED shutdown complete. 09:23:19.109 [MyScheduler_Worker-2] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down. 09:23:19.109 [MyScheduler_Worker-1] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down. 09:23:19.109 [MyScheduler_Worker-3] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
需要注意的一点就是:如果我们通过StdSchedulerFactory.getDefaultScheduler()的方式维护一个调度器,我们的应用不会终止直到scheduler.shutdown()方法被调用。下面我们添加一个简单的Hello的任务并加以调度。这里面我们提供QuartzTest完整的代码,我们在调度器开始后,让程序睡眠10秒。这样可以在shutdown之前,可以看到HelloJob的调度执行。
package com.linux.huhx.example1; import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; import java.util.concurrent.TimeUnit; /** * @Author: huhx * @Date: 2017-11-23 上午 8:59 */ public class QuartzTest { public static void main(String[] args) { try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.start(); // define the job and tie it to our HelloJob class JobDetail job = JobBuilder.newJob(HelloJob.class) .withIdentity("job1", "group1") .build(); // 每5秒运行一次job Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("trigger1", "group1") .startNow() .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(5) .repeatForever()) .build(); // Tell quartz to schedule the job using our trigger scheduler.scheduleJob(job, trigger); TimeUnit.SECONDS.sleep(10); scheduler.shutdown(); } catch (Exception e) { e.printStackTrace(); } } }
我们的HelloJob的代码比较简单就是输出字符串:hello world.
package com.linux.huhx.example1; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; /** * @Author: huhx * @Date: 2017-11-23 上午 9:02 */ public class HelloJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.out.println("hello world."); } }
运行QuartzTest,可以看到如下的日志输出。由于我们的调度器是每5秒执行一次HelloJob,睡眠10秒之后会执行3次。
09:39:50.488 [main] INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor 09:39:50.519 [main] INFO o.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl 09:39:50.519 [main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.3.0 created. 09:39:50.519 [main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized. 09:39:50.519 [main] INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.3.0) 'MyScheduler' 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 3 threads. Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. 09:39:50.519 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'MyScheduler' initialized from default resource file in Quartz package: 'quartz.properties' 09:39:50.519 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.3.0 09:39:50.519 [main] INFO org.quartz.core.QuartzScheduler - Scheduler MyScheduler_$_NON_CLUSTERED started. 09:39:50.519 [MyScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers 09:39:50.535 [MyScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers 09:39:50.535 [MyScheduler_QuartzSchedulerThread] DEBUG o.q.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.job1', class=com.linux.huhx.example1.HelloJob 09:39:50.551 [MyScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers 09:39:50.551 [MyScheduler_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.job1 hello world. 09:39:55.520 [MyScheduler_QuartzSchedulerThread] DEBUG o.q.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.job1', class=com.linux.huhx.example1.HelloJob 09:39:55.520 [MyScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers 09:39:55.520 [MyScheduler_Worker-2] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.job1 hello world. 09:40:00.521 [MyScheduler_QuartzSchedulerThread] DEBUG o.q.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.job1', class=com.linux.huhx.example1.HelloJob 09:40:00.521 [MyScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers 09:40:00.521 [MyScheduler_Worker-3] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.job1 hello world. 09:40:00.536 [main] INFO org.quartz.core.QuartzScheduler - Scheduler MyScheduler_$_NON_CLUSTERED shutting down. 09:40:00.536 [main] INFO org.quartz.core.QuartzScheduler - Scheduler MyScheduler_$_NON_CLUSTERED paused. 09:40:00.536 [main] DEBUG org.quartz.simpl.SimpleThreadPool - Shutting down threadpool... 09:40:00.536 [main] DEBUG org.quartz.simpl.SimpleThreadPool - Shutdown of threadpool complete. 09:40:00.536 [main] INFO org.quartz.core.QuartzScheduler - Scheduler MyScheduler_$_NON_CLUSTERED shutdown complete. 09:40:00.583 [MyScheduler_Worker-2] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down. 09:40:00.615 [MyScheduler_Worker-1] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down. 09:40:01.021 [MyScheduler_Worker-3] DEBUG org.quartz.simpl.SimpleThreadPool - WorkerThread is shut down.
友情链接