【springboot】定时任务

package com.example.demo.util;

import lombok.extern.slf4j.Slf4j;
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 org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
@EnableScheduling //开启定时任务
@EnableAsync //开启多线程
@Slf4j
public class ScheduleAsync {
    
    
    /** cron
     * [秒] [分] [小时] [日] [月] [周] [年]
     * 年非必须
     * *每秒/分/小时/日...都会触发
     * ?不指定值,只可用于日和周
     * 1-3表示区间,1、2、3都会触发
     * 5/15表示递增触发,从5开始,每15秒触发
     * L表示最后一天或周六
    */
    @Async
    @Scheduled(cron = "0/10 * * * * ?")//从00开始每十秒
    public void first(){
    
    
        log.info(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }

    @Async
    @Scheduled(cron = "0 0 2 * * ?")//每天半夜两点
    public void second(){
    
    
        log.info(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }
}

猜你喜欢

转载自blog.csdn.net/lorogy/article/details/113759199