sping的quartz设置定时任务
除了spring相关的jar包之外,还需要引入quartz-all-1.8.6.jar
下载地址:http://www.quartz-scheduler.org/downloads/
spring配置文件增加quartz-bean.xml和quartz-set.xml
quartz-bean.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.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"> <!-- 测试 -->
<bean id="testQuartzTask" class="com.tech.jin.quartz.TestQuartzTask"/> </beans>
quartz-set.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.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"> <!-- 定时任务定义 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- <property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:spring-quartz.properties" /> 用于数据库中配置任务,集群使用-->
<property name="overwriteExistingJobs" value="true" /><!--可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了 -->
<property name="startupDelay" value="1" /><!-- 启动完1秒后执行任务 -->
<property name="autoStartup" value="true" /><!-- 自动启动 -->
<property name="quartzProperties">
<props>
<prop key="org.quartz.threadPool.threadCount">50</prop>
</props>
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" /> <property name="triggers">
<list>
<!-- 配置多个任务,继续在list中添加 -->
<ref bean="testQuartzTaskTrigger" /><!-- 测试自动任务 --> </list>
</property>
</bean> <!-- 测试自动任务 -->
<bean id="testQuartzTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="testQuartzTaskJobDetail"/>
</property>
<property name="cronExpression">
<value>0/10 * * * * ? </value>
</property>
<property name="jobDataAsMap">
<map>
<entry key="jobClass" value="testQuartzTask"/>
<entry key="jobName" value="测试自动任务"/>
</map>
</property>
</bean> <!-- MethodInvokingJobDetailFactoryBean不支持序列化 -->
<bean id="testQuartzTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject"><ref bean="testQuartzTask"/></property>
<property name="targetMethod"><value>doTask</value></property>
<property name="concurrent" value="false"/><!-- concurrent(并发) : false表示等上一个任务执行完后再开启新的任务 -->
</bean> <!-- 这种支持job序列化,但是这个jobClass对应的类,必须实现job,有execute方法-->
<!-- <bean id="testQuartzTaskJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.tech.jin.quartz.TestQuartzTask"></property>
<property name="durability" value="true" />
<property name="requestsRecovery" value="true" />
</bean> --> </beans>
使用MethodInvokingJobDetailFactoryBean调用的类TestQuartzTask:
package com.tech.jin.quartz;
import org.apache.log4j.Logger;
public class TestQuartzTask {
private Logger logger = Logger.getLogger(TestQuartzTask.class);
public void doTask(){
logger.info("66666666666666666666666");
}
}
使用JobDetailFactoryBean调用的类TestQuartzTask:
package com.tech.jin.quartz; import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; public class TestQuartzTask implements Job{ private Logger logger = Logger.getLogger(TestQuartzTask.class);
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
logger.info("5555555"); } }
除此之外,还需要将quartz-bean.xml 和quartz-set.xml引入到ApplicationContext.xml文件中(推荐)
<import resource="spring-quartz.xml"/>
或者在web.xml中添加上读取sping配置文件的配置:classpath:spring/quartz-*xml。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/ApplicationContext.xml;
classpath:spring/quartz-*xml
</param-value>
</context-param>
--------------------------------------------------------------------------------------------------------------------------------------------------------
如果需要集群使用,就打开quartz-set.xml中SchedulerFactoryBean注掉的datasource和配置文件。
每个server把将要及正在运行的job所有状态都即时同步到中央数据库,然后再次触发调用时从数据库中分析是否已有别的server正在运行相同job
(同名同定时时间点的job属于相当job),如果相同job正在别的server运行,那么当前server忽略本次运行的job.
Quartz的 Task(11 张表)实例化采用数据库存储,基于数据库引擎及 High-Available 的策略(集群的一种策略)自动协调每个节点的 Quartz。
spring-quartz.properties:
#==============================================================
#Configure Main Scheduler Properties
#==============================================================
org.quartz.scheduler.instanceName = defaultScheduler
org.quartz.scheduler.instanceId = AUTO #==============================================================
#Configure JobStore
#==============================================================
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.maxMisfiresToHandleAtATime = 1
org.quartz.jobStore.misfireThreshold = 120000
org.quartz.jobStore.txIsolationLevelSerializable = true #==============================================================
#Configure ThreadPool
#==============================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true #==============================================================
#Skip Check Update
#update:true
#not update:false
#==============================================================
org.quartz.scheduler.skipUpdateCheck = true #============================================================================
# Configure Plugins
#============================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown = true
数据库的sql版本要和squartz完全一致,在下载解压后的quartz\quartz-1.8.6\docs\dbTables文件夹在都有
tables_mysql.sql、tables_oracle.sql等...
sping的quartz设置定时任务的更多相关文章
- Quartz 设置一个半小时任务实现
该文章属于本人原创,转载请注明出处. spring + Quartz 设置定时任务时要求没一个半小时执行一次 设置两个相同的定时任务 第一个从整点开始每三小时执行一次 ...
- quartz 时间设置(定时任务scheduler)
quartz用来设置定时任务的作业调度程序.在linux的crontab中用到. 格式为: * * * * * * * 其从左到右顺序代表 :[秒] [分] [小时] [日] [月] [周] [年] ...
- SpringBoot2.0整合Quartz实现动态设置定时任务时间
一. 引入依赖 <!-- 引入quartz依赖 --> <dependency> <groupId>org.springframework.boot</ ...
- Quartz.net 定时任务之Cron表达式
一.cron表达式简单介绍和下载 1.在上一篇博客"Quartz.net 定时任务之简单任务"中,我简单介绍了quartz的使用,而这篇博客我将介绍cron的具体使用(不足之处望大 ...
- springboot整合Quartz实现定时任务
1.maven依赖: <!--quartz--> <dependency> <groupId>org.quartz-scheduler</groupId> ...
- Spring+Quartz 实现定时任务的配置方法
Spring+Quartz 实现定时任务的配置方法 整体介绍 一.Quartz介绍 在企业应用中,我们经常会碰到时间任务调度的需求,比如每天凌晨生成前天报表,每小时生成一次汇总数据等等.Quartz是 ...
- Spring整合quartz2.2.3总结,quartz动态定时任务,Quartz定时任务集群配置
Spring整合quartz2.2.3总结,quartz动态定时任务,Quartz定时任务集群配置 >>>>>>>>>>>>&g ...
- 【spring-boot】 springboot整合quartz实现定时任务
在做项目时有时候会有定时器任务的功能,比如某某时间应该做什么,多少秒应该怎么样之类的. spring支持多种定时任务的实现.我们来介绍下使用spring的定时器和使用quartz定时器 1.我们使用s ...
- Spring Boot集成quartz实现定时任务并支持切换任务数据源
org.quartz实现定时任务并自定义切换任务数据源 在工作中经常会需要使用到定时任务处理各种周期性的任务,org.quartz是处理此类定时任务的一个优秀框架.随着项目一点点推进,此时我们并不满足 ...
随机推荐
- centos7下源码编译方式安装httpd
前言 Apache至少需要apr.apr-util.pcre组件的支持. APR(Apache portable Run-time libraries,Apache可移植运行库)的目的如其名称一样,主 ...
- Linux命令:xargs命令详解,xargs与管道的区别
阅读目录 为什么要用xargs,问题的来源 xargs是什么,与管道有什么不同 xargs的一些有用的选项 回到顶部 为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人 ...
- Ngx_Lua使用分享
2017年04月22日 20:05:21 阅读数:430 Nginx_Lua 1.1. 介绍 1.2. 安装 1.2.1. 安装JIT平台 1.2.2. NDK与Lua_module 1.2.3. 编 ...
- [LeetCode] 系统刷题1_代码风格及边界
代码风格 说自己不清楚的算法,比如KMP,如果解释不清楚或者写不出来的算法建议不提 注意代码的缩进以及空格的合理运用,使得代码看起来比较整洁有条理 注意边界的条件以及越界 误区: 算法想出来还仅仅不够 ...
- JS发送短信验证码
<div> <input type="tel" id="mobile" name="mobile" placeholder ...
- 利用Tensorflow实现逻辑回归模型
官方mnist代码: #下载Mnist数据集 import tensorflow.examples.tutorials.mnist.input_data mnist = input_data.read ...
- 解读经典面试题for循环console.log
for (var i = 1; i <= 5; i++) { setTimeout(function () { console.log(i) },1000)} 会打印出5个6,这是why 因为 ...
- 原生table相关写法
1.<table style="width: 100%;text-align: center" cellpadding="0" cellspacing=& ...
- unity3d-角色控制器续
自学是一个坚持和寂寞的过程,写博客更是一个总结与成长的过程,加油! 角色控制器续 之前学习了角色漫游,但里面有很多效果都不是我想要的.只有自己的动手实践了才能理会其中的奥妙.所以我又琢磨了许久. 为了 ...
- React-Native组件之Text内文字垂直居中方案
style: { height: 100, textAlign: 'center', textAlignVertical: 'center', } 以上方法在Android上显示水平垂直居中, 但在I ...