spring boot 使用Schedule创建轻量级定时任务
Scheduled
SpringBoot配置定时任务可以直接使用自带的Scheduled,这相当于一个轻量级的Quartz,它可以让我们直接使用注解来完成定时任务的配置。
Scheduled调度时间设置说明
使用Scheduled设置任务调度时间有以下几种方式,可根据实际情况选取一种即可:
//通过fixedRate属性,来设置上一次调度开始后再次调度的延时,fixedRate值的单位为毫秒,此例为每5秒执行1次
//注意:fixedRate只要到达间隔时间就会调度执行,不关心上次任务是否已执行完成,这样就会存在重复执行的风险!
@Scheduled(fixedRate = 5000) //该属性的功效与上面的fixedRate则是相反的,配置了该属性后会等到任务方法执行完成后,达到延迟配置的时间再次执行该方法
@Scheduled(fixedDelay = 5000) //通过cron属性,使用Cron表达式来设置执行时间,此例为每10秒执行1次
//在线Cron表达式生成器:http://cron.qqe2.com/
@Scheduled(cron="*/10 * * * * ?")
别外,可以通过initialDelay属性来设置第一次执行的延迟时间,只是做延迟的设定,并不会控制其他逻辑,需要配合fixedDelay或者fixedRate来使用
//第一次延迟10秒执行,之后每间隔5秒执行一次
@Scheduled(fixedDelay = 5000, initialDelay = 10000)
调度任务(定时任务)实现类
调度任务1
package Scheduler; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date; @Service
public class SchedulingTask1 { private Integer count=0;
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //通过fixedRate属性,来设置上一次调度开始后再次调度的延时,fixedRate值的单位为毫秒,此例为每5秒执行1次
@Scheduled(fixedRate = 5000)
private void process(){
//输出
System.out.println(String.format("第%s次执行任务SchedulingTask1 时间:%s", (++count).toString(), dateFormat.format(new Date())));
}
}
调度任务2
package Scheduler; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date; @Service
public class SchedulingTask2 { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //通过cron属性,使用Cron表达式来设置执行时间,此例为每10秒执行1次
@Scheduled(cron="*/10 * * * * ?")
private void process(){
//输出
System.out.println(String.format("SchedulingTask2执行... 时间:%s", dateFormat.format(new Date())));
}
}
也可以将SchedulingTask2类中的方法写在SchedulingTask1类中。
调度配置类
@ComponentScan注解的作用就是根据定义的扫描路径,把符合扫描规则的类装配到spring容器中
@EnableScheduling注解开启对计划任务的支持
package Scheduler; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling; @Configuration
@ComponentScan("Scheduler")
@EnableScheduling //通过@EnableScheduling注解开启对计划任务的支持
public class SchedulerConfig {
}
启动类
使用AnnotationConfigApplicationContext可以实现基于Java的配置类(包括各种注解)加载Spring的应用上下文。避免使用application.xml进行配置。相比XML配置,更加便捷。
package com.lgt.demo2; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import Scheduler.SchedulerConfig; @SpringBootApplication
public class Demo2Application { public static void main(String[] args) {
SpringApplication.run(Demo2Application.class, args);
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(SchedulerConfig.class);
} }
输出结果
第1次执行任务SchedulingTask1 时间:2019-04-15 13:41:33
第2次执行任务SchedulingTask1 时间:2019-04-15 13:41:38
SchedulingTask2执行... 时间:2019-04-15 13:41:40
第3次执行任务SchedulingTask1 时间:2019-04-15 13:41:43
第4次执行任务SchedulingTask1 时间:2019-04-15 13:41:48
SchedulingTask2执行... 时间:2019-04-15 13:41:50
第5次执行任务SchedulingTask1 时间:2019-04-15 13:41:53
第6次执行任务SchedulingTask1 时间:2019-04-15 13:41:58
SchedulingTask2执行... 时间:2019-04-15 13:42:00
spring boot 使用Schedule创建轻量级定时任务的更多相关文章
- [转] 使用Spring Boot和Gradle创建项目
Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的 ...
- 使用Spring Boot和Gradle创建AngularJS项目
Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的 ...
- [转]通过Spring Boot三分钟创建Spring Web项目
来源:https://www.tianmaying.com/tutorial/project-based-on-spring-boot Spring Boot简介 接下来我们所有的Spring代码实例 ...
- Spring Boot的快速创建
一.利用向导快速搭建Spring Boot应用 创建一个controller package com.hoje.springboot.Controller; import org.springfram ...
- spring boot 2X中@Scheduled实现定时任务及多线程配置
使用@Scheduled 可以很容易实现定时任务 spring boot的版本 2.1.6.RELEASE package com.abc.demo.common; import org.slf4j. ...
- Spring Boot TImer Schedule Quartz
Spring Boot 2.X(十二):定时任务-云栖社区-阿里云https://yq.aliyun.com/articles/723876?spm=a2c4e.11155472.0.0.2f8b3a ...
- spring boot 之 如何创建spring boot项目
创建spring boot的方式有非常多,今天我们使用maven来进行创建spring boot项目,因为maven使用的非常广泛,也很好用,很多IDE也都支持maven. 1 创建maven项目 1 ...
- spring boot 项目的创建
一. 进入https://start.spring.io 快速创建项目 二. 利用eclipse sts插件创建项目 1. 安装sts插件 进入https://spring.io/tools3/sts ...
- idea 社区版+spring boot+ssm+swagger创建rest api
新手上路,出了好多错,记录一下 1.创建数据库:springBootSsmTest 2.打开IDEA创建 Spring boot项目:File——New——Project——Spring Assist ...
随机推荐
- 目标检测之积分图---integral image 积分图2
前面在图像处理一栏中涉及到boxfilter 的时候,简单介绍过积分图,就是每个像素点是左边和上边的累加和,这样的话可以方便均值和方差,以及直方图统计的相关运算,这里再次结合网络资源重新单独对积分图做 ...
- javascript JS递归遍历对象 使用for(variable in object)或者叫for/in和forEach方式
1.递归遍历查找特定key值(ie9以下不支持forEach) 原文http://www.cnblogs.com/ae6623/p/5938560.html var obj = { first: &q ...
- A Short Introduction to Boosting
http://www.site.uottawa.ca/~stan/csi5387/boost-tut-ppr.pdf Boosting is a general method for improvin ...
- AndroidTest.java
以下代码使用ApiDemos-debug.apk进行测试 package com.saucelabs.appium; import io.appium.java_client.AppiumDriver ...
- office web apps的搭建部署(1)(写于2017.12.27)
因为业务方面的需求,项目要求搭建office-web-apps这个玩意儿,做一个在线预览编辑的功能,为了方便,我下面都用OWA代替这个服务. 首先说一下什么是office-web-apps-serve ...
- 给第三方apk进行系统签名的几种方式【转】
本文转载自:http://blog.csdn.net/luzhenrong45/article/details/47733053 版权声明:本文为博主原创文章,未经博主允许不得转载. -------- ...
- 数据结构之 线性表---单链表操作A (删除链表中的指定元素)
数据结构上机测试2-1:单链表操作A Time Limit: 1000MS Memory limit: 4096K 题目描述 输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据 ...
- Gym - 100187A A - Potion of Immortality —— 贪心
题目链接:http://codeforces.com/gym/100187/problem/A 题解: 光题意就想了很久:在最坏情况下的最小兔子数.其实就是至少用几只兔子就一定能找出仙药(答案存在的话 ...
- oracle查看锁表进程,杀掉锁表进程
查看锁表进程SQL语句1: select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, ao ...
- BestCoder3 1001 Task schedule(hdu 4907) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4907 题目意思:给出工作表上的 n 个任务,第 i 个任务需要 ti 这么长的时间(持续时间是ti ~ ...