[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 在开源任务调度框架中的翘首,它提供了强大任务调度机制,难能可贵的是它同时 ...
随机推荐
- 学习SVG系列(4):SVG滤镜效果
注意:Internet Explorer和Safari不支持SVG滤镜 <defs>.<filter> 所有互联网的SVG滤镜定义在<defs>元素中,<fi ...
- codeforces 721C (拓排 + DP)
题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...
- JavaScript笔记及总结
前言: 网页中HTML为内容,CSS做展现(修饰内容),Js为行为(交互). Js属于基于对象型的脚本语言,在学习时当作编程语言(如java,c#)学习更好理解. javascript是实现网页动态效 ...
- 黑马----JAVA泛型基础
黑马程序员:Java培训.Android培训.iOS培训..Net培训 JAVA范型-基础 一.泛型的概念 1.实现了参数化类型 2.用于编写可应用于多种类型的代码,即所编写的代码可应用于许多许多的类 ...
- IOS 关于开发的APP跳转第三方应用的心得
昨天晚上自己做了个APP,想做个功能可以去跳转到手机上的微博,微信.找了好些资料,下面总结下自己的心得. 跳转的核心代码如下: if ([[UIApplication sharedApplicatio ...
- git学习:关于origin和master
[转载请注明出处]http://www.cnblogs.com/mashiqi 2016/10/27 本文主要是对这篇博客文章的理解. git的服务器端(remote)端包含多个repository, ...
- Something about Wake-sleep
DBN可以看做是n个RBM串联组成,是一个多层神经网络. 多层的好处是可以用较少的参数表示复杂的函数. 而一些传统的training算法如BP算法,处理多层网络时,效果就不是很理想.
- 413 Request Entity Too Large
做小视频上传,结果接口总是返回500,服务器端跟踪,根本就进不来,再次翻查,发下服务器返回的其实是413,只不过APP底层接口将所有不是200的回包都转成500了,问题定位. 有了错误码,有了描述,字 ...
- jquery 中的 offset()
一.语法: 1.返回偏移坐标 $(select).offset(); top: $(select).offset().top; left: $(select).offset().left; 2.设 ...
- Flask-SQLAlchemy 的操作
from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) db = SQLAlchemy(app) ================= ...