quartz 2.3.1
package xxxxxxxxxx; import com.cjy.api.ext.quartz.CjyJobFactory; import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * <b> quartz job 工具类</b><br/> * <p>通过 {@link JobUtil#scheduleJob(String, Class, String)} 方法, 创建一个定时任务</p> * * @author Zero * @date 2019-07-23 09:20 */ @Component public class JobUtil { private static Scheduler scheduler; private static final Logger LOGGER = LoggerFactory.getLogger(JobUtil.class); private static CjyJobFactory JOB_FACTORY; private static String DEFAULT_GROUP = "default-quartz"; @Autowired private void setJobFactory(CjyJobFactory jobFactory) { JOB_FACTORY = jobFactory; } private static Scheduler getScheduler() { if (scheduler == null) { synchronized (JobUtil.class) { try { scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.setJobFactory(JOB_FACTORY); } catch (Exception e) { LOGGER.error(e.getMessage(), e); } } } return scheduler; } /** * 创建 quartz job. * * @param jobName job名字 * @param clazz 对应的 类 字节码 * @return jobDetail */ public static JobDetail createJob(String jobName, Class<? extends StatefulJob> clazz) { return JobBuilder.newJob(clazz). withIdentity(jobName, DEFAULT_GROUP).build(); } /** * 创建 cron 触发器 (quartz).. * 关于cron表达式 -- <a>http://cron.qqe2.com/</a> * * @param triggerName 触发器名称 * @param jobName job名字 应该与 createJob 里 的第一个参数一致 {@link JobUtil#createJob} * @param cron cron 表达式, * @return Trigger cron触发器 * @throws Exception 抛出异常 */ public static Trigger createCronTrigger(String triggerName, String jobName, String cron) throws Exception { return TriggerBuilder.newTrigger() .withIdentity(triggerName, DEFAULT_GROUP) .forJob(jobName, DEFAULT_GROUP) .withSchedule(CronScheduleBuilder.cronSchedule(cron)).build(); } /** * 创建 定时任务 * * @param jobName job 名字 (必须全英文) * @param cron cron 表达式 * @param clazz ? extend StatefulJob * @throws Exception 创建任务异常 */ public static void scheduleJob(String jobName, Class<? extends StatefulJob> clazz, String cron ) throws Exception { Scheduler scheduler = getScheduler(); scheduler.scheduleJob( JobUtil.createJob(jobName, clazz), JobUtil.createCronTrigger(jobName + "Trigger", jobName, cron) ); scheduler.start(); } }
quartz 1.8.6
package xxxx; import com.cjy.province.core.pojo.req.BaseUnicomReq; import com.cjy.province.service.BasicInterfacesService; import com.cjy.province.service.BasicInterfacesServiceImpl; import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.TreeSet; public class JobUtil { private static final Logger LOG = LoggerFactory.getLogger(JobUtil.class); private static final BasicInterfacesService basicInterfacesService = new BasicInterfacesServiceImpl(); private static Scheduler scheduler; /** * 创建 quartz job. * * @param jobName job名字 * @param clazz 对应的 类 字节码 * @return jobDetail */ public static JobDetail createJob(String jobName, Class<? extends StatefulJob> clazz) { JobDetail jobDetail = new JobDetail(); jobDetail.setName(jobName); jobDetail.setJobClass(clazz); jobDetail.setGroup("UnicomApi"); return jobDetail; } /** * 创建 cron 触发器 (quartz).. * 关于cron表达式 -- <a>http://cron.qqe2.com/</a> * * @param triggerName 触发器名称 * @param jobName job名字 应该与 createJob 里 的第一个参数一致 {@link JobUtil#createJob} * @param cron cron 表达式, * @return Trigger cron触发器 * @throws Exception 抛出异常 */ public static Trigger createCronTrigger(String triggerName, String jobName, String cron) throws Exception { CronTrigger trigger = new CronTrigger(); trigger.setName(triggerName); trigger.setJobName(jobName); trigger.setCronExpression(cron); trigger.setJobGroup("UnicomApi"); // 延迟一分钟执行.. // trigger.setStartTime(new Date(new Date().getTime() + 60000)); return trigger; } /** * 创建 线程... * * @return 线程... */ @SuppressWarnings("unchecked") public Thread createThread(final BaseUnicomReq req, final String tableName, final String types, final boolean isScenicReq) { return new Thread() { @Override public void run() { // 获取最 景区实时 最新的一条数据.. Map<String, String> map = basicInterfacesService.getLastMesTimeV2(tableName, types); boolean isRequestAble = basicInterfacesService.nextRequestAllow(map, types); while (isRequestAble) { if (isScenicReq) { // 如果是景区数据请求.. } else { req.doExecuteGetData(); basicInterfacesService.getNextRequestTime(map, types); isRequestAble = basicInterfacesService.nextRequestAllow(map, types); } } } }; } public static List<Thread> startThreadList(Thread... threads) { List<Thread> threadList = new ArrayList<>(); if (threads != null) { for (Thread t : threads) { if (t != null) { threadList.add(t); t.start(); } } } return threadList; } /** * 开启多个线程,并且使它们阻塞在主线程. * * @param threads 线程数组.. * @throws Exception 异常信息 */ public static void startThreadListAndBlock(Thread... threads) throws Exception { List<Thread> threadList = startThreadList(threads); if (threadList != null && threadList.size() > 0) { for (Thread t : threadList) { if (t != null) { t.join(); } } } } /** * 根据景区-县编码的对应关系...返回 市 的 编码 treeSet.... * * @param scenicMap 景区编码 - 县 编码 * @return treeSet */ public static TreeSet<String> createCitySet(Map<String, String> scenicMap) { TreeSet<String> set = new TreeSet<>(); if (scenicMap != null) { for (String val : scenicMap.values()) { val = "V0" + val.substring(0, 4) + "00"; set.add(val); } } return set; } /** * 根据景区-县编码的对应关系...返回 所有的 县域 编码 treeSet.... * * @param scenicMap 景区编码 - 县 编码 * @return treeSet */ public static TreeSet<String> createCountySet(Map<String, String> scenicMap) { TreeSet<String> set = new TreeSet<>(); if (scenicMap != null) { //val = "V0" + val.substring(0, 4) + "00"; set.addAll(scenicMap.values()); } return set; } /** * 创建 定时任务 * * @param jobName job 名字 (必须全英文) * @param cron cron 表达式 * @param clazz ? extend StatefulJob * @throws Exception 创建任务异常 */ public static void scheduleJob(String jobName, Class<? extends StatefulJob> clazz, String cron ) throws Exception { Scheduler scheduler = getScheduler(); scheduler.scheduleJob( JobUtil.createJob(jobName, clazz), JobUtil.createCronTrigger(jobName + "Trigger", jobName, cron) ); } }