博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot -- 定时执行
阅读量:3921 次
发布时间:2019-05-23

本文共 1378 字,大约阅读时间需要 4 分钟。

在这里插入图片描述

1、主程序添加 @EnableScheduling

package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableAsync;import org.springframework.scheduling.annotation.EnableScheduling;//@EnableAsync@EnableScheduling@SpringBootApplicationpublic class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args); }}

2、测试:

package com.example.service;import org.springframework.scheduling.annotation.Async;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;@Servicepublic class TestService {
/** * second(秒), minute(分), hour(时), day of month(日), month(月), day of week(周几). * 0 * * * * MON-FRI * 【0 0/5 14,18 * * ?】 每天14点整,和18点整,每隔5分钟执行一次 * 【0 15 10 ? * 1-6】 每个月的周一至周六10:15分执行一次 * 【0 0 2 ? * 6L】每个月的最后一个周六凌晨2点执行一次 * 【0 0 2 LW * ?】每个月的最后一个工作日凌晨2点执行一次 * 【0 0 2-4 ? * 1#1】每个月的第一个周一凌晨2点到4点期间,每个整点都执行一次; */ // @Scheduled(cron = "0 * * * * MON-SAT") //@Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT") // @Scheduled(cron = "0-4 * * * * MON-SAT") @Scheduled(cron = "0/4 * * * * MON-SAT") //每4秒执行一次 public void hello(){
System.out.println("hello"); }}

cron 值对应的是:

秒 分 时 日 月 周

这个方法就会每天从零秒开始,每四秒执行一次

转载地址:http://cyern.baihongyu.com/

你可能感兴趣的文章
OpenTelemetry - 云原生下可观测性的新标准
查看>>
使用 ML.NET 实现峰值检测来排查异常
查看>>
通过 .NET NativeAOT 实现用户体验升级
查看>>
如何友好的处理 WebApi 中抛出的错误
查看>>
因MemoryCache闹了个笑话
查看>>
Dotnet的垃圾回收
查看>>
乘风破浪,.Net Core遇见Dapr,为云原生而生的分布式应用运行时
查看>>
gRPC在C#中的未来属于grpc-dotnet
查看>>
快速排序的性能和名字一样优秀
查看>>
开源推荐:Asp.Net Core入门学习手册!
查看>>
ML.NET 示例:对象检测
查看>>
C#基于yolov3的行人检测
查看>>
ML.NET Cookbook:(16)什么是规范化?为什么我需要关心?
查看>>
WPF 修改(优化)Menu菜单的样式
查看>>
晕了!这个配置值从哪来的?
查看>>
我开发了一款基于web容器的前端项目容器
查看>>
WPF实现环(圆)形菜单
查看>>
WPF 写一个提醒工具软件(完整项目)
查看>>
NET问答: 多个 await 和 Task.WaitAll 是等价的吗?
查看>>
MIPS衰落 LoongArch崛起
查看>>