代码:

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<String, Object> 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<Table> list = new ArrayList<Table>();
// 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

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>

spring-quartz.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- =========JDBC版=========== -->
<!--
持久化数据配置,需要添加quartz.properties
-->
<bean name="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>
<property name="configLocation" value="classpath:quartz.properties"/>
</bean> </beans>

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: ShinhoQuarze
org.quartz.scheduler.instanceId = AUTO org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 2
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true org.quartz.jobStore.misfireThreshold: 60000
#============================================================================
# Configure JobStore
#============================================================================ #default config
#org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
#\u6301\u4E45\u5316\u914D\u7F6E
org.quartz.jobStore.class:org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass:org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties:true #============================================================================
#havent cluster spring
#============================================================================
org.quartz.jobStore.isClustered = false #\u6570\u636E\u5E93\u8868\u524D\u7F00
org.quartz.jobStore.tablePrefix:qrtz_
org.quartz.jobStore.dataSource:qzDS #============================================================================
# Configure Datasources
#============================================================================
#JDBC\u9A71\u52A8
org.quartz.dataSource.qzDS.driver:com.mysql.jdbc.Driver
org.quartz.dataSource.qzDS.URL:jdbc:mysql://localhost:3306/shbi
org.quartz.dataSource.qzDS.user:root
org.quartz.dataSource.qzDS.password:root
org.quartz.dataSource.qzDS.maxConnection:10

我的Quartz笔记的更多相关文章

  1. SpringBoot中使用Quartz笔记

    Quartz可以用来做什么? Quartz是一个任务调度框架,可用来做定时任务. 吧啦吧啦......... 还是直接上代码. application.properties  配置文件. * * ? ...

  2. [Quartz笔记]玩转定时调度

    简介 Quartz是什么? Quartz是一个特性丰富的.开源的作业调度框架.它可以集成到任何Java应用. 使用它,你可以非常轻松的实现定时任务的调度执行. Quartz的应用场景 场景1:提醒和告 ...

  3. quartz笔记

    首先网络上的很多教程经常有错(信息过载了),其主要原因是版本发生了变化,例如quartz1和2之间还是有不少差别的,导致查找资料的人浪费了不少时间.所以无论教程如何写,都建议读者首先学习官网的教程,如 ...

  4. 5.17 Quartz笔记

    有用到构建者模式: builder---JobDetail相当于需要构建者构建出来的一个配件:JobDetail为Job实例提供了许多设置属性,以及JobDetaMap成员变量属性,它用来存储特定Jo ...

  5. Quartz.net 2.x 学习笔记03-使用反射加载定时任务

    将定时任务信息存储在XML文件中,使用反射加载定时任务 首先新建一个MVC的空站点,使用NuGet添加对Quartz.net和Common.Logging.Log4Net1213的引用,同时使用NuG ...

  6. Spring研磨分析、Quartz任务调度、Hibernate深入浅出系列文章笔记汇总

    Spring研磨分析.Quartz任务调度.Hibernate深入浅出系列文章笔记汇总 置顶2017年04月27日 10:46:45 阅读数:1213 这系列文章主要是对Spring.Quartz.H ...

  7. Quartz学习笔记:集群部署&高可用

    Quartz学习笔记:集群部署&高可用 集群部署 一个Quartz集群中的每个节点是一个独立的Quartz应用,它又管理着其他的节点.这就意味着你必须对每个节点分别启动或停止.Quartz集群 ...

  8. Quartz学习笔记:基础知识

    Quartz学习笔记:基础知识 引入Quartz 关于任务调度 关于任务调度,Java.util.Timer是最简单的一种实现任务调度的方法,简单的使用如下: import java.util.Tim ...

  9. 作业调度框架 Quartz 学习笔记(三) -- Cron表达式 (转载)

    前面两篇说的是简单的触发器(SimpleTrigger) , SimpleTrigger 只能处理简单的事件出发,如果想灵活的进行任务的触发,就要请出 CronTrigger 这个重要人物了. Cro ...

随机推荐

  1. sqljdbc 无法连接到主机

    今天发现sqljdbc 无法连接到SQL Server 主机.原因是SQL Server Browser 服务没有打开

  2. 注解配置定时任务——@Scheduled

    Java中注解@Scheduled 的注解代码如下: @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(Ret ...

  3. <?xml version="1.0" encoding="UTF-8" standalone="no"?>

    XML standalone 定义了外部定义的 DTD 文件的存在性. standalone element 有效值是 yes 和 no. 如下是一个例子: <?xml version=&quo ...

  4. SWIFT解析天气JSON格式

    访问以下链接可以得到京城当天的天气:http://www.weather.com.cn/adat/sk/101010100.html 返回的JSON格式如下: {"weatherinfo&q ...

  5. Okhttp对http2的支持简单分析

    在< Okhttp之RealConnection建立链接简单分析>一文中简单的分析了RealConnection的connect方法的作用:打开一个TCP链接或者打开一个隧道链接,在打开t ...

  6. HDU 4970

    http://acm.hdu.edu.cn/showproblem.php?pid=4970 比赛的时候线段树水过的,比赛后线段树一直T,看了下正解真的是智商压制 题意:走直线,长度1-N,还有一些人 ...

  7. word2vec 小测试

    Bag-of-words Model Previous state-of-the-art document representations were based on the bag-of-words ...

  8. 任务三 简单程序测试及 GitHub Issues 的使用

    我提交的Issue 我被提出的Issue 在使用Issue的过程中我发现提出的Issue不能指派任务人和问题类型,被提出的Issue可以. 碰到最多的问题是测试程序的过程中, 比如用户未按指定格式输入 ...

  9. Ajax和SpringMVC之间JSON交互

    Ajax和SpringMVC之间的json数据传输有两种方式: 1.直接传输Json对象 2.将Json序列化成json字符串 1.直接传输Json对象 前端Ajax $(document).read ...

  10. HihoCoder - 1501:风格不统一如何写程序

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi写程序时习惯用蛇形命名法(snake case)为变量起名字,即用下划线将单词连接起来,例如:file_name. ...