最常用的方法
@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 注解的 ----定时任务的更多相关文章

  1. springboot 基于@Scheduled注解 实现定时任务

    前言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...

  2. Spring 的@Scheduled注解实现定时任务运行和调度

    Spring 的@Scheduled注解实现定时任务运行和调度 首先要配置我们的spring.xml   ---  即spring的主配置文件(有的项目中叫做applicationContext.xm ...

  3. Spring Boot 使用 @Scheduled 注解创建定时任务

    在项目开发中我们经常需要一些定时任务来处理一些特殊的任务,比如定时检查订单的状态.定时同步数据等等. 在 Spring Boot 中使用 @Scheduled 注解创建定时任务非常简单,只需要两步操作 ...

  4. 基于@Scheduled注解的Spring定时任务

    1.创建spring-task.xml 在xml文件中加入命名空间 <beans xmlns="http://www.springframework.org/schema/beans& ...

  5. 使用轻量级Spring @Scheduled注解执行定时任务

    WEB项目中需要加入一个定时执行任务,可以使用Quartz来实现,由于项目就一个定时任务,所以想简单点,不用去配置那些Quartz的配置文件,所以就采用了Spring @Scheduled注解来实现了 ...

  6. Spring Boot入门(三):使用Scheduled注解实现定时任务

    在程序开发的过程中,经常会使用定时任务来实现一些功能,比如: 系统依赖于外部系统的非核心数据,可以定时同步 系统内部一些非核心数据的统计计算,可以定时计算 系统内部的一些接口,需要间隔几分钟或者几秒执 ...

  7. 使用spring提供的@Scheduled注解创建定时任务

    使用方法 操作非常简单,只要按如下几个步骤配置即可 1. 导入jar包或添加依赖,其实定时任务只需要spring-context即可,当然起服务还需要spring-web: 2. 编写定时任务类和方法 ...

  8. spring @Scheduled注解执行定时任务

    以前框架使用quartz框架执行定时调度问题. 这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用注解的 ...

  9. quartz 框架定时任务,使用spring @Scheduled注解执行定时任务

    配置quartz 在spring中需要三个jar包: quartz-1.6.5.jar.commons-collections-3.2.jar.commons-logging-1.1.jar 首先要配 ...

随机推荐

  1. 【洛谷P1919】A*B Problem升级版

    题目大意:rt 题解:将长度为 N 的大整数看作是一个 N-1 次的多项式,利用 FFT 计算多项式的卷积即可. 代码如下 #include <bits/stdc++.h> using n ...

  2. Kendo UI使用教程:CDN服务

    [Kendo UI最新试用版下载] Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support for React和 ...

  3. java模拟验证码生成

    设计思想 第一步:随机生成字符串 第二步:用户输入字符串 第三步:将两个字符串转化为同一类型 第四步:比较是否相同 第五步:输出结果 程序流程图 程序源代码 /*2017/10/7 王翌淞 验证码模拟 ...

  4. Redis数据类型之散列表

    Redis五大数据类型以及操作 目录: 一.redis的两种链接方式 二.redis的字符串操作(string) 三.redis的列表操作(list) 四.redis的散列表操作(类似于字典里面嵌套字 ...

  5. 第四篇:存储库之mongodb、redis、mysql

    MongoDB的简单操作 一.简介 二.MongoDB基础知识 三.安装 四.基本数据类型 五.增删改查操作 六.可视化工具 七.pymongo 一.简介 MongoDB是一款强大.灵活.且易于扩展的 ...

  6. iOS弹窗UIAlertAction用法

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认" message:@&quo ...

  7. VUE DIV模拟input框的基本处理

    关键代码 <div class="dialog-main" :contenteditable= "editable" v-text="notic ...

  8. 手动安装jar包到maven仓库

    1.手动安装jar包到maven仓库 本地没有下载安装maven,但是eclipse已经集成的maven. 选中任何一个maven项目,右键/Run as/maven build... 在Goals输 ...

  9. 箱排序(Bin Sort)

    1.基本思想 排序过程无须比较关键字,而是通过"分配"和"收集"过程来实现排序.它们的时间复杂度可达到线性阶:O(n). 箱排序也称桶排序(Bucket Sor ...

  10. 记一次创建svc代理失败

    在看尚硅谷的k8s视频中,学到ingress代理的时候,由于之前按照视频安装了V1.15.1,后面环境又出了问题,重新安装了 16.1的,为这次失败埋下了伏笔. 教案中的yaml apiVersion ...