Quartz调度简单案例:
前期准备
导入Quartz到项目中,本人是 用的Maven,代码如下
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency>
下载地址如下所示:http://www.quartz-scheduler.org/downloads Maven
1、定义Job工作类
package com.timerBin.quartz.simple;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class SimpleJob implements Job {
private static int i=0;
@Override
public void execute(JobExecutionContext job) throws JobExecutionException {
try {
System.err.println("开始:"+i);
i++;
} catch (Exception e) {
e.printStackTrace();
}
}
}
2、简单例子调用Quartz(最基础调用)
package com.timerBin.quartz.simple;
import java.util.Date;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.SimpleTriggerImpl;
public class SimpleQuartz {
public static void main(String[] args) {
runQuartz();
}
public static void runQuartz(){
try {
//定义任务 分组名称信息 分组名 如果不设置分组名 是默认分组Default
JobDetail jobDetail = new JobDetailImpl("myJobName","myJobGroupName", SimpleJob.class);
//定义调度触发器
SimpleTriggerImpl strigger = new SimpleTriggerImpl("myTiggerName");
//触发器 从当前时间开始
strigger.setStartTime(new Date());
//调用10次
strigger.setRepeatCount(10);
//每隔2000毫秒
strigger.setRepeatInterval(2000);
//定义调度
Scheduler scheudle = new StdSchedulerFactory().getScheduler();
scheudle.scheduleJob(jobDetail, strigger);
//调度启动
scheudle.start();
Thread.currentThread().sleep(1000);
//调度关闭
System.err.println("关闭 ");
scheudle.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3、包含日历的Quartz简单调用例子
package com.timerBin.quartz.simple;
import java.util.GregorianCalendar;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.calendar.AnnualCalendar;
import org.quartz.impl.triggers.SimpleTriggerImpl;
public class SimpleQuartz {
public static void main(String[] args) {
runQuartz();
}
public static void runQuartz(){
try {
//定义任务 分组名称信息 分组名 如果不设置分组名 是默认分组Default
JobDetail jobDetail = new JobDetailImpl("myJobName","myJobGroupName", SimpleJob.class);
//定义日历集合
AnnualCalendar holidays = new AnnualCalendar();
//定义日历 默认获得当前时间 也可以根据构造方法定义
GregorianCalendar calendar = new GregorianCalendar();
//calendar.set(1, 2015); 年
//calendar.set(2, 1); 月
//calendar.set(5, 14); 日
//将日志添加到几何中 false 不排除 true 排除
holidays.setDayExcluded(calendar, false);
//定义调度
Scheduler scheudle = new StdSchedulerFactory().getScheduler();
scheudle.addCalendar("myCalendars", holidays, false, false);
//定义调度触发器
SimpleTriggerImpl strigger = new SimpleTriggerImpl("myTiggerName");
//将日历添加到调度
strigger.setCalendarName("myCalendars");
//触发器 从日历中的当前时间开始
strigger.setStartTime(calendar.getTime());
//调用10次
strigger.setRepeatCount(10);
//每隔2000毫秒
strigger.setRepeatInterval(2000);
scheudle.scheduleJob(jobDetail, strigger);
//调度启动
scheudle.start();
Thread.currentThread().sleep(1000);
//调度关闭
System.err.println("关闭 ");
scheudle.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4、将调度任务状态信息保存到数据库
1)下载调度数据库表结构
所在路径:quartz-2.2.1\docs\dbTables\xxxx.sql
2)在相应数据库中执行sql文件
3)在项目的src路径下创建quartz.properties文件,内容可如下所示(Mysql):
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.tablePrefix=QRTZ_
org.quartz.jobStore.dataSource=mySource
org.quartz.dataSource.mySource.driver=com.mysql.jdbc.Driver
org.quartz.dataSource.mySource.URL=jdbc:mysql://xxx.xxx.xxx.xxx:3306/test
org.quartz.dataSource.mySource.user=root
org.quartz.dataSource.mySource.password=root1234
org.quartz.dataSource.mySource.maxConnections=10
org.quartz.threadPool.threadCount=1
package com.timerBin.quartz.simple;
import java.util.List;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.JobKey;
public class SimpleDbQuartz {
public static void main(String[] args) {
runDbQuartz();
}
public static void runDbQuartz(){
try {
//定义调度
Scheduler scheudle = new StdSchedulerFactory().getScheduler();
//获取所有调度的名称
List<String> list = scheudle.getJobGroupNames();
for(int i=0;i<list.size();i++){
//根据所有调度名称回复调度
scheudle.resumeJob(new JobKey(list.get(i)));
}
//开始调度
scheudle.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
5、在Spring中集成Quartz