Quartz-Spring通过 @Scheduled驱动任务
【摘要】
文章目录
概述步骤配置文件中增加task命名空间配置Spring扫描和task扫描编写带有注解的Job类
示例@Scheduled解读示例源码
概述
上一篇博文Quartz-Sp...

概述
上一篇博文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
- 1
- 2
- 3
- 4
配置Spring扫描和task扫描
<!-- 定时任务扫描 -->
<task:annotation-driven/>
- 1
- 2
编写带有注解的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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
带有注解的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的方式......");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
测试类
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");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
运行结果
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的方式......
......
......
......
......省略....
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
@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 "";
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
可配置属性说明
属性类型 | 属性 | 属性说明 |
---|---|---|
String | cron | cron的表达式 |
String | zone | cron表达式将被解析的时区 |
long | fixedDelay | 在最后一次调用结束和下一次调用开始之间的固定时间段执行注释方法。 |
String | fixedDelayString | 在最后一次调用结束和下一次调用开始之间的固定时间段执行注释方法。 |
long | fixedRate | 在调用之间以固定的时间段执行带注释的方法。 |
String | fixedRateString | 在调用之间以固定的时间段执行带注释的方法。 |
long | initialDelay | 在首次执行fixedRate()或fixedDelay()任务之前要延迟的毫秒数。 |
String | initialDelayString | 在首次执行fixedRate()或fixedDelay()任务之前要延迟的毫秒数。 |
示例源码
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster
文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。
原文链接:artisan.blog.csdn.net/article/details/78514774
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)