04_Spring中使用Quartz
【Spring中使用SimplerTrigger】

【QuartzTask.java】
package com.higgin.task; import java.text.SimpleDateFormat;
import java.util.Date; public class QuartzTask { public QuartzTask() {
System.out.println("QuartzTask 构造方法---");
} //这个就是要被SimpleTrigger定时触发执行的方法
public void doSimpleTriggerTask(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("doSimpleTriggerTask()定时执行方法:"+sdf.format(new Date()));
}
//这个就是要被CronTrigger定时触发执行的方法
public void doCronTriggerTask(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("doCronTriggerTask()定时执行方法:"+sdf.format(new Date()));
}
}
【simpleTriggerTask-spring.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 注册一个普通的bean -->
<bean id="quartzTask" class="com.higgin.task.QuartzTask"></bean> <!-- 1.制定任务信息 -->
<bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 设置执行对象 -->
<property name="targetObject" ref="quartzTask"></property>
<!-- 设置执行对象中对应的执行方法 -->
<property name="targetMethod" value="doSimpleTriggerTask"></property>
<!-- 是否可以同步执行:不可同步执行 -->
<property name="concurrent" value="false"></property>
</bean> <!-- 2.制定任务执行时机(任务执行触发器) -->
<bean id="simplerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<!-- 设置任务详细 -->
<property name="jobDetail" ref="myJobDetail"></property>
<!-- 设置任务延迟执行时间 :延迟2秒后执行(每隔2秒执行一次)-->
<property name="repeatInterval" value="2000"></property>
</bean> <!-- 3.设置调度工厂 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 可以通过该属性注册多个Trigger -->
<property name="triggers">
<list>
<ref bean="simplerTrigger"/>
</list>
</property>
</bean> </beans>
【CronTriggerTest.java】
package com.higgin.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class CronTriggerTest {
public static void main(String[] args) {
ApplicationContext context =new ClassPathXmlApplicationContext("cronTriggerTask-spring.xml");
}
}
【运行结果】

【Spring中使用CronTrigger】

【QuartzTask.java,同上】
package com.higgin.task; import java.text.SimpleDateFormat;
import java.util.Date; public class QuartzTask { public QuartzTask() {
System.out.println("QuartzTask 构造方法---");
} //这个就是要被SimpleTrigger定时触发执行的方法
public void doSimpleTriggerTask(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("doSimpleTriggerTask()定时执行方法:"+sdf.format(new Date()));
}
//这个就是要被CronTrigger定时触发执行的方法
public void doCronTriggerTask(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("doCronTriggerTask()定时执行方法:"+sdf.format(new Date()));
}
}
【cronTriggerTask-spring.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 注册一个普通的bean -->
<bean id="quartzTask" class="com.higgin.task.QuartzTask"></bean> <!-- 1.制定任务信息 -->
<bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 设置执行对象 -->
<property name="targetObject" ref="quartzTask"></property>
<!-- 设置执行对象中对应的执行方法 -->
<property name="targetMethod" value="doCronTriggerTask"></property>
<!-- 是否可以同步执行:不可同步执行 -->
<property name="concurrent" value="false"></property>
</bean> <!-- 2.制定任务执行时机(任务执行触发器) -->
<bean id="cronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 设置任务详细 -->
<property name="jobDetail" ref="myJobDetail"></property>
<!-- 设置任务执行时机,cron表达式-->
<property name="cronExpression" value="0/3 * * * * ?"></property>
</bean> <bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 设置任务详细 -->
<property name="jobDetail" ref="myJobDetail"></property>
<!-- 设置任务执行时机,cron表达式 :秒 分 时 日 月 周 年(可选) -->
<property name="cronExpression" value="0 21 16 20C * ?"></property>
</bean> <!-- 3.设置调度工厂 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger1"/>
<!-- <ref bean="cronTrigger2"/> -->
</list>
</property>
</bean> </beans>
【CronTriggerTest.java】
package com.higgin.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class CronTriggerTest {
public static void main(String[] args) {
ApplicationContext context =new ClassPathXmlApplicationContext("cronTriggerTask-spring.xml");
}
}
【运行结果】

