2020-08-07 17:11:12 +08:00
|
|
|
package com.cowr.common.plugin;
|
|
|
|
|
|
|
|
|
|
import com.jfinal.plugin.IPlugin;
|
|
|
|
|
import org.quartz.*;
|
|
|
|
|
import org.quartz.impl.StdSchedulerFactory;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.Enumeration;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
|
|
import static org.quartz.CronScheduleBuilder.cronSchedule;
|
|
|
|
|
import static org.quartz.JobBuilder.newJob;
|
|
|
|
|
import static org.quartz.TriggerBuilder.newTrigger;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Created by lisai on 2015/5/27.
|
|
|
|
|
*/
|
|
|
|
|
public class QuartzPlugin implements IPlugin {
|
2020-10-18 00:39:17 +08:00
|
|
|
private SchedulerFactory sf = null;
|
|
|
|
|
private Scheduler sched = null;
|
|
|
|
|
private String config = "job.properties";
|
|
|
|
|
private Properties properties;
|
2020-08-07 17:11:12 +08:00
|
|
|
|
|
|
|
|
public QuartzPlugin(String config) {
|
|
|
|
|
this.config = config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public QuartzPlugin() {
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 00:39:17 +08:00
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
2020-08-07 17:11:12 +08:00
|
|
|
public boolean start() {
|
|
|
|
|
sf = new StdSchedulerFactory();
|
|
|
|
|
try {
|
|
|
|
|
sched = sf.getScheduler();
|
|
|
|
|
} catch (SchedulerException e) {
|
|
|
|
|
new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
loadProperties();
|
|
|
|
|
Enumeration enums = properties.keys();
|
|
|
|
|
while (enums.hasMoreElements()) {
|
|
|
|
|
String key = enums.nextElement() + "";
|
|
|
|
|
if (!key.endsWith("job")) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
String cronKey = key.substring(0, key.indexOf("job")) + "cron";
|
|
|
|
|
String enable = key.substring(0, key.indexOf("job")) + "enable";
|
|
|
|
|
if (isDisableJob(enable)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
String jobClassName = properties.get(key) + "";
|
|
|
|
|
String jobCronExp = properties.getProperty(cronKey) + "";
|
|
|
|
|
Class clazz;
|
|
|
|
|
try {
|
|
|
|
|
clazz = Class.forName(jobClassName);
|
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
JobDetail job = newJob(clazz).withIdentity(jobClassName,
|
|
|
|
|
jobClassName).build();
|
|
|
|
|
CronTrigger trigger = newTrigger()
|
|
|
|
|
.withIdentity(jobClassName, jobClassName)
|
|
|
|
|
.withSchedule(cronSchedule(jobCronExp)).build();
|
|
|
|
|
Date ft = null;
|
|
|
|
|
try {
|
|
|
|
|
ft = sched.scheduleJob(job, trigger);
|
|
|
|
|
sched.start();
|
|
|
|
|
} catch (SchedulerException ee) {
|
|
|
|
|
new RuntimeException(ee);
|
|
|
|
|
}
|
|
|
|
|
System.out.println(job.getKey() + " has been scheduled to run at: " + ft
|
|
|
|
|
+ " and repeat based on expression: "
|
|
|
|
|
+ trigger.getCronExpression());
|
|
|
|
|
}
|
2020-10-18 00:39:17 +08:00
|
|
|
return true;
|
2020-08-07 17:11:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isDisableJob(String enable) {
|
|
|
|
|
return Boolean.valueOf(properties.get(enable) + "") == false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadProperties() {
|
|
|
|
|
properties = new Properties();
|
|
|
|
|
InputStream is = QuartzPlugin.class.getClassLoader()
|
|
|
|
|
.getResourceAsStream(config);
|
|
|
|
|
try {
|
|
|
|
|
properties.load(is);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean stop() {
|
|
|
|
|
try {
|
|
|
|
|
sched.shutdown();
|
|
|
|
|
} catch (SchedulerException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 00:39:17 +08:00
|
|
|
public static void main(String[] args) {
|
2020-08-07 17:11:12 +08:00
|
|
|
QuartzPlugin plugin = new QuartzPlugin();
|
|
|
|
|
plugin.start();
|
|
|
|
|
System.out.println("执行成功!!!");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|