[Spring] - Quartz定时任务 - Annotation
Spring + Quartz可以使用annoation方式:
1、AppJob类:
package com.my.quartz.testquartz1; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class AppJob { // @Scheduled(fixedDelay=1000) //第一种方式
// fixedDelay延时多少毫秒,多少毫秒执行一次
/*
1 Seconds (0-59)
2 Minutes (0-59)
3 Hours (0-23)
4 Day of month (1-31)
5 Month (1-12 or JAN-DEC)
6 Day of week (1-7 or SUN-SAT)
7 Year (1970-2099)
取值:可以是单个值,如6;
也可以是个范围,如9-12;
也可以是个列表,如9,11,13
也可以是任意取值,使用*
*/
// 0 * * * * * 代表每分钟执行一次
/*
2011-09-07 09:23:00
2011-09-07 09:24:00
2011-09-07 09:25:00
2011-09-07 09:26:00
*/
@Scheduled(cron="0/1 * * * * ?")
public void execute() {
long ms = System.currentTimeMillis();
System.out.println(ms);
} }
cron的写法,可参见:
http://www.cnblogs.com/HD/p/4205937.html
2、Spring配置文件:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!--注解说明 -->
<context:annotation-config />
<!-- Spring自动扫描包 -->
<context:component-scan base-package="com.my" /> <!-- 计划任务 -->
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor" scheduler="scheduler" /> </beans>
3、运行类:
package com.my.quartz.testquartz1; import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App
{
@SuppressWarnings("resource")
public static void main( String[] args ) throws InterruptedException, SchedulerException
{
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
System.out.println("Job-End");
//Scheduler scheduler = (Scheduler) context.getBean("schedulerBean");
//scheduler.shutdown();
}
}
运行结果:

当然,Spring的配置文件也可以这样写:
<!-- 定时器开关-->
<task:annotation-driven />
<!-- 管理的Bean -->
<bean id="appJob" class="com.my.quartz.testquartz1" />
<task:scheduled-tasks scheduler="task">
<!--每三秒执行一次 -->
<task:scheduled ref="appJob" method="execute"
cron="0/1 * * * * ?" />
</task:scheduled-tasks>
<task:scheduler id="task" pool-size="10" />
这样写是可配置的,使用Annotation方式是编译式的。
[Spring] - Quartz定时任务 - Annotation的更多相关文章
- Spring quartz定时任务service注入问题
今天想单元测试一下spring中的quartz定时任务,job类的大致结构和下面的SpringQtz1类相似,我的是实现的org.quartz.Job接口,到最后总是发现job类里注入的service ...
- spring quartz定时任务
配置quartz 在spring中需要三个jar包: quartz-1.8.5.jar.commons-collections-3.2.1.jar.commons-logging-1.1.jar 首先 ...
- [Spring] Java spring quartz 定时任务
首先,需要导入quartz 的jar包 ① applicationContext.xml <!-- 轮询任务 --> <import resource="classpath ...
- Spring Quartz定时任务设置
这里主要记录一下定时任务的配置,偏向于记录型的一个教程,这里不阐述Quartz的原理. 首先,在Spring配置文件里配置一个自己写好的一个包含执行任务方法的一个类. <bean id=&quo ...
- 一看便知spring+quartz定时任务
这是我经过网上收集然后加上自己的测试写的,以便大家使用 标配:已测 注意需要的包:(在已经配置spring 的情况下) quartz-all-1.6.jar spring-context ...
- Spring+Quartz(定时任务)
此处用到的Quartz版本是quartz-2.2.3 官方网站:http://www.opensymphony.com/quartz 首先先介绍用到的几个关键类:scheduler任务调度.Job任务 ...
- spring + Quartz定时任务配置
<bean id="exportBatchFileTask" class="com.ydcn.pts.task.ExportBatchFileTask"& ...
- spring + quartz定时任务,以及修改定时任务
spring4+quartz2.2.3,定时任务弄好了,修改定时任务没折腾起,没找到合适的解决方案. 最终使用库spring-context-support 3.2.17.RELEASE + qua ...
- Java Spring Quartz 定时任务
公司需要使用JAVA的WebServer完成简单的定时跑任务的工作.其他例如:每隔30分钟执行锁定用户解锁任务. Quartz 在开源任务调度框架中的翘首,它提供了强大任务调度机制,难能可贵的是它同时 ...
随机推荐
- Java面向对象基础知识汇总
OOP:Orient Object Programe AOP:Aspect Orient Programe 封装:Encapsulation 继承:Inheritance 多态:Polymorphmi ...
- 【Python】re正则表达式
简单举几个常用的re正则表达式的例子: m = re.match("abc", "abcdef") print(m.group()) # 输出:abc m = ...
- redis 数据类型详解 以及 redis适用场景场合
1. MySql+Memcached架构的问题 实际MySQL是适合进行海量数据存储的,通过Memcached将热点数据加载到cache,加速访问,很多公司都曾经使用过这样的架构,但随着业务数据量的 ...
- selected 刷新页面后selected选中的值保持不表(thinkphp 从控制器assign 传值到js)
昨晚解决select 刷新页面以后选择的值保持不变,要想让seleted不变,有两种思路, 1,在提交表单的时候,将所选择的option的属性设为checked . 2.将option的value或者 ...
- BackTrack5-r3系统软件更新
所需文件包地址:http://pan.baidu.com/s/1i3ouc9v(64位更新包) 进入BT系统图形模式-打开BT终端输入:apt-get update 按回车//更新软件目录 软件更新将 ...
- SqlServer性能优化 通过压缩与计算列提高性能(十一)
压缩: 1.压缩的对象 1.表 2.索引(非聚集索引手工做) 3.备份(手工做) 2.对性能影响 1.提高IO性能 2.降低CPU性能 行压缩: 1.对null值不占用空间 2.对Nu ...
- Android 学习第16课,java 包、类等相关的一些基础知识
1.建议将类放在包中,不要使用无名包 2.建议包名都用小写单词组成,不要用大写 3.建议包名用“域名的倒写.项目名.模块名”的形式,以确保包名的唯一性 注意:类变量与实例变量.类方法与实例方法的区别 ...
- day27_反射
1.反射-概述(掌握) 反射就是在程序运行过程中,通过.class文件动态的获取类的信息(属性,构造,方法),并调用 注意:JAVA不是动态语言,因为动态语言强调在程序运行过程中不仅能获取并调用类里面 ...
- Outline of Apache Jena Notes
1 description 这篇是语义网应用框架Apache Jena学习记录的索引. 初始动机见Apache Jena - A Bootstrap 2 Content 内容组织基本上遵循Jena首页 ...
- QT5学习过程的小问题集锦
*** only available with -std=c++11 or -std=gnu++11 添加以下代码到*.pro文件. CONFIG += c++11 在 Qt creator 中设置 ...