博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
我的Quartz笔记
阅读量:6472 次
发布时间:2019-06-23

本文共 6452 字,大约阅读时间需要 21 分钟。

代码:

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();        Cache
myCache = 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

 

转载地址:http://qkvko.baihongyu.com/

你可能感兴趣的文章
Docker Tag
查看>>
ZOJ 2459 Pyramids
查看>>
activemq自己配置安装过程
查看>>
Java Practices -> Home
查看>>
(收藏)Andriod中文翻译组
查看>>
列出Database所有Key列或者获取表主键名称
查看>>
类依赖项的不透明性和透明性
查看>>
更改 input type 的值
查看>>
双重OAuth 2.0架构
查看>>
Codeforces 452A Eevee
查看>>
推荐的优秀博客--多关注,多看看
查看>>
深入理解盒模型
查看>>
java中已经排序的列表中插入新值
查看>>
整理一些开源项目
查看>>
hibernate -- HQL语句总结
查看>>
Android深入浅出之 AudioTrack分析
查看>>
spring的AOP
查看>>
为Guid数据类型的属性(property)赋值
查看>>
jQuery中的100个技巧(译)
查看>>
初始化应用程序数据ng-init指令
查看>>