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 ...
随机推荐
- WPF 仿IPhone滑块开关 样式 - CheckBox
原文:WPF 仿IPhone滑块开关 样式 - CheckBox <Style x:Key="CheckRadioFocusVisual"> <Setter Pr ...
- WPF优化:Freezable冻结对象
原文:WPF优化:Freezable冻结对象 WPF虽然很美观,效果很炫,但是对资源的消耗也很大,尤其是初次接触WPF的人,因为很多地方虽然实现了想要的效果,但是由于经验问题,所以也会造成很大的资源浪 ...
- 多线程 NSThread 的使用
NSThread简介 使用NSThread 实现多线程,需要手动管理线程的生命周期, 一.线程的创建 //1.实例方法创建,,需要手动启动线程 NSThread *thread = [[NSThrea ...
- url 路径的拼接
url 路径的拼接 刚开始做项目总是被路径所困扰,不知道何时该拼接,何时不拼接,怎么拼接,如何拼接,有像地址栏拼接一样的,又在跳转页面拼接的,还有在 a 标签中 href 中拼接的 ,当时做的时候一 ...
- flask简单了解
Flask简介: Flask是一个Python编写的Web 微框架,让我们可以使用Python语言快速实现一个网站或Web服务,在介绍Flask之前首先来聊下它和Django的联系以及区别,djang ...
- 找出数组中的最小值(es5/es6)
1.命令式编程,只需要迭代数组,检查当前最小值是否大于数组元素,如果是更新最小值即可. var s = [2,3,4,5,6,7,8]; for(var i=0,m=s.length;i<m;i ...
- 让Nginx支持pathinfo
# 典型配置 location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_ ...
- Oracle下lag和lead分析函数
[转自] http://blog.csdn.net/thinkscape/article/details/8290894 Lead和Lag分析函数可以在同一次查询中取出同一字段的前N行的数据(Lag) ...
- mysql允许数据库远程连接
2018-11-06 进入数据库 mysql -uroou(用户) -p123456(密码) 授权某个user可远程访问 grant all privileges on *.* to ' with g ...
- PIE SDK PCA融合
1.算法功能简介 PCA 融合分三步实现,首先将多光谱数据进行主成分变换,然后用高分辨单波段替换第一主成分波段,最后进行主成份逆变换得到融合图像. PIE支持算法功能的执行,下面对PCA融合算法功能进 ...