zoukankan      html  css  js  c++  java
  • springboot任务之定时任务

    在启动入口上加上@EnableScheduling ,在需要定时的方法上加上@Scheduled注解

    比如:

    package com.gong.spingbootes.service;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Service;
    
    @Service
    public class ScheduledServcie {
        //秒、分、时、日、月、周几
        @Scheduled(cron = "0 * * * * MON-FRI")
        public void hello(){
            System.out.println("hell...");
        }
    }

    @Scheduled注解中主要参数为cron,里面有六个值,分别对应着注释中的。上述代码意思是:星期一到星期五的整秒执行方法一次。

    启动服务器,当时间是到13:22:00时,在控制台会输出:

    在比如:

    @Scheduled(cron="0,1,2,3,4 * * * * MON-FRI") :周一到周五的第0,1,2,3,4秒都会运行

    @Scheduled(cron="0-4 * * * * MON-FRI "):周一到周五的第0,1,2,3,4秒都会运行

    @Scheduled(cron="0/4 * * * * MON-FRI"):周一到周五从第0秒开始,每隔4秒执行一次

    具体的可以参照上述表格。

  • 相关阅读:
    搜索旋转排序数组
    SpringBoot整合mybatis
    《浪潮之巅》阅读笔记01
    阅读杂记01
    go home or stand up
    关于URL编码/javascript/js url 编码(轉)
    水晶报表 相关。
    Format函数(转)
    asp 亂碼問題。
    圣人不死,大盗不止
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12298736.html
Copyright © 2011-2022 走看看