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
 
   < 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 {
 
   /**
    * 定时计算。每天凌晨 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

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表达式的使用方法
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring配置定时任务,你需要进行以下步骤: 1. 在Spring配置文件中添加命名空间声明,并指定相应的xsd版本号。你可以使用以下的配置格式:xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd" 2. 启用注解驱动的任务调度器。你可以使用以下的配置格式:<task:annotation-driven scheduler="dataScheduler"/> 其中"dataScheduler"是自定义的任务调度器名称。 3. 配置任务调度器的线程池大小。如果你的项目中有多个任务需要定时执行,并且可能会相互交叉执行,你需要根据任务的具体执行情况来配置线程池大小。你可以使用以下的配置格式:<task:scheduler id="dataScheduler" pool-size="5"/> 其中"pool-size"指定了线程池的大小。 请注意,Spring的任务调度默认是单线程的,如果你的项目中有多个任务需要定时执行,并且执行时间可能相互交叉,你需要配置线程池以确保任务可以同时执行。 你可以在Spring的官方文档中找到更多关于配置定时任务的详细信息和示例代码。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [spring如何设置定时任务详解(@Scheduled)](https://blog.csdn.net/CSDN___LYY/article/details/85266567)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值