package com.dy.common.schedulerTask; import java.util.Properties; import org.apache.logging.log4j.*; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.impl.StdSchedulerFactory; public class SchedulerTaskFactory { private static Logger log = LogManager.getLogger(SchedulerTaskFactory.class.getName()) ; private static Scheduler scheduler ; private static Properties pro ; static{ pro = new Properties() ; //以下属性从quartz-all-2.1.7.jar得到,而且下面所有项目都要配置 pro.put("org.quartz.scheduler.instanceName", "DefaultQuartzScheduler"); pro.put("org.quartz.scheduler.rmi.export", "false"); pro.put("org.quartz.scheduler.rmi.proxy", "false"); pro.put("org.quartz.scheduler.wrapJobExecutionInUserTransaction", "false"); pro.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool"); pro.put("org.quartz.threadPool.threadCount", "10"); pro.put("org.quartz.threadPool.threadPriority", "5"); pro.put("org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread", "true"); pro.put("org.quartz.jobStore.misfireThreshold", "60000"); pro.put("org.quartz.jobStore.class", "org.quartz.simpl.RAMJobStore"); } /** * 得到调度器唯一实例 * @return */ public static Scheduler getSingleScheduler(){ if(scheduler == null){ try { scheduler = new StdSchedulerFactory(pro).getScheduler(); } catch (SchedulerException e) { log.error(e) ; } } return scheduler ; } /** * 得到调度器唯一实例 * @param threadPoolMaxCount * @param threadPoolPriority * @return */ public static Scheduler getSingleScheduler(Integer threadPoolMaxCount, Integer threadPoolPriority){ if(scheduler == null){ try { if(threadPoolMaxCount != null && threadPoolMaxCount.intValue() >= 0){ pro.put("org.quartz.threadPool.threadCount", "" + (threadPoolMaxCount==null?10:(threadPoolMaxCount<=0?10:threadPoolMaxCount))); } if(threadPoolPriority != null && threadPoolPriority.intValue() >= 0){ pro.put("org.quartz.threadPool.threadPriority", "" + (threadPoolPriority==null?5:(threadPoolPriority<=0?5:threadPoolPriority))); } if(threadPoolMaxCount != null && threadPoolPriority != null){ if(threadPoolMaxCount.intValue() < threadPoolPriority.intValue()){ throw new SchedulerException("threadPoolMaxCount必须大于等于threadPoolPriority") ; } } scheduler = new StdSchedulerFactory(pro).getScheduler(); } catch (SchedulerException e) { log.error(e) ; } } return scheduler ; } /** * 关闭调度器 * @throws SchedulerException */ public static void shutdownScheduler() throws SchedulerException{ scheduler.shutdown() ; } }