quartz版本:2.1.7
多个quartz定时任务的java实现,主要有以下三个实现类和配置文件组成。
其中任务启动类是为了初始化和启动所有任务,只需要加载一次即可;测试任务类是定时任务具体实现的类,多个类代表多个定时任务,且该类与配置文件中的配置项需统一;配置文件是为了设置定时任务类名、是否开启、触发定时规则。详情见以下代码。
1. 任务启动类 QuartzInit.java
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzInit {
/**
* 读取配置文件并设置定时器
* 1、该方法加载一次即可
* 2、需要读取配置文件
* @throws SchedulerException
*/
public void start(){
// 创建工厂
SchedulerFactory schedulerfactory = new StdSchedulerFactory();
Scheduler scheduler = null;
try {
scheduler = schedulerfactory.getScheduler();
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
// 加载配置文件,并循环创建每一个定时任务
Properties properties = new Properties();
try {
properties.load(new FileInputStream(new File("../quartzJob.properties")));
} catch (Exception e) {
throw new RuntimeException(e);
}
Enumeration<Object> enums = properties.keys();
while (enums.hasMoreElements()) {
// 获取符合规范的参数名称
String key = enums.nextElement() + ""; // xxxx.job
if (!key.endsWith("job")) {
continue;
}
String cronKey = key.substring(0, key.indexOf("job")) + "cron"; // xxxx.cron
String enable = key.substring(0, key.indexOf("job")) + "enable"; // xxxx.enable
// 获取参数值
String jobClassName = properties.get(key) + ""; // 定时任务实现类
String jobCronExp = properties.getProperty(cronKey) + ""; // 定时任务的cron表达式
String enableVal = properties.getProperty(enable) + ""; // 定时任务开关
if (!"true".equalsIgnoreCase(enableVal)) {
continue;
}
Class clazz = null;
try {
clazz = Class.forName(jobClassName);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
// 指明job的名称,所在组的名称,以及绑定job类
JobDetail job = JobBuilder.newJob(clazz)
.withIdentity(jobClassName, jobClassName)
.build();
// 定义触发的条件
CronTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity(jobClassName, jobClassName)
.withSchedule(CronScheduleBuilder.cronSchedule(jobCronExp))
.build();
// 把作业和触发器注册到任务调度中
Date ft = null;
try {
ft = scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
System.out.println(jobClassName + " has been scheduled to run at: " + ft
+ " and repeat based on expression: "
+ trigger.getCronExpression());
}
// 启动任务
try {
scheduler.start();
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws InterruptedException {
new QuartzInit().start();
}
}
2. 测试任务类 HelloJob.java
package test;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job{
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
// 在此处实现任务
System.out.println("hello there!");
}
}
3. 配置文件 quartzJob.properties
hello.job=test.HelloJob
hello.cron=* * * * * ?
hello.enable=true
每个定时任务均有三个配置项,分别是类全限定名、cron表达式和任务开关,分别对应上面的hello.job、hello.cron和hello.enable三个键。若有多个任务,则需要在配置文件增加以上三个配置项,切记不同的任务其键名是不一样的,例如以上例子中的任务键名为hello,若是再添加一个任务需要将另起键名,例如hello1,hello2等。