Spring Boot 之 Scheduling Tasks定时任务
来源:互联网 发布:微表情心理学软件 编辑:程序博客网 时间:2024/05/16 18:01
<span style="color:#ffffff;"></span>
几乎大部分的应用都会有定时执行任务的需求。使用Spring Boot 之Scheduling Tasks 能够提高您的开发效率。
下载demo : git clone https://github.com/spring-guides/gs-scheduling-tasks.git ;
使用IDEA 或者eclipse 打开项目
进入 cd into gs-scheduling-tasks/initial 这个项目
src/main/java/hello/ScheduledTasks.java
package hello;import java.text.SimpleDateFormat;import java.util.Date;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Componentpublic class ScheduledTasks { private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class); private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000) public void reportCurrentTime() { log.info("The time is now {}", dateFormat.format(new Date())); }}
@Componet 注解 能使Spring 找到该类。@Scheduled 注解 定义一个特定的方法,fixedRate,表示任务开始执行时间间隔,单位毫米。f ixedDelay 表示 任务延迟执行,并
按照该时间间隔执行。也可以用更复杂些的定时配置 @Scheduled(cron=". . .") expressions for more sophisticated task scheduling.
启用定时功能
创建类
src/main/java/hello/Application.java
package hello;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication@EnableSchedulingpublic class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class); }}
@SpringBootApplication SpringBoot 项目的基础配置,详情请看上一章
@EnableScheduling 确保在后台创建一个任务执行者。
运行 main 方法
你将会看到 每5秒执行一次
[...]2016-08-25 13:10:00.143 INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:002016-08-25 13:10:05.143 INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:052016-08-25 13:10:10.143 INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:102016-08-25 13:10:15.143 INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:15原文:https://spring.io/guides/gs/scheduling-tasks/
0 0
- Spring Boot 之 Scheduling Tasks定时任务
- Scheduling Tasks定时任务
- spring boot:Scheduling Tasks
- Spring任务调度Scheduling Tasks
- spring - Scheduling Tasks 计划任务
- Spring任务调度Scheduling Tasks
- Spring Boot之定时调度Scheduled Tasks
- SpringBoot非官方教程 | 第十八篇: 定时任务(Scheduling Tasks)
- SpringBoot非官方教程 | 第十八篇: 定时任务(Scheduling Tasks)
- SpringBoot非官方教程 | 第十八篇: 定时任务(Scheduling Tasks)
- SpringBoot非官方教程 | 第十八篇: 定时任务(Scheduling Tasks)
- Spring Boot实战之定时任务
- Spring Boot定时任务之传参
- Spring Boot 定时任务之@Schedule
- spring-boot 定时任务之Scheduled Task
- Spring Boot 定时任务之Quartz
- spring boot 定时任务
- spring boot 定时任务
- Arduino—电机控制
- meta标签中的http-equiv属性使用介绍
- 链栈
- cordova Process finished with exit code -1
- JVM学习01——内存区域及内存溢出
- Spring Boot 之 Scheduling Tasks定时任务
- JavaSE 俄罗斯方块
- python安装scrapy框架
- python中argsort的使用-排序
- logstash 处理各种时间格式
- 数据库的基本操作之单列添加
- 300. Longest Increasing Subsequence
- 使用经验5
- get请求 post请求哪个更安全