04_Spring中使用Quartz的更多相关文章
- 项目中使用Quartz集群分享--转载
项目中使用Quartz集群分享--转载 在公司分享了Quartz,发布出来,希望大家讨论补充. CRM使用Quartz集群分享 一:CRM对定时任务的依赖与问题 二:什么是quartz,如何使用, ...
- (4) Spring中定时任务Quartz集群配置学习
原 来配置的Quartz是通过spring配置文件生效的,发现在非集群式的服务器上运行良好,但是将工程部署到水平集群服务器上去后改定时功能不能正常运 行,没有任何错误日志,于是从jar包.JDK版本. ...
- Spring 中使用Quartz实现任务调度
前言:Spring中使用Quartz 有两种方式,一种是继承特定的基类:org.springframework.scheduling.quartz.QuartzJobBean,另一种则不需要,(推荐使 ...
- 浅谈Spring中的Quartz配置
浅谈Spring中的Quartz配置 2009-06-26 14:04 樊凯 博客园 字号:T | T Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在 ...
- 10 -- 深入使用Spring -- 5...2 在Spring中使用Quartz
10.5.2 在Spring中使用Quartz Spring 的任务调度抽象层简化了任务调度,在Quartz基础上提供了更好的调度抽象.本系统使用Quartz框架来完成任务调度,创建Quartz的作业 ...
- 在Jboss中使用Quartz
Jboss EJB默认使用的定时服务是TimerService,TimerService的使用过程较为繁琐,需要使用一个无状态的serviceBean去实现scheduleTimer, timeout ...
- spring 中使用quartz实现定时任务
一般开发系统,使用定时任务非常常见.当然也可以用Java实现.比如定时器.大致如下: 1: public static void main(String[] args) { 2: Timer time ...
- Spring中使用Quartz之MethodInvokingJobDetailFactoryBean配置任务
Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz. Spring中使用Quartz的3种方法(MethodInvokingJobDetailFactoryBean,i ...
- 在springboot项目中引入quartz任务调度器。
quartz是一个非常强大的任务调度器.我们可能使用它来管理我们的项目,常见的是做业绩统计等等.当然它的功能远不止这些.我们在这里不介绍quartz的原理,下面讲讲如何在springboot中使用qu ...
随机推荐
- PHP 实时生成并下载超大数据量的 Excel 文件
//另外由于excel数据是从数据库里逐步读出然后写入输出流的所以需要将PHP的执行时间设长一点 //(默认30秒)set_time_limit(0)不对PHP执行时间做限制. set_time_li ...
- openmpi-3.0.1超线程报错问题
先简单记录一下,虽然还有一些疑惑没有解决. 之前安装openmpi是用的命令安装,版本比较低,mfix并行总出现死锁问题,于是想看看是不是openmpi版本导致,虽然目前还未找到具体原因,但是先记录下 ...
- 【算法笔记】B1023 组个最小数
1023 组个最小数 (20 分) 给定数字 0-9 各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意 0 不能做首位).例如:给定两个 0,两个 1,三个 ...
- P4578 [FJOI2018]所罗门王的宝藏
传送门 考虑一个位置答案传递性,如果某个位置的红宝石转动确定了,那么会引起连锁反应: 如图,绿色的转动确定了,那么那两个蓝色的转动也确定了 自己手玩一下,发现如果有解那么随便找一个开始然后一路玩下去最 ...
- Codeforces - 915E 离散化区间覆盖
我一直以来都错认为离散化就是换个映射,其实还需要在离散值两端加上相差为1的值才能真正离散 不然看一下test3就知道 不过这个离散姿势太暴力,以至于我1000ms时限跑出998ms(其实是太懒没有删重 ...
- vm 中安装 CentOS7
第三步:安装ISO文件 1.在vm下,文件,新建虚拟机 在我的机算机中,选中刚命名的CentOS7,右键,属性 2.开启虚拟机 PS: 打开虚拟机之后,提示了一个小错误,LZ根据错误提示,到BIOS里 ...
- css3之transition、transform、animation比较
css3动画多少都有些了解,但是对于transition.transform.animation这几个属性一直是比较模糊的,所以啊,这里做一个总结,也希望大家都可以对此有一个更好地理解. 其实, ...
- pat00-自测3. 数组元素循环右移问题 (20)
00-自测3. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在 ...
- nyoj 10——skiing————————【记忆化搜索】
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...
- Javascript模块化编程(三)require.js的用法及功能介绍
这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战.我采用的是一个非常流行的库require.js感兴趣的朋友可以了解下啊 我采用的是一个非常流行的 ...