基于 @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 首先要配 ...
随机推荐
- feign 发送请求时,传多个参数时的写法
第一:传参方式写法,当参数个数大于2个时,需要用@RequestParam @PostMapping(value = "/configReader/configValue.do", ...
- c语言日志打印
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <stdarg.h> ...
- 关于mysql备份与恢复的操作
逻辑备份:将数据库的数据以逻辑的SQL语句的方式导出 mysqldump备份数据库 备份数据并带创建数据库 (含数据) mysqldump -uroot -p'123456' -B dbname &g ...
- 转发(forward)和重定向(redirect)的区别?
1)forward是容器中控制权的转向,是服务器请求资源,服务器直接访问目标地址的URL,把那个URL 的响应内容读取过来,然后把这些内容再发给浏览器,浏览器根本不知道服务器发送的内容是从哪儿来的,所 ...
- Linux shell 下简单的进度条实现
Linux shell 下简单的进度条实现 [root@db145 ~]# cat print_process.sh function Proceess(){ spa='' i= ] do print ...
- 【leetcode】Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
- js 获取select的值
var t = document.getElementById("provid"); console.log(t.value); console.log(t.text); //未定 ...
- csp-s2019 AFO记
DAY 0 上午出发前大家都很颓废的样子. 我因为还没有实现刷完NOIP专题的所有题的目标而去憨比的学DDP. 最后还是不会,保卫王国是写不成了…… 该走了,学校领导来开了个欢送会,祝福我们从里WA到 ...
- shell变量与运算
shell变量与运算 @(0003 shell编程) 变量存在于内存中.假设变量str,设置或修改变量属性时,不带$号,只有引用变量的值时才使用$号.也就是说在内存中,标记变量的变量名称是str,而不 ...
- sqli-lab(15)
要考四级了 翻译过来就是 基于时间的单引号盲注 0X01盲注 的了解 https://www.cnblogs.com/ldhbetter/p/9201840.html 这里写的清清楚楚 A 先拆解长度 ...