當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Quartz-Spring通过 @Scheduled驱动任务
生活随笔
收集整理的這篇文章主要介紹了
Quartz-Spring通过 @Scheduled驱动任务
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 概述
- 步驟
- 配置文件中增加task命名空間
- 配置Spring掃描和task掃描
- 編寫帶有注解的Job類
- 示例
- @Scheduled解讀
- 示例源碼
概述
上一篇博文Quartz-Spring集成Quartz通過XML配置的方式中我們了解到了通過xml配置的方式集成Quartz,我們發(fā)現(xiàn)使用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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/taskhttp://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秒執(zhí)行一次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 ""; }可配置屬性說明
| String | cron | cron的表達式 |
| String | zone | cron表達式將被解析的時區(qū) |
| long | fixedDelay | 在最后一次調用結束和下一次調用開始之間的固定時間段執(zhí)行注釋方法。 |
| String | fixedDelayString | 在最后一次調用結束和下一次調用開始之間的固定時間段執(zhí)行注釋方法。 |
| long | fixedRate | 在調用之間以固定的時間段執(zhí)行帶注釋的方法。 |
| String | fixedRateString | 在調用之間以固定的時間段執(zhí)行帶注釋的方法。 |
| long | initialDelay | 在首次執(zhí)行fixedRate()或fixedDelay()任務之前要延遲的毫秒數(shù)。 |
| String | initialDelayString | 在首次執(zhí)行fixedRate()或fixedDelay()任務之前要延遲的毫秒數(shù)。 |
示例源碼
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster
總結
以上是生活随笔為你收集整理的Quartz-Spring通过 @Scheduled驱动任务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Quartz-Spring集成Quart
- 下一篇: gradle idea java ssm