代码:
package com.smt.autorun;import java.io.File;import java.util.ArrayList;import java.util.Date;import java.util.List;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import org.apache.log4j.Logger;import org.apache.poi.util.IOUtils;import org.ehcache.Cache;import org.ehcache.config.builders.CacheConfigurationBuilder;import org.ehcache.config.builders.CacheManagerBuilder;import org.ehcache.config.builders.ResourcePoolsBuilder;import org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.SchedulerFactory;import org.quartz.Trigger;import org.quartz.TriggerBuilder;import org.quartz.impl.StdSchedulerFactory;import static org.quartz.CronScheduleBuilder.*;import static org.quartz.JobBuilder.newJob;import com.smt.jobs.CheckJob;import com.smt.pojo.Table;public class AutoRun implements ServletContextListener { private static final Logger LOGGER = Logger.getLogger(AutoRun.class); private Scheduler scheduler = null; @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub LOGGER.info("end"); EhcacheUtils.closeCache(); try { scheduler.shutdown(true); } catch (SchedulerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void contextInitialized(ServletContextEvent arg0) { // TODO Auto-generated method stub LOGGER.info("begin"); EhcacheUtils.openCache(); CachemyCache = EhcacheUtils.cacheManager.createCache("myCache", CacheConfigurationBuilder .newCacheConfigurationBuilder(String.class, Object.class, ResourcePoolsBuilder.heap(100)).build()); myCache.put("test", "test"); // 加载所有的pdm // PdmParser parser = new PdmParser(); // List list = new ArrayList
(); // File file = new File("D:\\work\\powerdesigner"); // File[] files = file.listFiles(); // for(File f : files){ // if(f.getName().contains(".pdm")){ // Table[] tbs = // parser.parsePDM_VO("D:\\work\\powerdesigner\\"+f.getName(),f.getName().replaceAll(".pdm", // "")); // for(Table tb : tbs){ // list.add(tb); // } // } // } // myCache.put("tables", list); SchedulerFactory schedulerFactory = new StdSchedulerFactory(); try { scheduler = schedulerFactory.getScheduler(); JobDetail job = newJob(CheckJob.class) //定义Job类为HelloQuartz类,这是真正的执行逻辑所在 .withIdentity("job1", "group2") //定义name/group .usingJobData("name", "quartz") //定义属性 .build(); Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("myTrigger") .withSchedule(dailyAtHourAndMinute(19,00)) .build(); scheduler.scheduleJob(job, trigger); scheduler.start(); } catch (SchedulerException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
package com.smt.jobs;import org.apache.log4j.Logger;import org.quartz.DisallowConcurrentExecution;import org.quartz.Job;import org.quartz.JobDetail;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import com.smt.controller.DatabaseController;public class CheckJob implements Job { private static final Logger LOGGER = Logger.getLogger(CheckJob.class); @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { JobDetail detail = arg0.getJobDetail(); //获取参数 //String name = detail.getJobDataMap().getString("name"); LOGGER.info("执行了!!!!!!!!!!!!!!!!!!!!!!!"); }}
pom.xml
org.quartz-scheduler quartz 2.2.1 org.quartz-scheduler quartz-jobs 2.2.1
spring-quartz.xml
quartz.properties:
# Default Properties file for use by StdSchedulerFactory# to create a Quartz Scheduler Instance, if a different# properties file is not explicitly specified.##============================================================================# Configure Main Scheduler Properties#============================================================================org.quartz.scheduler.instanceName: ShinhoQuarzeorg.quartz.scheduler.instanceId = AUTOorg.quartz.scheduler.rmi.export: falseorg.quartz.scheduler.rmi.proxy: falseorg.quartz.scheduler.wrapJobExecutionInUserTransaction: false#============================================================================# Configure ThreadPool#============================================================================org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPoolorg.quartz.threadPool.threadCount: 2org.quartz.threadPool.threadPriority: 5org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: trueorg.quartz.jobStore.misfireThreshold: 60000#============================================================================# Configure JobStore#============================================================================#default config#org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore#\u6301\u4E45\u5316\u914D\u7F6Eorg.quartz.jobStore.class:org.quartz.impl.jdbcjobstore.JobStoreTXorg.quartz.jobStore.driverDelegateClass:org.quartz.impl.jdbcjobstore.StdJDBCDelegateorg.quartz.jobStore.useProperties:true#============================================================================#havent cluster spring#============================================================================org.quartz.jobStore.isClustered = false #\u6570\u636E\u5E93\u8868\u524D\u7F00org.quartz.jobStore.tablePrefix:qrtz_org.quartz.jobStore.dataSource:qzDS#============================================================================# Configure Datasources#============================================================================#JDBC\u9A71\u52A8org.quartz.dataSource.qzDS.driver:com.mysql.jdbc.Driverorg.quartz.dataSource.qzDS.URL:jdbc:mysql://localhost:3306/shbiorg.quartz.dataSource.qzDS.user:rootorg.quartz.dataSource.qzDS.password:rootorg.quartz.dataSource.qzDS.maxConnection:10