[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 在开源任务调度框架中的翘首,它提供了强大任务调度机制,难能可贵的是它同时 ...
随机推荐
- 学习linux/unix编程方法的建议(转)
假设你是计算机科班出身,计算机系的基本课程如数据结构.操作系统.体系结构.编译原理.计算机网络你全修过 我想大概可以分为4个阶段,水平从低到高从安装使用=>linux常用命令=>linux ...
- net iis 部署中出现的问题及解决方案
1.HTTP500.21 错误 解决方法:重新注册asp.net C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe –i ...
- repo upload上传提交时发生remote rejected异常
部分关键异常内容为: ...... remote:ERROR:committer email address %%%%%% remote:ERROR:does not match your user ...
- BZOJ 1799 同类分布
一开始没想出来..一看题解 我艹直接枚举数位的和啊.....怪不得给50s. 还是太蠢. #include<iostream> #include<cstdio> #includ ...
- MVC5 CodeFirst (一)
创建一个MVC5项目,VS生成了AccountController.HomeController,F5直接运行,实现了注册.登录.修改……但是注册的用户只是在内存里,下面我们来持久化. 三个命令:en ...
- ASP.NET 学习记录之一
(放着期末考试不复习,我每天废寝忘食地阅读从图书馆借来的ASP.NET相关专业书籍,到现在快一个星期,终于掌握了点东西,但是一拿到真刀真枪就做不出来什么了:老师把实验室打开,我就在这码码代码咯) As ...
- Windows 10和Visual Studio 2015 能给.Net方向的开发从业者带来什么?
.Net 多年前我们选择了你,现在在当前的移动互联网热火朝天的时代,你能给我们什么样的惊喜?面对IOS和android的势头,windows的移动端能否实现三国鼎立? windows 10 号称统一各 ...
- c# 强制转换, 隐式转换, 显式转换
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- JS原生效果瀑布流布局的实现(一)
JS原生效果 实现: HTML页面布局: <!DOCTYPE html> <html> <head> <meta charset="utf-8&qu ...
- Vector Calculus
Vector Fields Vector Function F(x,y,...)=P(x,y)i + Q(x,y)j + ... = <P(x,y), Q(x,y), ...> F=Pi ...