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是处理此类定时任务的一个优秀框架.随着项目一点点推进,此时我们并不满足 ...
随机推荐
- WCG distribution of byteball
https://steemit.com/byteball/@punqtured/processing-for-good-get-rewarded-for-crunching-numbers-to-cu ...
- 将 context node 中的内容 分配给 desing layer
1 将 context node 中的内容 分配给 desing layer 选中context node 右键>assignment to design layer.
- Python socketserver模块解析
参考:https://blog.csdn.net/qq_33733970/article/details/79153938 1.功能简介 socketserver模块是对socket模块的再封装,用于 ...
- 线程中使用SaveFileDialog不能弹出窗体
在子线程中使用 SaveFileDialog 无法弹出窗体,主要是我们需要用主线程去处理SaveFileDialog , 我们可以将子线程进行如下设置: public partial class Fo ...
- Java -- 深入浅出GC自动回收机制
1,去年开春去美团和58同城面试的时候第一个问题基本上都是来说说 Java GC机制,当时年轻的我也很耿直,直接说不会,现在想想还是当时年轻啊.刚好这段时间被各大论坛的面试题刷屏,见到最多的也是也是这 ...
- caffe训练模型中断的解决办法(利用solverstate)
caffe训练过程中会生成.caffemodel和.solverstate文件,其中caffemodel为模型训练文件,可用于参数解析,solverstate为中间状态文件 当训练过程由于断电等因素中 ...
- caffe训练脚本文件时遇到./build/tools/caffe: not found
原文转载:https://blog.csdn.net/zhongshaoyy/article/details/53502373 cifar10训练步骤如下: (1)打开终端,应用cd切换路径,如 cd ...
- 17. Letter Combinations of a Phone Number(bfs)
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- 谈谈html中一些比较"偏门"的知识(map&area;iframe;label)
说明:这里所说的"偏门"只是相对于本人而言,记录在此,加深印象.也希望有需要的朋友能获得些许收获! 1.空元素(void):没有内容的元素. 常见的有:<br>,< ...
- hdu1569 莫比乌斯反演
hdu 1695 莫比乌斯反演 给出a,b,c,d,k, 求满足a <= x <= b && c <= y <= d && gcd(x,y)=k ...