本文介绍spring的Scheduler定时任务

目录结构

config

// @EnableScheduling 开启后台任务
package com.springlearn.learn.config; import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling; @Configuration
@EnableScheduling
public class SchedulerConfig {
}

scheduler

// 获取当前时间

package com.springlearn.learn.scheduler;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class GetCurrentTimeSchedule {
private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss sss"); @Scheduled(initialDelay=3*1000, fixedDelay=1*1000)
public void GetCurrentTime() {
Date now = new Date();
System.out.println("Now is:" + df.format(now));
}
}

@Scheduled配置参数

cron
second, minute, hour, day of month, month, day(s) of week
@Scheduled(cron="0 * * * * *" ) 每分钟执行一次
@Scheduled(cron="*/10 * * * * *") 每10秒执行一次
@Scheduled(cron="*0 0 8-10 * * *") 8点,9点,10点各执行一次
@Scheduled(cron="0 0/30 8-10 * * *") 8点,8点半,9点,9点半,10点,10点半各执行一次
@Scheduled(cron="0 0 9-17 * * MON-FRI") 周一到周五的每天的9点到17点各执行一次
@Scheduled(cron="0 0 0 1 1 ?") 元旦节午夜执行一次
zone
时区
@Scheduled(cron="0 * * * * *", zone="Asia/Shanghai")
fixedDelay
当上一个任务完成,才会执行下一个
fixedDelayString
fixedRate
不管上一个任务完没完成,时间一到,都会执行下一个
fixedRateString
initialDelay
initialDelayString

springboot成神之——Scheduler定时任务的更多相关文章

  1. springboot成神之——ioc容器(依赖注入)

    springboot成神之--ioc容器(依赖注入) spring的ioc功能 文件目录结构 lang Chinese English GreetingService MyRepository MyC ...

  2. springboot成神之——springboot入门使用

    springboot创建webservice访问mysql(使用maven) 安装 起步 spring常用命令 spring常见注释 springboot入门级使用 配置你的pom.xml文件 配置文 ...

  3. springboot成神之——mybatis和mybatis-generator

    项目结构 依赖 generator配置文件 properties配置 生成文件 使用Example 本文讲解如何在spring-boot中使用mybatis和mybatis-generator自动生成 ...

  4. springboot成神之——swagger文档自动生成工具

    本文讲解如何在spring-boot中使用swagger文档自动生成工具 目录结构 说明 依赖 SwaggerConfig 开启api界面 JSR 303注释信息 Swagger核心注释 User T ...

  5. springboot成神之——log4j2的使用

    本文介绍如何在spring-boot中使用log4j2 说明 依赖 日志记录语句 log4j2配置文件 本文介绍如何在spring-boot中使用log4j2 说明 log4j2本身使用是非常简单的, ...

  6. springboot成神之——mybatis在spring-boot中使用的几种方式

    本文介绍mybatis在spring-boot中使用的几种方式 项目结构 依赖 WebConfig DemoApplication 方式一--@Select User DemoApplication ...

  7. springboot成神之——application.properties所有可用属性

    application.properties所有可用属性 # =================================================================== # ...

  8. springboot成神之——springboot+mybatis+mysql搭建项目简明demo

    springboot+mybatis+mysql搭建项目简明demo 项目所需目录结构 pom.xml文件配置 application.properties文件配置 MyApplication.jav ...

  9. springboot成神之——websocket发送和请求消息

    本文介绍如何使用websocket发送和请求消息 项目目录 依赖 DemoApplication MessageModel WebConfig WebSocketConfig HttpHandshak ...

随机推荐

  1. 【sparkSQL】DataFrame的常用操作

    scala> import org.apache.spark.sql.SparkSession import org.apache.spark.sql.SparkSession scala> ...

  2. 【sparkSQL】创建DataFrame及保存

    首先我们要创建SparkSession val spark = SparkSession.builder() .appName("test") .master("loca ...

  3. jQuery对select操作

    (转自:http://www.cnblogs.com/as285996985/articles/1535014.html) //遍历option和添加.移除optionfunction changeS ...

  4. javaScript-条件语句优化

    1.多重判断时使用 Array.includes test = (fruit: string) => { if (fruit == 'apple' || fruit == 'strawberry ...

  5. 原创:Scala学习笔记(不断更新)

    Scala是一种函数式语言和面向对象语言结合的新语言,本笔记中就零散记下学习scala的一些心得,主要侧重函数式编程方面. 1. 以递归为核心控制结构. 实现循环处理的方式有三种:goto,for/w ...

  6. js鼠标拖动(转自刘68)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Java并发编程总结

    基础概念 1.什么是原子操作?在Java Concurrency API中有哪些原子类(atomic classes)?原子操作(atomic operation)意为"不可被中断的一个或一 ...

  8. MySQL auto_increment介绍 以及 查询和修改auto_increment的方法

    一.auto_increment使用方法 .创建table时设置auto_increment属性和初始值100 create table nonove ( id bigint unsigned not ...

  9. typedeifn typename

    1.类型说明typedef 类型说明的格式为: typedef  类型 定义名; 类型说明只定义了一个数据类型的新名字而不是定义一种新的数据类型.定义名表示这个类型的新名字. 例如: 用下面语句定义整 ...

  10. 9.9 Python 文档字符串

    9.9 Python 文档字符串. 进入 Python 标准库所在的目录. 检查每个 .py 文件看是否有__doc__ 字符串, 如果有, 对其格式进行适当的整理归类. 你的程序执行完毕后, 应该会 ...