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 ...
随机推荐
- 20165224 陆艺杰 《Java程序设计》课程总结
每周作业链接汇总 https://www.cnblogs.com/lyj-/p/8414278.html https://www.cnblogs.com/lyj-/p/8695018.html htt ...
- phpmyadmin更改用户名和密码
我是用的xampp集成环境,wampp也差不多.另外没有配图,希望读者可以在实践的过程中有所思考,本文的主旨就是:找一个不叫root但和root一样厉害的人来管理数据库. 1,做个准备 首先创建一个和 ...
- SVN:Access to 'xxx' forbidden
可以做以下尝试:
- Spring学习笔记(五)—— Spring整合JDBC
一.Spring对JDBC的支持 Spring提供了很多模板整合Dao技术 与JDBC的整合中,Spring中提供了一个可以操作数据库的对象——JdbcTemplate,该对象封装了JDBC技术,与D ...
- ProgressBar(进度条)、SeekBar(拖动条)与星级评分条(RatingBar)
1.ProgressBar(进度条) (1)介绍 (2)常用属性 (3)xml代码 <ProgressBar android:id="@+id/progressBar2" s ...
- kotlin spring @value 注解
spring boot和kotlin里静态类使用@Value注解配置解决方案前言spring boot里默认是不能给静态属性使用@Value赋值的.所以这里使用中间变量过渡绑定. 方案//applic ...
- UVA - 10125 哈希
题意:求集合中最大的\(d\)使得\(a+b=d-c\) 学习一下哈希的姿势(原来所谓链地址法就是直接跑个图啊) 哈希真有趣,全靠xjb乱搞 就叫这套hash为xjb-fibonacci-lpy-ha ...
- 移动距离--dfs-蓝桥杯
题目描述: X星球居民小区的楼房全是一样的,并且按矩阵样式排列.其楼房的编号为1,2,3... 当排满一行时,从下一行相邻的楼往反方向排号. 比如:当小区排号宽度为6时,开始情形如下: 1 2 ...
- 【记录】dvwa总结
一.Brute Force 选择集束炸弹 设置payload stack attck 防御:密码加密,使用验证码,使用统一的参数代替帐号和密码. 二.Command Injection |,||,&a ...
- 剑指offer——面试题11:快速排序
#include"iostream" #include"random" using namespace std; /* void Swap(int &a ...