/** * Add the Jobs and Triggers defined in the given map of <code>JobSchedulingBundle</code> s to the * given scheduler. * * @param jobBundles * @param sched * @param overWriteExistingJobs * @throws Exception */ public void scheduleJobs(Map jobBundles, Scheduler sched, boolean overWriteExistingJobs) throws Exception { getLog().info("Scheduling " + jobsToSchedule.size() + " parsed jobs."); Iterator itr = calsToSchedule.iterator(); while (itr.hasNext()) { CalendarBundle bndle = (CalendarBundle) itr.next(); addCalendar(sched, bndle); } itr = jobsToSchedule.iterator(); while (itr.hasNext()) { JobSchedulingBundle bndle = (JobSchedulingBundle) itr.next(); scheduleJob(bndle, sched, overWriteExistingJobs); } itr = listenersToSchedule.iterator(); while (itr.hasNext()) { JobListener listener = (JobListener) itr.next(); getLog() .info( "adding listener " + listener.getName() + " of class " + listener.getClass().getName()); sched.addJobListener(listener); } getLog().info(jobBundles.size() + " scheduled jobs."); }
/*1.初始化servlet 容器上下文*/ public void contextInitialized(ServletContextEvent sce) { sc = sce.getServletContext(); contextPath = sc.getContextPath(); realPath = sc.getRealPath("/") + "/"; String quartz_configFile = sc.getInitParameter("quartz-config-file"); URL quartzConfigFileURL = Thread.currentThread().getContextClassLoader().getResource(quartz_configFile); if (null != quartzConfigFileURL) { logger.info("quartz定时调度线程信息:" + quartzConfigFileURL.getFile()); } String shutdownUnload = sc.getInitParameter("shutdown-on-unload"); if (null != shutdownUnload) { performShutdown = Boolean.valueOf(shutdownUnload).booleanValue(); } /*参数配置*/ StdSchedulerFactory factory; try { if (quartz_configFile != null) factory = new StdSchedulerFactory(quartz_configFile); else factory = new StdSchedulerFactory(); scheduler = factory.getScheduler(); // 添加quartz任务监听日志 scheduler.addSchedulerListener(new SchedulerListenerImpl()); scheduler.addJobListener(new JobListenerImpl()); scheduler.addTriggerListener(new TriggerListenerImpl()); scheduler.start(); } catch (Exception e) { e.printStackTrace(); } }