【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的更多相关文章

  1. 项目中使用Quartz集群分享--转载

    项目中使用Quartz集群分享--转载 在公司分享了Quartz,发布出来,希望大家讨论补充. CRM使用Quartz集群分享  一:CRM对定时任务的依赖与问题  二:什么是quartz,如何使用, ...

  2. (4) Spring中定时任务Quartz集群配置学习

    原 来配置的Quartz是通过spring配置文件生效的,发现在非集群式的服务器上运行良好,但是将工程部署到水平集群服务器上去后改定时功能不能正常运 行,没有任何错误日志,于是从jar包.JDK版本. ...

  3. Spring 中使用Quartz实现任务调度

    前言:Spring中使用Quartz 有两种方式,一种是继承特定的基类:org.springframework.scheduling.quartz.QuartzJobBean,另一种则不需要,(推荐使 ...

  4. 浅谈Spring中的Quartz配置

    浅谈Spring中的Quartz配置 2009-06-26 14:04 樊凯 博客园 字号:T | T Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在 ...

  5. 10 -- 深入使用Spring -- 5...2 在Spring中使用Quartz

    10.5.2 在Spring中使用Quartz Spring 的任务调度抽象层简化了任务调度,在Quartz基础上提供了更好的调度抽象.本系统使用Quartz框架来完成任务调度,创建Quartz的作业 ...

  6. 在Jboss中使用Quartz

    Jboss EJB默认使用的定时服务是TimerService,TimerService的使用过程较为繁琐,需要使用一个无状态的serviceBean去实现scheduleTimer, timeout ...

  7. spring 中使用quartz实现定时任务

    一般开发系统,使用定时任务非常常见.当然也可以用Java实现.比如定时器.大致如下: 1: public static void main(String[] args) { 2: Timer time ...

  8. Spring中使用Quartz之MethodInvokingJobDetailFactoryBean配置任务

    Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz. Spring中使用Quartz的3种方法(MethodInvokingJobDetailFactoryBean,i ...

  9. 在springboot项目中引入quartz任务调度器。

    quartz是一个非常强大的任务调度器.我们可能使用它来管理我们的项目,常见的是做业绩统计等等.当然它的功能远不止这些.我们在这里不介绍quartz的原理,下面讲讲如何在springboot中使用qu ...

随机推荐

  1. [Maven]Codehaus的Maven Repository地址

    In ~/.m2/settings.xml you can update the URL to be used for specific repositories. For example: < ...

  2. 【转】idea project中导入其他文件夹下的模块,可能出现java.io.FileNotFoundException: XXX.xml

    在一个project 中导入一个java 模块, 我要执行该模块的main函数 ,在main函数中有一个 FileReader(“generatorConfig.xml”) 而generatorCon ...

  3. struts2学习笔记(三)—— struts2的常见配置

    一.配置文件的加载顺序 每次从客户端发送请求到服务器都要先经过Struts2的核心过滤器StrutsPrepareAndExecuteFilter,这个过滤器有两个功能:预处理和执行.在预处理中主要就 ...

  4. paraview添加vector

    https://youtu.be/cygVdhn-kG0 (须自行FQ)

  5. mysql 命令连接

    远程登陆MySQL,同时指定对应的端口和ip. 假设远程的ip为:10.154.0.43 端口为:1341 输入如下命令: mysql -h 10.154.0.43 -P 1341 -u root - ...

  6. UVA - 136 Ugly Numbers (有关set使用的一道题)

    Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, ...

  7. scrapy模块之分页处理,post请求,cookies处理,请求传参

    一.scrapy分页处理 1.分页处理 如上篇博客,初步使用了scrapy框架了,但是只能爬取一页,或者手动的把要爬取的网址手动添加到start_url中,太麻烦接下来介绍该如何去处理分页,手动发起分 ...

  8. Node.js frameworks

    1. Express 2. Koa 3. LoopBack egghead.io What is egghead? egghead is a group of working web developm ...

  9. git push的一些坑

    在安装git的时候我们一般会自己设置一个用户名和邮箱,这个一般设置为全局的用户名,如下所示 git config --global user.name "xxx" git conf ...

  10. innoback 参数及使用说明

    --defaults-file 同xtrabackup的--defaults-file参数,指定mysql配置文件; --apply-log 对xtrabackup的--prepare参数的封装; - ...