Spring - 定时任务

Spring定时任务

两步实现定时任务

  • 开启定时任务注解
  • 设置执行时间
package top.onefine.schedule;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.DateFormat;
import java.util.Date;

@EnableScheduling  // 开启定时任务注解
@SpringBootApplication
public class ScheduleApplication {
    
    

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

    // 半小时执行一次
//    @Scheduled(fixedRate = 30 * 60 * 1000)  // 以毫秒为单位,启动时就会执行一次
    @Scheduled(fixedRate = 3 * 1000)
    public void playSomething1() {
    
    
        System.out.println("执行任务1:" + DateFormat.getDateInstance().format(new Date()));
    }

    // 四小时执行一次
//    @Scheduled(fixedRate = 4 * 60 * 60 * 1000)
    @Scheduled(fixedRate = 6 * 1000)
    public void playSomething12() {
    
    
        System.out.println("执行任务2:" + DateFormat.getDateInstance().format(new Date()));
    }
}

cron表达式

package top.onefine.schedule;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.DateFormat;
import java.util.Date;

@EnableScheduling  // 开启定时任务注解
@SpringBootApplication
public class ScheduleApplication {
    
    

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

    @Scheduled(cron = "0 0/30 9-22 * * ?")  // 每天9点到22点每隔30分钟执行一次
    public void playSomething1() {
    
    
        System.out.println("执行任务1:" + DateFormat.getDateInstance().format(new Date()));
    }

    @Scheduled(cron = "0 0 9-22/4 * * ?")  // 每天9点到22点每隔4小时执行一次
    public void playSomething12() {
    
    
        System.out.println("执行任务2:" + DateFormat.getDateInstance().format(new Date()));
    }
}

在线生成cron表达式:https://cron.qqe2.com/

在这里插入图片描述

cron表达式在spring中由六个部分组成(最后一个部分不被spring支持),每个部分之间由空格分隔开来。


dayofweek中1表示星期天,2表示星期一…7表示星期六。

在这里插入图片描述

注:
以1: 5为例,分钟上5/20表示1: 5触发,1: 25触发,1: 45触发;1点就结束了!下一次从2点开始,2: 5触发,2: 25触发…

在这里插入图片描述

异步多线程实现

定时任务默认是单线程的定时任务,如果任务持续时间较长,就会将后续定时任务拖延,导致丢失任务。

两步实现

  • 开启异步注解
  • 设置异步执行
package top.onefine.schedule;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.DateFormat;
import java.util.Date;

@EnableScheduling  // 开启定时任务注解
@EnableAsync  // 开启异步注解
@SpringBootApplication
public class ScheduleApplication {
    
    

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

    @Async  // 设置异步执行
//    @Scheduled(cron = "1 0 0 0 0 ?")
    @Scheduled(fixedRate = 1000)
    public void playSomething1() {
    
    
        System.out.println(Thread.currentThread().getName() + "执行任务1:" + DateFormat.getDateInstance().format(new Date()));
    }

    @Async  // 设置异步执行
//    @Scheduled(cron = "2 0 0 0 0 ?")
    @Scheduled(fixedRate = 2000)
    public void playSomething12() {
    
    
        System.out.println(Thread.currentThread().getName() + "执行任务2:" + DateFormat.getDateInstance().format(new Date()));
    }
}

在这里插入图片描述

总结

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/jiduochou963/article/details/106366541