Quartz-Spring通过 @Scheduled驱动任务-CSDN博客

187 阅读2分钟

文章目录


在这里插入图片描述

概述

上一篇博文Quartz-Spring集成Quartz通过XML配置的方式中我们了解到了通过xml配置的方式集成Quartz,我们发现使用xml的方式,会配置很多bean的信息,但是如果使用注解的方式,会更方便,配置注解相对简单。


步骤

配置文件中增加task命名空间

xmlns:task="http://www.springframework.org/schema/task" 

http://www.springframework.org/schema/task   
http://www.springframework.org/schema/task/spring-task.xsd

配置Spring扫描和task扫描

 <!-- 定时任务扫描 -->
 <task:annotation-driven/>

编写带有注解的Job类

详见示例部分


示例

这里写图片描述

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:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/task
                           http://www.springframework.org/schema/task/spring-task.xsd">

    <!-- 扫描组件 -->
    <context:component-scan base-package="com.xgj.quartz.quartzWithSpring.anno"/>

    <!-- 定时任务扫描 -->
    <task:annotation-driven/>

</beans>

带有注解的Job类

package com.xgj.quartz.quartzWithSpring.anno;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class MyAnnoJob {

	@Scheduled(cron = "*/5 * * * * ?")
	// 每隔5秒执行一次
	public void test() throws Exception {
		System.out.println("Spring集成Quartz 使用 Annotation的方式......");
	}
}

测试类

package com.xgj.quartz.quartzWithSpring.anno;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringQuartzAnnoTest {
	public static void main(String[] args) {
		// 启动Spring 容器
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
				"classpath:com/xgj/quartz/quartzWithSpring/anno/spring-quartz-anno.xml");
		System.out.println("initContext successfully");
	}
}

运行结果

2017-11-12 08:44:44,353  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6fbdd27a: startup date [Sun Nov 12 08:44:44 BOT 2017]; root of context hierarchy
2017-11-12 08:44:44,436  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/quartz/quartzWithSpring/anno/spring-quartz-anno.xml]
2017-11-12 08:44:45,435  INFO [main] (ScheduledAnnotationBeanPostProcessor.java:262) - No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
initContext successfully
Spring集成Quartz 使用 Annotation的方式......
Spring集成Quartz 使用 Annotation的方式......
Spring集成Quartz 使用 Annotation的方式......
Spring集成Quartz 使用 Annotation的方式......
Spring集成Quartz 使用 Annotation的方式......
......
......
......
......省略....

@Scheduled解读

我们来看下源码

package org.springframework.scheduling.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
    String cron() default "";

    String zone() default "";

    long fixedDelay() default -1L;

    String fixedDelayString() default "";

    long fixedRate() default -1L;

    String fixedRateString() default "";

    long initialDelay() default -1L;

    String initialDelayString() default "";
}

可配置属性说明

属性类型属性属性说明
Stringcroncron的表达式
Stringzonecron表达式将被解析的时区
longfixedDelay在最后一次调用结束和下一次调用开始之间的固定时间段执行注释方法。
StringfixedDelayString在最后一次调用结束和下一次调用开始之间的固定时间段执行注释方法。
longfixedRate在调用之间以固定的时间段执行带注释的方法。
StringfixedRateString在调用之间以固定的时间段执行带注释的方法。
longinitialDelay在首次执行fixedRate()或fixedDelay()任务之前要延迟的毫秒数。
StringinitialDelayString在首次执行fixedRate()或fixedDelay()任务之前要延迟的毫秒数。

示例源码

代码已托管到Github—> github.com/yangshangwe…