最常用的方法
@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. Berlekamp-Massey algorithm

    https://www.cnblogs.com/zzqsblog/p/6877339.html https://blog.csdn.net/qq_39972971/article/details/80 ...

  2. 解决 i18n properties文件中文必须是unicode的问题

    解决 i18n properties文件中文必须是unicode的问题 i18n  unicode  UTF-8  目前产品需要做国际化,但 java 的 I18N 资源文件中中文必须转换成 unic ...

  3. 第六篇:Scrapy框架

    爬虫框架之Scrapy 一.介绍 二.安装 三.命令行工具 四.项目结构以及爬虫应用简介 五.Spiders 六.Selectors 七.Items 八.Item Pipelin 九. Dowload ...

  4. 【转】原生js实现移动端h5长按事件

    $("#target").on({ touchstart: function(e) { // 长按事件触发 timeOutEvent = setTimeout(function() ...

  5. percona 5.7 + tokudb

    percona 5.7 + tokudb Percona + TokuDB 部署 # 参考资料https://www.kancloud.cn/devops-centos/centos-linux-de ...

  6. JAVA笔记15-线程同步

    一.概念 1.多个线程之间访问同一资源,进行协调的过程是线程同步.例如两个人同时操作同一银行账户.解决方法:加锁 2.Java种引入对象互斥锁的概念,保证共享数据操作的完整性.每个对象都对应于一个可称 ...

  7. 【leetcode】638. Shopping Offers

    题目如下: In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, ther ...

  8. C#内存占用释放

    序言 系统启动起来以后,内存占用越来越大,使用析构函数.GC.Collect什么的也不见效果,后来查了好久,找到了个办法,就是使用 SetProcessWorkingSetSize函数.这个函数是Wi ...

  9. [POJ]P3126 Prime Path[BFS]

    [POJ]P3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35230   Accepted: ...

  10. OI多项式 简单学习笔记

    咕咕咕 先开个坑(其实是存模板来了) 一些特别简单的前置东西qwq 复数的计算 复数相加:向量相加,复数相乘.复数相乘:模长相乘,旋转量相加(就是复平面坐标轴逆时针旋转的角度) (当然也可以直接使用c ...