Spring Boot 与 Kotlin 定时任务(Scheduling Tasks)

Stella981
• 阅读 638

在编写Spring Boot应用中会遇到这样的场景,比如:需要定时地发送一些短信、邮件之类的操作,也可能会定时地检查和监控一些标志、参数等。

创建定时任务

在Spring Boot中编写定时任务是非常简单的事,下面通过实例介绍如何在Spring Boot中创建定时任务,实现每过5秒输出一下当前时间。

在Spring Boot的主类中加入@EnableScheduling注解,启用定时任务的配置

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.scheduling.annotation.EnableScheduling


/**
 * Created by http://quanke.name on 2018/1/12.
 */


@SpringBootApplication
@EnableScheduling
class Application

fun main(args: Array<String>) {
    SpringApplication.run(Application::class.java, *args)
}

创建定时任务实现类

import org.apache.commons.logging.LogFactory
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
import java.text.SimpleDateFormat
import java.util.*


/**
 * Created by http://quanke.name on 2018/1/12.
 */
@Component
class ScheduledTasks {

    val log = LogFactory.getLog(ScheduledTasks::class.java)!!

    private val dateFormat = SimpleDateFormat("HH:mm:ss")

    @Scheduled(fixedRate = 1000)
    fun reportCurrentTime() {
        log.info("现在时间 , ${dateFormat.format(Date())}")
    }
}

运行程序,控制台中可以看到类似如下输出,定时任务开始正常运作了。

2018-01-21 23:09:01.112  INFO 23832 --- [           main] n.q.kotlin.chaper11_8_1.ApplicationKt    : Started ApplicationKt in 8.024 seconds (JVM running for 8.724)
2018-01-21 23:09:02.112  INFO 23832 --- [pool-2-thread-1] n.q.k.chaper11_8_1.task.ScheduledTasks   : 现在时间 , 23:09:02
2018-01-21 23:09:03.042  INFO 23832 --- [pool-2-thread-1] n.q.k.chaper11_8_1.task.ScheduledTasks   : 现在时间 , 23:09:03
2018-01-21 23:09:04.042  INFO 23832 --- [pool-2-thread-1] n.q.k.chaper11_8_1.task.ScheduledTasks   : 现在时间 , 23:09:04
2018-01-21 23:09:05.042  INFO 23832 --- [pool-2-thread-1] n.q.k.chaper11_8_1.task.ScheduledTasks   : 现在时间 , 23:09:05

@Scheduled详解

在上面的入门例子中,使用了@Scheduled(fixedRate = 1000) 注解来定义每过1秒执行的任务,对于@Scheduled的使用可以总结如下几种方式:

  • @Scheduled(fixedRate = 1000) :上一次开始执行时间点之后1秒再执行
  • @Scheduled(fixedDelay = 1000) :上一次执行完毕时间点之后1秒再执行
  • @Scheduled(initialDelay=1000, fixedRate=5000) :第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次
  • @Scheduled(cron="*/1 * * * * *") :通过cron表达式定义规则

@Scheduled 注解是单线程的,如果需要多线程,请增加@Async

更多Spring Boot 和 kotlin相关内容

欢迎关注《Spring Boot 与 kotlin 实战》

欢迎关注公众号**《全栈架构》**

Spring Boot 与 Kotlin 定时任务(Scheduling Tasks)

全栈有风险,菜鸟需谨慎

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Stella981 Stella981
2年前
SpringBoot学习笔记:计划任务
SpringBoot学习笔记:计划任务计划任务在企业的实践生产中,可能需要使用一些定时任务,如月末、季末和年末需要统计各种各样的报表,每周自动备份数据等。在Spring中使用定时任务1、加入@EnableScheduling注解,以启用定时任务机制@EnableS
Stella981 Stella981
2年前
HIVE 时间操作函数
日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec
Stella981 Stella981
2年前
Spring Boot @Scheduled 定时任务实战
作者:J'KYOwww.cnblogs.com/pejsidney/p/9046818.html假设我们已经搭建好了一个基于SpringBoot项目,首先我们要在Application中设置启用定时任务功能@EnableScheduling。启动定时任务packagecom.scheduling;import
Stella981 Stella981
2年前
Spring Boot整合Scheduled定时任务器、整合Quartz定时任务框架
首先说明一下,这里使用的是Springboot2.2.6.RELEASE版本,由于Springboot迭代很快,所以要注意版本问题。1、Scheduled定时任务器:是Spring3.0以后自带的一个定时任务器。1<?xmlversion"1.0"encoding"UTF8"?2<projectxmlns"h
Stella981 Stella981
2年前
Laravel 技巧之 定时任务
定时任务ScheduledTasks是Laravel提供的组件之一,稍微上点规模的项目应该都会用到,比如开发微信应用时通过定时任务去刷新accesstoken,比如每天定时发推送提现用户要记得签到。对于定时任务的基本用法,官网文档已经描述得很详细了,这里不再多说。本文主要是介绍定时任务在实际应用中的两个小技巧:1\.多个任务并行执行
Wesley13 Wesley13
2年前
4、定时任务关闭超时未支付的订单
//1.在主启动类上加上支持定时任务的注解@EnableSchedulingpublicclassApplication{//2.编写定时任务@ComponentpublicclassOrderJob{@Autowiredp
Easter79 Easter79
2年前
SpringBoot学习笔记:计划任务
SpringBoot学习笔记:计划任务计划任务在企业的实践生产中,可能需要使用一些定时任务,如月末、季末和年末需要统计各种各样的报表,每周自动备份数据等。在Spring中使用定时任务1、加入@EnableScheduling注解,以启用定时任务机制@EnableS
京东云开发者 京东云开发者
11个月前
定时任务原理方案综述 | 京东云技术团队
本文主要介绍目前存在的定时任务处理解决方案。业务系统中存在众多的任务需要定时或定期执行,并且针对不同的系统架构也需要提供不同的解决方案。京东内部也提供了众多定时任务中间件来支持,总结当前各种定时任务原理,从定时任务基础原理、单机定时任务(单线程、多线程)、分布式定时任务介绍目前主流的定时任务的基本原理组成、优缺点等。希望能帮助读者深入理解定时任务具体的算法和实现方案。
京东云开发者 京东云开发者
6个月前
Java服务总在半夜挂,背后的真相竟然是... | 京东云技术团队
最近有用户反馈测试环境Java服务总在凌晨00:00左右挂掉,用户反馈Java服务没有定时任务,也没有流量突增的情况,Jvm配置也合理,莫名其妙就挂了