Detailed explanation of the configuration and use of Spring Task timing tasks


Reference address: http://www.cnblogs.com/chen-lhx/p/5581129.html


Using timed tasks in spring

Using timed tasks based on xml configuration files

First configure spring to open the scheduled task

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
 
   < 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 >

Define your own task execution logic

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" );
   }
}

Using timed tasks based on annotations

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 {
 
   /**
    * 定时计算。每天凌晨 01:00 执行一次
    */
   @Scheduled (cron = "0 0 1 * * *" )
   public void show() {
     System.out.println( "show method 2" );
   }
 
   /**
    * 启动时执行一次,之后每隔2秒执行一次
    */
   @Scheduled (fixedRate = 1000 * 2
   public void print() {
     System.out.println( "print method 2" );
   }
}

In this way, when the project starts, the scheduled task will be executed on time according to the rules.

Using scheduled tasks in Spring Boot

It is more convenient to use in Spring Boot.

import springboot starterpackage

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= 0 ;
 
  @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表达式的使用方法

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324669794&siteId=291194637