Quartz 入门系列教程-01-入门案例
2018-04-02 20:43
651 查看
Quartz
Quartz is a richly featured, open source job scheduling library that can be integrated within virtuallyany Java application - from the smallest stand-alone application to the largest e-commerce system.
Quick Start
Quartz 可以帮助我们使得任务调度变得简单。导入 jar
使用 maven 管理 jar,依赖如下<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency>
示例代码
MyJob.java定义一个简单的 Job
package com.ryo.quartz.hello.job; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.err.println("Hello Quartz!"); } }
AppMain.java
运行上述 Job
package com.ryo.quartz.hello; import com.ryo.quartz.hello.job.MyJob; import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; public class AppMain { public static void main(String[] args) throws SchedulerException { // define the job and tie it to our MyJob class JobDetail job = JobBuilder.newJob(MyJob.class) .withIdentity("job1", "group1") .build(); // Trigger the job to run now, and then repeat every 5 seconds Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("trigger1", "group1") .startNow() .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(5) .repeatForever()) .build(); // Grab the Scheduler instance from the Factory Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); // and start it off scheduler.start(); // Tell quartz to schedule the job using our trigger scheduler.scheduleJob(job, trigger); } }
运行结果
每 5S 执行一次我们的 JobSLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Hello Quartz! Hello Quartz! Hello Quartz! Hello Quartz!
代码地址
quartz-hello系列导航
系列导航相关文章推荐
- AR入门系列-在vuforia官网的使用-01-史上最详细AR入门教程
- 云星数据---Scala实战系列(精品版)】:Scala入门教程025-Scala实战源码-Scala 的特质 (接口)01
- [转]Ultra Fractal教程系列04——快速入门教程01——创建一个分形图像
- 云星数据---Scala实战系列(精品版)】:Scala入门教程033-Scala实战源码-Scala apply方法01 调用
- [转]Ultra Fractal教程系列43——动画功能的使用01——创建动画分形案例
- Quartz 入门系列教程-02-Jobs 和 Trigger
- Angular系列----AngularJS入门教程01:AngularJS模板 (转载)
- Quartz 入门系列教程-04-Triggers 深入学习
- 云星数据---Scala实战系列(精品版)】:Scala入门教程039-Scala实战源码-Scala Case Class 01
- 微信公众账号开发系列01--开发入门教程
- 云星数据---Scala实战系列(精品版)】:Scala入门教程022-Scala实战源码-Scala 多态01
- ffmpeg入门系列教程(新API)示例 01
- AR入门系列-在vuforia官网的使用-01-史上最详细AR入门教程
- 云星数据---Scala实战系列(精品版)】:Scala入门教程050-Scala实战源码-Scala implicit 操作01
- Quartz 入门系列教程-00-序章
- 云星数据---Scala实战系列(精品版)】:Scala入门教程031-Scala实战源码-Scala 对象 01
- 系列文章 -- OpenCV入门教程
- Jenkins入门系列之——01第一章 Jenkins是什么?
- SeaJS入门教程系列之SeaJS介绍(一)
- JFinal项目开发菜鸟入门教程01