spring-quartz
首先在web的xml defaultSystemInit xxx.xxx.xxx.app.common.servlet.DefaultInitServlet 1 log4jConfig xxx/log4j/log4j.xml quartzConfig xxx/quartz/quartz.properties initAuthContext true xxx.xxx.xxx.app.common.servlet.DefaultInitServlet类要extends HttpServlet import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.impl.StdSchedulerFactory; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; public void init() throws ServletException { // BeanUtils使用Convert转换字段,重新注册转换器 add by lhm ConvertUtils.register(new IntegerConverter(null), Integer.class); ConvertUtils.register(new LongConverter(null), Long.class); // 初始化配置文件根目录 initRootConfigPath(); // 初始化log4j配置(日志输出目录等) initLog4j(); // 初始化quartz配置(如果子系统有用quartz). initQuartz(); } // 初始化quartz配置(quartz配置文件目录等). // 从init-param中得到quartz.properties配置文件相对路径. String quartzConfig = this.getInitParameter("quartzConfig"); if (quartzConfig == null || quartzConfig.length() == 0) { logger.info("没有指定quartz配置文件相对路径!"); return; } // 得到quartz配置文件全路径名 String quartzConfigFileName = ConfigurationHelper.getFullFileName(quartzConfig); FileInputStream fileInputStream ; try { logger.info("得到quartz配置文件全路径名:"+quartzConfigFileName); fileInputStream = new FileInputStream(quartzConfigFileName); } catch (FileNotFoundException e) { logger.error("找不到quartz配置文件,文件名:" + quartzConfigFileName); return; } // 得到quartz配置文件的目录 String quartzConfigPath = StringUtils.substringBeforeLast(quartzConfigFileName, "/"); logger.info("得到quartz配置文件的目录:"+quartzConfigPath); // 加载配置文件 Properties properties = new Properties(); try { properties.load(fileInputStream); logger.info("====================="+File.separator); // 设置quartz_jobs.xml路径 properties.setProperty("org.quartz.plugin.jobInitializer.fileName", quartzConfigPath + File.separator + "job.xml"); } catch (IOException e) { logger.error("加载quartz配置文件失败,文件名:" + quartzConfigFileName); return; } // 启动quartz StdSchedulerFactory factory ; try { factory = new StdSchedulerFactory(properties); } catch (SchedulerException e) { logger.error("初始化quartz失败,失败原因:" + e.getMessage()); return; } // 启动quartz任务调度器 try { scheduler = factory.getScheduler(); scheduler.start(); } catch (SchedulerException e) { logger.error("启动quartz任务调度失败,失败原因:" + e.getMessage()); return; } 得到quartz配置文件全路径名:D:\Tomcat 5.5.0_CS\bin\config\op/quartz/quartz.properties 得到quartz配置文件的目录:D:\Tomcat 5.5.0_CS\bin\config\op/quartz =====================\ 接下来: quartz.properties文件 # # Configure Main Scheduler Properties # org.quartz.scheduler.instanceName = OP_Job_Scheduler org.quartz.scheduler.instanceId = one # # Configure ThreadPool # org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 1 org.quartz.threadPool.threadPriority = 8 # # Configure JobStore # org.quartz.jobStore.misfireThreshold = 5000 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore # =========================================================================== # Configure SchedulerPlugins =============================================== # =========================================================================== org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingTriggerHistoryPlugin org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin org.quartz.plugin.jobInitializer.fileName = quartz_jobs.xml org.quartz.plugin.jobInitializer.overWriteExistingJobs = false org.quartz.plugin.jobInitializer.failOnFileNotFound = true org.quartz.plugin.jobInitializer.scanInterval = 600 org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin org.quartz.plugin.shutdownhook.cleanShutdown = true 接下来:job.xml PosCertApplyTask Default Quartz task for POS certificate apply. com.xxx.xxx.app.xxx.task.ShpTask PosCertApplyTaskCron Default PosCertApplyTask Default 0 0/10 * * * ? 最后就是任务类: package com.xxx.xxx.app.xxx.task; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class ShpTask implements Job{ public void execute(JobExecutionContext arg0) throws JobExecutionException { // TODO Auto-generated method stub System.out.println("我就是定时器................................"); } }