[Spring] - Quartz定时任务 - Annotation

Spring + Quartz可使用annoation方式:html

 

一、AppJob类:spring

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的写法,可参见:spring-mvc

http://www.cnblogs.com/HD/p/4205937.htmlmvc

 

二、Spring配置文件:app

<?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>

 

三、运行类:spa

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();
    }
}

 

运行结果:code

 


 

 

固然,Spring的配置文件也能够这样写:component

    <!-- 定时器开关-->
    <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方式是编译式的。xml