Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Spring Task定时任务的配置和使用详解大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

记录下Spring自带的定时任务用法

spring中使用定时任务

基于xml配置文件使用定时任务

首先配置spring开启定时任务

@H_489_13@ <beans xmlns="http://www.springframework.org/scheR_918_11845@a/beans" xmlns:xsi="http://www.w3.org/2001/XMLscheR_918_11845@a-instance" xmlns:p="http://www.springframework.org/scheR_918_11845@a/p" xmlns:task="http://www.springframework.org/scheR_918_11845@a/task" xmlns:context="http://www.springframework.org/scheR_918_11845@a/context" xmlns:aop="http://www.springframework.org/scheR_918_11845@a/aop" xsi:scheR_918_11845@aLOCATIOn="http://www.springframework.org/scheR_918_11845@a/beans http://www.springframework.org/scheR_918_11845@a/beans/spring-beans-4.0.xsd http://www.springframework.org/scheR_918_11845@a/tx http://www.springframework.org/scheR_918_11845@a/tx/spring-tx-4.0.xsd http://www.springframework.org/scheR_918_11845@a/jee http://www.springframework.org/scheR_918_11845@a/jee/spring-jee-4.0.xsd http://www.springframework.org/scheR_918_11845@a/context http://www.springframework.org/scheR_918_11845@a/context/spring-context-4.0.xsd http://www.springframework.org/scheR_918_11845@a/aop http://www.springframework.org/scheR_918_11845@a/aop/spring-aop-4.0.xsd http://www.springframework.org/scheR_918_11845@a/task http://www.springframework.org/scheR_918_11845@a/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>

定义自己的任务执行逻辑

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

基于注解使用定时任务

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

这样,当项目启动,定时任务就会按照规则按时执行了。

Spring Boot中使用定时任务

Spring Boot中使用更加方便

引入springboot starter

@H_489_13@ <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>

在程序入口启动类添加@Enablescheduling,开启定时任务@L_673_10@

@SpringBootApplication
@Enablescheduling
public class Application {

 public static void main(String[] args) {
   SpringApplication.run(Application.class,args);
 }

定义定时任务逻辑

@Component
public class myTask3 {

 private int count=0;

 @scheduled(cron="*/6 * * * * ?")
 private void process() {
   System.out.println("this is scheduler task runing "+(count++));
 }
}

任务执行规则说明

先来看看@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种参数:

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

Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式:

  1. Seconds minutes Hours DayofMonth Month DayofWeek Year
  2. Seconds minutes Hours DayofMonth Month DayofWeek

一个域可出现的字符如下:

  1. Seconds: 可出现",- * /"四个字符,有效范围为0-59的整数
  2. minutes: 可出现",- * /"四个字符,有效范围为0-59的整数
  3. Hours: 可出现",- * /"四个字符,有效范围为0-23的整数
  4. DayofMonth: 可出现",- * / ? L W C"八个字符,有效范围为0-31的整数
  5. @H_594_74@month: 可出现",- * /"四个字符,有效范围为1-12的整数或JAN-DEC
  6. DayofWeek: 可出现",- * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推
  7. Year: 可出现",- * /"四个字符,有效范围为1970-2099年

一个域都使用数字,但还可以出现如下特殊字符,它们的含义是:

  1. *:表示匹配该域的任意值,假如在minutes域使用*,即表示每分钟都会触发事件。
  2. ?:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。因为DayofMonth和 DayofWeek会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 13 13 15 20 ?,其中最后一位只能用?,而不能使用,如果使用*表示不管星期几都会触发,实际上并不是这样。
  3. -:表示范围,例如在minutes域使用5-20,表示从5分到20分钟每分钟触发一次。
  4. /:表示起始时间开始触发,然后每隔固定时间触发一次,例如在minutes域使用5/20,则意味着5分钟触发一次,而25,45等分别触发一次。
  5. ,:表示列出枚举值值。例如:在minutes域使用5,20,则意味着在5和20分每分钟触发一次。
  6. L:表示最后,只能出现在DayofWeek和DayofMonth域,如果在DayofWeek域使用5L,意味着在最后的一个星期四触发。
  7. W:表示有效工作日(周一到周五),只能出现在DayofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 DayofMonth使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一 到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份。
  8. LW:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。
  9. #:用于确定每个月第几个星期几,只能出现在DayofMonth域。例如在4#2,表示某月的第二个星期三。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

大佬总结

以上是大佬教程为你收集整理的Spring Task定时任务的配置和使用详解全部内容,希望文章能够帮你解决Spring Task定时任务的配置和使用详解所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:springtask