Spring Boot实战之定时任务
来源:互联网 发布:淘宝店铺运营方案 编辑:程序博客网 时间:2024/05/16 01:57
Spring Boot实战之定时任务
本文主要介绍如何在Spring Boot中使用定时任务
1、创建定时任务类ScheduledTasks
package com.xiaofangtech.sunt.tasks;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Componentpublic class ScheduledTasks {private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000) public void reportCurrentTime() { System.out.println("The time is now " + dateFormat.format(new Date())); }}
package com.xiaofangtech.sunt;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication@EnableSchedulingpublic class SpringBeanApplication {public static void main(String[] args) {SpringApplication.run(SpringBeanApplication.class, args);}}
其中 @EnableScheduling 注解的作用是发现注解@Scheduled的任务并后台执行。
3、运行
每隔5秒钟打印一次时间
The time is now 20:55:04The time is now 20:55:09The time is now 20:55:14The time is now 20:55:19The time is now 20:55:24The time is now 20:55:29
1 0
- Spring Boot实战之定时任务
- Spring Boot 之 Scheduling Tasks定时任务
- Spring Boot定时任务之传参
- Spring Boot 定时任务之@Schedule
- spring-boot 定时任务之Scheduled Task
- Spring Boot 定时任务之Quartz
- spring boot 定时任务
- spring boot 定时任务
- spring boot定时任务
- Spring boot 定时任务
- Spring Boot 定时任务
- Spring Boot 实现定时任务
- spring boot创建定时任务
- Spring Boot定时任务实例
- 【Spring Boot】定时任务实现
- Spring Boot 实现定时任务
- spring boot实现定时任务
- Spring Boot : 定时任务(十)
- java 多线程之Sychronization的三种使用情况
- POJ 1125 Stockbroker Grapevine
- Android应用开发SharedPreferences存储数据的使用方法
- Asynctask实现源码分析
- bzoj 4590: [Shoi2015]自动刷题机
- Spring Boot实战之定时任务
- C#控制台基础 helloworld之 用类中的普通委托的事件输出
- JavaScript 学习(二)之基本用法
- 夜神模拟器SD卡文件存储位置
- Ticket管理工具:Redmine
- Code Review 总结一
- JQuery特效多张图片上传
- POJ 1287Networking【prim】
- 关于ARGB_8888、ALPHA_8、ARGB_4444、RGB_565的理解