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) { The synchronized (JobUtil. Class ) { the try { Scheduler = StdSchedulerFactory.getDefaultScheduler (); scheduler.setJobFactory (JOB_FACTORY); } the catch (Exception E) { LOGGER.error (e.getMessage (), E); } } } return Scheduler; } / ** * Create Job Quartz. * * @param the jobName Job name * @param clazz corresponding class bytecode * @Return jobDetail * / public static JobDetail createJob (jobName String, Class <? The extends StatefulJob> clazz) { return JobBuilder.newJob (clazz). WithIdentity (jobName, of DEFAULT_GROUP in) .build (); } / ** * Create a cron trigger device (the Quartz) .. * about cron expression - <a> http://cron.qqe2.com/ </a> * * @param triggerName trigger name * @param should first jobName job in the name of createJob a consistent parameter { @link JobUtil # createJob} * @paramcron cron expression, *@return the Trigger trigger the cron * @throws Exception thrown * / public static the Trigger createCronTrigger (triggerName String, String The jobName, the cron String) throws Exception { return TriggerBuilder.newTrigger () .withIdentity (triggerName, of DEFAULT_GROUP in the) .forJob (The jobName, of DEFAULT_GROUP in) . .withSchedule (CronScheduleBuilder.cronSchedule (cron)) Build (); } / ** * create a task timer * * @param jobName the job name (must be in English) * @param cron cron expression *@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 new the JobDetail (); jobDetail.setName (The jobName); jobDetail.setJobClass (clazz); jobDetail.setGroup ( "UnicomApi" ); return jobDetail; } / ** * Create cron trigger (Quartz) .. * expressed on cron type - <a> http://cron.qqe2.com/ </a> * * @param triggerName trigger name * @param jobName the Job name should be consistent with createJob in the first argument { @link JobUtil # createJob } * @param cron cron expression, * @return the trigger 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 thread ... * / @SuppressWarnings ( "unchecked" ) public the Thread CreateThread ( Final BaseUnicomReq REQ, Final String tableName, Final String types, Final boolean isScenicReq) { return new new the Thread () { @Override public void RUN () { // get the most scenic spots in real time the latest piece of data .. the Map <String, String> = the 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<The Thread> = ThreadList new new ArrayList<>(); IF (Threads =! Null ) { for (the Thread T: Threads) { IF (! T = null ) { threadList.add (T); t.start (); } } } return ThreadList; } / ** * open multiple threads are blocked and the main thread. * * @param threads array of threads .. * @throws exception abnormality information * / public static void startThreadListAndBlock (the thread threads ...) throws Exception { List <the Thread> ThreadList = startThreadList (Threads); IF (ThreadList =! null && threadList.size ()> 0 ) { for (the Thread T: ThreadList) { IF (T! = null ) { t.join (); } } } } / ** * the area - correspondence between encoded County City ... return code .... TreeSet * * @param scenicMap resort encoding - encoding County * @ return TreeSet * / Public staticTreeSet <String> createCitySet (the Map <String, String> scenicMap) { TreeSet <String> SET = new new TreeSet <> (); IF (scenicMap =! Null ) { for (String Val: scenicMap.values ()) { Val = "V0" + val.substring (0,. 4) + "00" ; set.add (Val); } } return SET; } / ** * the area - corresponding relationship between county encoded encoding ... return all county .... TreeSet * * @param scenicMap scenic coding - coding County * @returnTreeSet * / public static TreeSet <String> createCountySet (the Map <String, String> scenicMap) { TreeSet <String> SET = new new TreeSet <> (); IF (! scenicMap = null ) { // Val = "V0" + Val .substring (0, 4) + "00"; set.addAll (scenicMap.values ()); } return the SET; } / ** * create a task timer * * @param jobName the job name (must be in English) * @param cron cron expression * @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) ); } }