基于 @Scheduled 注解的 ----定时任务
最常用的方法
@Scheduled 注解表示起开定时任务 依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
在启动类上添加 这个注解即可自动开启任务
@EnableScheduling//开启定时任务
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication
@EnableScheduling//开启定时任务
public class ScheduledApplication { public static void main(String[] args) {
SpringApplication.run(ScheduledApplication.class, args);
} }
任务类
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import java.util.Date; @Component
public class HelloComponent {
/**
* @Scheduled 注解表示起开定时任务
*
* fixedDelay 属性表示下一个任务在本次任务执行结束 2000 ms 后开始
* fixedRate 表示下一个任务在本次任务开始 2000ms 之后开始
* initialDelay 表示启动延迟时间
*/
@Scheduled(cron = "0/5 * * * * ?")
public void hello() {
System.out.println("hello:"+new Date());
}
}
可以 动态的添加 schedul 任务 在任务类的方法上 添加
@Scheduled(cron = "${jobs.schedule}")
在配置文件 application.properties文件中 中添加
jobs.schedule = 0 0/45 * * * ?
基于 @Scheduled 注解的 ----定时任务的更多相关文章
- springboot 基于@Scheduled注解 实现定时任务
前言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...
- Spring 的@Scheduled注解实现定时任务运行和调度
Spring 的@Scheduled注解实现定时任务运行和调度 首先要配置我们的spring.xml --- 即spring的主配置文件(有的项目中叫做applicationContext.xm ...
- Spring Boot 使用 @Scheduled 注解创建定时任务
在项目开发中我们经常需要一些定时任务来处理一些特殊的任务,比如定时检查订单的状态.定时同步数据等等. 在 Spring Boot 中使用 @Scheduled 注解创建定时任务非常简单,只需要两步操作 ...
- 基于@Scheduled注解的Spring定时任务
1.创建spring-task.xml 在xml文件中加入命名空间 <beans xmlns="http://www.springframework.org/schema/beans& ...
- 使用轻量级Spring @Scheduled注解执行定时任务
WEB项目中需要加入一个定时执行任务,可以使用Quartz来实现,由于项目就一个定时任务,所以想简单点,不用去配置那些Quartz的配置文件,所以就采用了Spring @Scheduled注解来实现了 ...
- Spring Boot入门(三):使用Scheduled注解实现定时任务
在程序开发的过程中,经常会使用定时任务来实现一些功能,比如: 系统依赖于外部系统的非核心数据,可以定时同步 系统内部一些非核心数据的统计计算,可以定时计算 系统内部的一些接口,需要间隔几分钟或者几秒执 ...
- 使用spring提供的@Scheduled注解创建定时任务
使用方法 操作非常简单,只要按如下几个步骤配置即可 1. 导入jar包或添加依赖,其实定时任务只需要spring-context即可,当然起服务还需要spring-web: 2. 编写定时任务类和方法 ...
- spring @Scheduled注解执行定时任务
以前框架使用quartz框架执行定时调度问题. 这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用注解的 ...
- quartz 框架定时任务,使用spring @Scheduled注解执行定时任务
配置quartz 在spring中需要三个jar包: quartz-1.6.5.jar.commons-collections-3.2.jar.commons-logging-1.1.jar 首先要配 ...
随机推荐
- web性能优化--算法优化(四)
避免for-in 把数组长度保存在局部变量中 较少迭代次数(Duffs Device) 基于函数的循环比基于循环的迭代消耗性能更多 优化if-else,一般switch比if-else速度快(hash ...
- CodeForces-437C(贪心)
链接: https://vjudge.net/problem/CodeForces-437C 题意: On Children's Day, the child got a toy from Delay ...
- VUEX报错 [vuex] Do not mutate vuex store state outside mutation handlers
数组 错误的写法:let listData= state.playList; // 数组深拷贝,VUEX就报错 正确的写法:let listDate= state.playList.slice(); ...
- GET和POST请求的区别和使用场景
本质上的区别: GET请求.处理.响应过程中只是产生一个TCP数据包,而POST请求会产生两个TCP数据包. 更具体地说,GET请求过程中头和请求正文数据一起到服务器端, 而POST请求过程中, ...
- 【知识】location.search获取中文时候会被编码成一串字符
[转码] 例如:case.html?id='这个是页面的标题' 当想要使用location.search获取?id='这个是页面的标题'的时候,包含的中文会被编码成一串字符串. 所以我们需要进行解码, ...
- Selenium 元素常用操作方法(键盘和鼠标事件)
一.简单操作 click():点击 send_keys():输入 clear():清空 submit():提交表单 size:返回元素的尺寸 text:获取元素的文本 get_attribute(): ...
- [BZOJ1902]:[NOIP2004]虫食算(搜索)
题目传送门 题目描述 所谓虫食算,就是原先的算式中有一部分被虫子啃掉了,需要我们根据剩下的数字来判定被啃掉的字母. 来看一个简单的例子: 43#98650#45+8468#6633=444455069 ...
- 在一个tomcat中配置多个tomcat服务器 111
<Service name="Tomcat_1"> <Connector port="8888" protocol="HTT ...
- python双划线类型
通过__开始的属性感觉分为两种类型: 利用dir调用的时候: < build in x object at 0x01F116B0>:这种貌似和运算符重载有关系,用户如果需要的话可以重写该函 ...
- LINK : fatal error LNK1104: 无法打开文件“qtmaind.lib”
LINK : fatal error LNK1104: 无法打开文件"qtmaind.lib" VS2019+QT5.13出现该问题,可以尝试这样解决. 1.找打qtmaind.l ...