天天看點

Spring Task定時任務的配置和使用詳解

參考位址:http://www.cnblogs.com/chen-lhx/p/5581129.html

spring中使用定時任務

基于xml配置檔案使用定時任務

首先配置spring開啟定時任務

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

<

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

/>

<!-- 定時器開關-->

<

bean

id

=

"myTask"

class

=

"com.spring.task.MyTask"

></

bean

>

<

task:scheduled-tasks

>

<!-- 這裡表示的是每隔五秒執行一次 -->

<

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

>

定義自己的任務執行邏輯

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

package

com.spring.task;

public

class

MyTask {

public

void

show() {

System.out.println(

"show method 1"

);

}

public

void

print() {

System.out.println(

"print method 1"

);

}

}

基于注解使用定時任務

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

package

com.spring.task;

import

org.springframework.scheduling.annotation.Scheduled;

import

org.springframework.stereotype.Component;

@Component

public

class

MyTask2 {

@Scheduled

(cron =

"0 0 1 * * *"

)

public

void

show() {

System.out.println(

"show method 2"

);

}

@Scheduled

(fixedRate =

1000

*

2

public

void

print() {

System.out.println(

"print method 2"

);

}

}

這樣,當項目啟動,定時任務就會按照規則按時執行了。

Spring Boot中使用定時任務

Spring Boot中使用更加友善。

引入

springboot starter

1 2 3 4

<

dependency

>

<

groupId

>org.springframework.boot</

groupId

>

<

artifactId

>spring-boot-starter</

artifactId

>

</

dependency

>

在程式入口啟動類添加

@EnableScheduling

,開啟定時任務功能

1 2 3 4 5 6 7

@SpringBootApplication

@EnableScheduling

public

class

Application {

public

static

void

main(String[] args) {

SpringApplication.run(Application.

class

, args);

}

定義定時任務邏輯

1 2 3 4 5 6 7 8 9 10

@Component

public

class

MyTask3 {

private

int

count=

;

@Scheduled

(cron=

"*/6 * * * * ?"

)

private

void

process() {

System.out.println(

"this is scheduler task runing "

+(count++));

}

}

任務執行規則說明

先來看看

@Scheduled

注解的源碼

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

@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種參數:

  1. cron:指定cron表達式
  2. zone:預設使用伺服器預設時區。可以設定為

    java.util.TimeZone

    中的zoneId
  3. fixedDelay:從上一個任務完成開始到下一個任務開始的間隔,機關毫秒
  4. fixedDelayString:同上,時間值是

    String

    類型
  5. fixedRate:從上一個任務開始到下一個任務開始的間隔,機關毫秒
  6. fixedRateString:同上,時間值是

    String

    類型
  7. initialDelay:任務首次執行延遲的時間,機關毫秒
  8. initialDelayString:同上,時間值是

    String

    類型

    cron表達式的使用方法