1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| package com.forg.plugin;
import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.Properties;
import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.Job; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory;
import com.jfinal.log.Logger; import com.jfinal.plugin.IPlugin;
public class QuartzPlugin implements IPlugin {
private final Logger logger = Logger.getLogger(getClass()); private static final String JOB = "job";
private String config = "job.properties";
private Properties properties;
public QuartzPlugin(String config) { this.config = config; }
public QuartzPlugin() { }
private SchedulerFactory schedulerFactory; private Scheduler scheduler;
@SuppressWarnings("unchecked") public boolean start() { try { loadProperties(); } catch (IOException e) { logger.error(e.getMessage()); return false ; }
if (properties == null) { return false; } schedulerFactory = new StdSchedulerFactory(); try { scheduler = schedulerFactory.getScheduler(); } catch (SchedulerException e) { logger.error(e.getMessage()); return false; }
if (scheduler == null) { logger.error("scheduler is null"); return false; }
Enumeration<Object> enums = properties.keys(); while (enums.hasMoreElements()) { String key = enums.nextElement() + ""; if (!key.endsWith(JOB) || !isTrue(getJobKey(key,"enable"))) { continue; } String jobClassName = properties.get(key) + ""; String jobName = key.substring(0,key.lastIndexOf(".")); String jobCronExp = properties.getProperty(getJobKey(key,"cron")) + ""; String jobGroup = properties.getProperty(getJobKey(key,"group","jobGroup1")); Class<? extends Job> jobClass = null; try { jobClass = (Class<? extends Job>) Class.forName(jobClassName); } catch (ClassNotFoundException e) { logger.error(e.getMessage()); return false; } JobDetail job = JobBuilder.newJob(jobClass) .withIdentity(jobName, jobGroup).build(); CronTrigger trigger = TriggerBuilder .newTrigger() .withIdentity("trigger", jobGroup) .withSchedule( CronScheduleBuilder.cronSchedule(jobCronExp)) .build();
try { scheduler.scheduleJob(job, trigger); scheduler.start(); } catch (SchedulerException e) { logger.error(e.getMessage()); return false; } } return true; }
public boolean stop() {
try { scheduler.shutdown(); } catch (SchedulerException e) { logger.error(e.getMessage()); return false; }
return true; }
private void loadProperties() throws IOException { properties = new Properties(); InputStream is = QuartzPlugin.class.getClassLoader() .getResourceAsStream(config); properties.load(is); } private String getJobKey(String str,String type,String defaultValue) { String key = getJobKey(str,type); if (key == null || "".equals(key.trim())) return defaultValue; return key; } private String getJobKey(String str,String type) { return str.substring(0, str.lastIndexOf(JOB)) + type; } private boolean isTrue(String key) { Object enable = properties.get(key); if (enable != null && "false".equalsIgnoreCase((enable + "").trim())) { return false; } return true; }
}
|