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

  1. Spring quartz定时任务service注入问题

    今天想单元测试一下spring中的quartz定时任务,job类的大致结构和下面的SpringQtz1类相似,我的是实现的org.quartz.Job接口,到最后总是发现job类里注入的service ...

  2. spring quartz定时任务

    配置quartz 在spring中需要三个jar包: quartz-1.8.5.jar.commons-collections-3.2.1.jar.commons-logging-1.1.jar 首先 ...

  3. [Spring] Java spring quartz 定时任务

    首先,需要导入quartz 的jar包 ① applicationContext.xml <!-- 轮询任务 --> <import resource="classpath ...

  4. Spring Quartz定时任务设置

    这里主要记录一下定时任务的配置,偏向于记录型的一个教程,这里不阐述Quartz的原理. 首先,在Spring配置文件里配置一个自己写好的一个包含执行任务方法的一个类. <bean id=&quo ...

  5. 一看便知spring+quartz定时任务

    这是我经过网上收集然后加上自己的测试写的,以便大家使用 标配:已测 注意需要的包:(在已经配置spring 的情况下) quartz-all-1.6.jar        spring-context ...

  6. Spring+Quartz(定时任务)

    此处用到的Quartz版本是quartz-2.2.3 官方网站:http://www.opensymphony.com/quartz 首先先介绍用到的几个关键类:scheduler任务调度.Job任务 ...

  7. spring + Quartz定时任务配置

    <bean id="exportBatchFileTask" class="com.ydcn.pts.task.ExportBatchFileTask"& ...

  8. spring + quartz定时任务,以及修改定时任务

    spring4+quartz2.2.3,定时任务弄好了,修改定时任务没折腾起,没找到合适的解决方案. 最终使用库spring-context-support 3.2.17.RELEASE +  qua ...

  9. Java Spring Quartz 定时任务

    公司需要使用JAVA的WebServer完成简单的定时跑任务的工作.其他例如:每隔30分钟执行锁定用户解锁任务. Quartz 在开源任务调度框架中的翘首,它提供了强大任务调度机制,难能可贵的是它同时 ...

随机推荐

  1. hadoop2的高可用性

    1   hadoop2  namenode由一个节点变成两个节点,同时在线,且同时只有一个是活跃的,如果一个出了问题,另外一个立即接替:没必要配置Secondary NameNode.Checkpoi ...

  2. react native 之 react-native-image-picke的详细使用图解

    最近需要在react native项目中集成相机和相册的功能,于是在网上找了一个好用的第三方插件:react-native-image-picke. 该插件可以同时给iOS和Android两个平台下使 ...

  3. 百川sdk----自己的WebViewClient不被执行

    我在百川sdk的旺旺群中,追问这个问题N多次,一直没有人答复,哎,凡事都要靠自己..... 1.先查看下百川sdk中,是怎么处理咱们传递过去的 WebViewClient public class l ...

  4. jQuery method and examples

    一:介绍: jQuery:是DOM和js的封装.jQuery是一个兼容多浏览器的javascript库,核心理念是write less,do more(写得更少,做得更多).现在大多数的pc端的网站都 ...

  5. Linux 和 Windows 常用工具列表

    1. Windows 篇 1)MobaItem 一款优秀的Windows下模拟Linux Terminal工具,同时也可以用来远程Linux服务器. 2. Linux 篇 2) MC Linux下非常 ...

  6. linux压缩解压文件

    首先进入文件夹 cd /home/ftp2/1520/web 压缩方法一:压缩web下的888.com网站 zip -r 888.com.zip888.com 压缩方法二:将当前目录下的所有文件和文件 ...

  7. ICTCLAS 汉语词性标注集

    以前使用jieba分词时,并没有注意到词性标注集到底包含哪些,刚好最近学习自然语言处理,涉及到分词以及词性标注,将ICTCLAS 词性标注集记录如下: ICTCLAS 汉语词性标注集 代码 名称 帮助 ...

  8. 设为首页 添加到收藏夹 (share)

    设为首页,添加到收藏夹 分享自:http://my.oschina.net/lyx2012/blog/60036 设为首页 和 收藏本站js代码 兼容IE,chrome,ff <script t ...

  9. 前后台数据交互 后台封装数据 json格式

    namespace ~.sverIterface { public class EventPlayerInfo { public string name { get; set; } public st ...

  10. Ajax+JQuery

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...