當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Task定时任务的配置和使用详解
生活随笔
收集整理的這篇文章主要介紹了
Spring Task定时任务的配置和使用详解
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
spring中使用定時任務(wù)
1、基于xml配置文件使用定時任務(wù)
首先配置spring開啟定時任務(wù)
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"> <task:annotation-driven /> <!-- 定時器開關(guān)--> <bean id="myTask" class="com.spring.task.MyTask"></bean> <task:scheduled-tasks> <!-- 這里表示的是每隔五秒執(zhí)行一次 --> <task:scheduled ref="myTask" method="show" cron="*/5 * * * * ?" /> <task:scheduled ref="myTask" method="print" cron="*/10 * * * * ?"/> </task:scheduled-tasks> <!-- 自動掃描的包名 --><context:component-scan base-package="com.spring.task" /> </beans>定義自己的任務(wù)執(zhí)行邏輯
package com.spring.task; /** * 定義任務(wù) */ public class MyTask { public void show() { System.out.println("show method 1"); } public void print() { System.out.println("print method 1"); } }2、基于注解使用定時任務(wù)
package com.spring.task; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /*** 基于注解的定時器 */ @Component public class MyTask2 { /*** 定時計算。每天凌晨 01:00 執(zhí)行一次*/@Scheduled(cron = "0 0 1 * * *")public void show() {System.out.println("show method 2"); } /*** 啟動時執(zhí)行一次,之后每隔2秒執(zhí)行一次 */@Scheduled(fixedRate = 1000*2) public void print() { System.out.println("print method 2");} }這樣,當項目啟動,定時任務(wù)就會按照規(guī)則按時執(zhí)行了。
3、Spring Boot中使用定時任務(wù)
Spring Boot中使用更加方便。
引入springboot starter包
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId> </dependency>在程序入口啟動類添加@EnableScheduling,開啟定時任務(wù)功能
@SpringBootApplication @EnableScheduling public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}定義定時任務(wù)邏輯
@Component public class MyTask3 {private int count=0;@Scheduled(cron="*/6 * * * * ?")private void process() {System.out.println("this is scheduler task runing "+(count++));} }任務(wù)執(zhí)行規(guī)則說明
先來看看@Scheduled注解的源碼
@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 -1;String fixedDelayString() default "";long fixedRate() default -1;String fixedRateString() default "";long initialDelay() default -1;String initialDelayString() default ""; }可以看出,注解中可以傳8種參數(shù):
cron表達式的使用方法
Cron表達式是一個字符串,字符串以5或6個空格隔開,分為6或7個域,每一個域代表一個含義,Cron有如下兩種語法格式:
每一個域可出現(xiàn)的字符如下:
每一個域都使用數(shù)字,但還可以出現(xiàn)如下特殊字符,它們的含義是:
?
?
看來這個東西還是要經(jīng)常用才不會忘啊。。。?
?
轉(zhuǎn)載于:https://www.cnblogs.com/luxd/p/7525121.html
總結(jié)
以上是生活随笔為你收集整理的Spring Task定时任务的配置和使用详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4则运算代码地址
- 下一篇: (转)写的非常好的一个WPF学习之路