首页 > 其他分享 >SpringBoot 定时任务示例

SpringBoot 定时任务示例

时间:2023-01-26 09:22:26浏览次数:56  
标签:SpringBoot 示例 springframework annotation cron org import 定时 public

示例

# ScheduledTaskService.java

package com.ln.myboot3.schedule;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ScheduledTaskService {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    private ScheduledTaskService() {
        System.out.println("ScheduledTaskService Construct.......");
    }


    @Scheduled(fixedRate = 5000) //通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行
    public void reportCurrentTime() {
        System.out.println("每隔5秒执行一次 " + dateFormat.format(new Date()));
    }

    @Scheduled(cron = "0 07 20 ? * *") //使用cron属性可按照指定时间执行,本例指的是每天20点07分执行
    //cron是UNIX和类UNIX(Linux)系统下的定时任务
    public void fixTimeExecution() {
        System.out.println("在指定时间 " + dateFormat.format(new Date()) + " 执行");
    }

    @Scheduled(cron = "0/10 * * * * ?")
    public void init() {
        this.prepare();
    }

    public void prepare() {
        System.out.println("=== 每10s执行一次,now time:" + dateFormat.format(new Date()));
    }
}

#配置 TaskScheduleConfig

package com.ln.myboot3.schedule;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;

@Configuration
@ComponentScan()
@EnableScheduling
public class TaskScheduleConfig {
    @Bean
    public TaskScheduler taskScheduler() {
        return new ConcurrentTaskScheduler();
    }
}

# 启动 Myboot3Application

package com.ln.myboot3;

import com.ln.myboot3.schedule.TaskScheduleConfig;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

@SpringBootApplication
public class Myboot3Application {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskScheduleConfig.class);
    }

}

 

cron含义

[秒] [分] [时] [日] [月] [周] [年]

线上工具: https://cron.qqe2.com/

 

标签:SpringBoot,示例,springframework,annotation,cron,org,import,定时,public
From: https://www.cnblogs.com/kaituorensheng/p/17067557.html

相关文章

  • 230125_50_SpringBoot入门
    SpringBoot实战:员工管理系统1.静态资源导入可以从百度网盘获取资源:链接:https://pan.baidu.com/s/1x-6U_NCNEhIXOq0CcvRW-g提取码:mg94复制这段内容后打开百度......
  • SpringBoot 教程
    什么是SpringBootSpringBoot教程提供了SpringFramework的基本和高级概念。我们的SpringBoot教程面向初学者和专业人士。SpringBoot是一个Spring模块,为Spring框架提......
  • Rust 中 HashSet 的基础用法示例
    代码:usestd::collections::{HashSet,HashMap};fnmain(){letmutset:HashSet<i32>=HashSet::new();set.insert(1);set.insert(2);//插入元素......
  • Rust中 HashMap 的基础用法示例
    代码:usestd::collections::HashMap;fnmain(){//创建一个hash-map,key为字符串类型,value为无符号整数类型letmutmap:HashMap<&str,u32>=HashMap::new();......
  • Hadoop MapReduce介绍、官方示例及执行流程Apache Hadoop概述
    Hadoop离线是大数据生态圈的核心与基石,是整个大数据开发的入门。本次分享内容让初学者能高效、快捷掌握Hadoop必备知识,大大缩短Hadoop离线阶段学习时间,下面一起开始今天的学......
  • 230124_50_SpringBoot入门
    thymeleaf语法1.th:utext,转义文本controllermodel.addAttribute("msg","<h1>hello,springboot!</h1>");html<divth:text="${msg}"></div><divth:utext="......
  • 多线程案例-实现定时器
    1.定时器是什么定时器是软件开发中的一个重要组件,功能是当达到一个特定的时间后,就执行某个指定好的代码定时器是一个非常常用的组件,特别是在网络编程中,当出现了"连接......
  • springboot+myBatis
    mybatis依赖<!--mybatis依赖--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-start......
  • SpringBoot开发Restful API及使用jmeter测试
     Restful API简介Representational State Transfer,简称为REST, 即表现层状态转化,简单来说,客户端通过HTTP方法对服务器的资源进行操作, 实现表现层状态转化GET:获取......
  • 230123_50_SpringBoot入门
    首页和图标定制首页:新建index文件,放到静态资源加载目录<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>首页-测试</title></head>......