日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Quartz-Spring通过 @Scheduled驱动任务

發(fā)布時間:2025/3/21 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 ""; }

可配置屬性說明

屬性類型屬性屬性說明
Stringcroncron的表達式
Stringzonecron表達式將被解析的時區(qū)
longfixedDelay在最后一次調用結束和下一次調用開始之間的固定時間段執(zhí)行注釋方法。
StringfixedDelayString在最后一次調用結束和下一次調用開始之間的固定時間段執(zhí)行注釋方法。
longfixedRate在調用之間以固定的時間段執(zhí)行帶注釋的方法。
StringfixedRateString在調用之間以固定的時間段執(zhí)行帶注釋的方法。
longinitialDelay在首次執(zhí)行fixedRate()或fixedDelay()任務之前要延遲的毫秒數(shù)。
StringinitialDelayString在首次執(zhí)行fixedRate()或fixedDelay()任務之前要延遲的毫秒數(shù)。

示例源碼

代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster

總結

以上是生活随笔為你收集整理的Quartz-Spring通过 @Scheduled驱动任务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。