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. RadioButtonFor绑定值

    <div class="form-group"> <label class="control-label col-md-2">是否< ...

  2. codeforces 734E(DFS,树的直径(最长路))

    题目链接:http://codeforces.com/contest/734/problem/E 题意:有一棵黑白树,每次操作可以使一个同色连通块变色,问最少几次操作能使树变成全黑或全白. 思路:先进 ...

  3. WCF 部署在Windows 2012 IIS上各种报错的解决方法

    1.由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本 ,请添加处理程序.如果勇载文件,请添加 MIME 映射. 以管理员身份,在cmd中运行C:\Windows\Microsoft.NET\F ...

  4. Html 之div+css布局之css选择器

    CSS选择器 什么叫选择器?通俗的来说就是 我想改变html 中某个地方的  字体大小 或者背景色 或者其它属性 内边距 外边距,宽度高度 等等 一些Css 样式. 那么我们如何找到对应的 元素呢? ...

  5. css实现隐藏显示

    <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" ...

  6. map 容器的使用

    C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值. 一.map的说明    1   头文件   #include   <map> ...

  7. Javascript 事件对象(五)事件捕获

    事件捕获: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" c ...

  8. 一个assert的写法

    ]; int assert_buf_len; #ifdef XXX_DEBUG #define assert(expr, ...) \ do{ \ if ((!(expr))) \ {\ char * ...

  9. Wiki语料处理

    最近在做知识图谱相关工作,源数据主要来自百度百科,互动百科,中文维基百科等.其中中文维基百科提供数据库下载,下文主要讨论如何处理Wiki数据. 1. 中文维基数据下载 下载dump:https://d ...

  10. Javaweb学习随笔_JSP的九大内置对象

    JSP内置对象整理 1. 九大内置对象: out,request,response,session,application,page,pageContext,config,Exception. 1.1 ...