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 ...
随机推荐
- UISearchBar 自定义处理
首先通过 KVC 获取到内部的 textField, 然后自定制处理 UITextField *searchField = [searchBar valueForKey:@"searchFi ...
- DataFactory使用和注意,排列组合
DataFactory使用和注意 mysql 连接ODBC开放数据库连接(Open Database Connectivity,ODBC)驱动程序 生成数据:int不能用 Build a compos ...
- LeetCode154.寻找旋转排序数组中的最小值 II
154.寻找旋转排序数组中的最小值 II 描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). ...
- 创建一个流(Stream)可以让Bitmap或Image保存到流里面
创建一个流(Stream)可以让Bitmap或Image保存到流里面 http://blog.csdn.net/angxiao/article/details/7481465 写文件流 ...
- js定时器的结束和开始
今天在做一个页面的报表的时候,需要在报表内容改变后屏蔽掉页面上的一些选择框. 因为这个报表是自身的链接实现的改变,我只能读取到history改变了,基于这个来判断 我写了一个判断条件,然后将他放在了一 ...
- HTML嵌套php
1. <?php echo 'if you want to serve XHTML or XML documents, do it like this'; ?> 2. <scri ...
- Nginx 安装--图片服务器搭建
1. nginx 需要依赖以下模块: gzip模块需要 zlib 库 rewrite模块需要 pcre 库 ssl 功能需要openssl库 1.1.安装pcre 1. 获取pcre ...
- phpstorm 2017 关掉变量提示 parameter name hints
配置面板中搜索 hints 路径 Editor > General > Appearance > Show parameter name hits 去掉前面的勾就行了
- java 简单的des加密示例
1.加密结果 包含 : 对int加密 .对string加密.对byte[]加密. 10-09 18:33:32.484 7617-7617/com.example.tt.downtest D/Ciph ...
- pulic——function(下载的公共的)
1. /* * 用途: 对Date的扩展,将 Date 转化为指定格式的String */ // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年( ...