使用方法

操作非常简单,只要按如下几个步骤配置即可

1. 导入jar包或添加依赖,其实定时任务只需要spring-context即可,当然起服务还需要spring-web;

2. 编写定时任务类和方法,在方法上加@Scheduled注解,注意定时方法不能有返回值;

3. 在spring容器中注册定时任务类;

4. 在spring配置文件中开启定时功能。

示例Demo

maven依赖

<dependency>
  <groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>

定时任务类

@Component(value = "scheduleJob")
public class ScheduleJob { /**
* 固定间隔时间执行任务,单位为微秒
*/
@Scheduled(fixedDelay = 2000)
public void timeJob() {
System.out.println("2s过去了......");
} /**
* 使用cron表达式设置执行时间
*/
@Scheduled(cron = "*/5 * * * * ?")
public void cronJob() {
System.out.println("cron表达式设置执行时间");
}
}

spring配置文件——注册定时任务类,单个注册或包扫描均可,这里使用包扫描,这时定时任务类必须加组件注解

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!--扫描指定包下加了组件注解的bean-->
<context:component-scan base-package="cn.monolog.diana.schedule" /> </beans>

spring配置文件——开启定时功能

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd"> <!--开启定时功能-->
<task:executor id="executor" pool-size="10" queue-capacity="128" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor" scheduler="scheduler" /> </beans>

启动服务后,控制台会输出如下语句

2s过去了......
2s过去了......
cron表达式设置执行时间
2s过去了......
2s过去了......
2s过去了......
cron表达式设置执行时间
2s过去了......
2s过去了......

指定定时任务执行时间的方式

从上面的demo可以看出,定时任务的执行时间主要有两种设置方式:

1. 使用@Scheduled注解的fixedDelay属性,只能指定固定间隔时长;

2. 使用@Scheduled注解的cron属性,可以指定更丰富的定时方式。

那么问题来了,这个cron表达式该怎么写?

记录在这篇博文中 https://www.cnblogs.com/dubhlinn/p/10740838.html

使用spring提供的@Scheduled注解创建定时任务的更多相关文章

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

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

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

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

  3. Spring Boot中@Scheduled注解的使用方法

    Spring Boot中@Scheduled注解的使用方法 一.定时任务注解为@Scheduled,使用方式举例如下 //定义一个按时间执行的定时任务,在每天16:00执行一次. @Scheduled ...

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

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

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

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

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

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

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

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

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

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

  9. 【转】使用spring @Scheduled注解执行定时任务

    http://blog.csdn.net/sd4000784/article/details/7745947 以前框架使用quartz框架执行定时调度问题. 老大说这配置太麻烦.每个调度都需要多加在s ...

随机推荐

  1. Paper Reading_Distributed System

    最近(以及预感接下来的一年)会读很多很多的paper......不如开个帖子记录一下读paper心得 Mark一个上海交通大学东岳网络工作室的paper notebook Mark一个大神的笔记 Ed ...

  2. UITextField 文本框 只能输入数字 且保留2位小数 实现

    http://blog.sina.com.cn/s/blog_aa7579f601015xvx.html #pragma mark - #pragma mark UITextField - (BOOL ...

  3. react搭建

    https://juejin.im/post/5b4de4496fb9a04fc226a7af

  4. PowerEdge T630服务器安装机器学习环境(Ubuntu18.04、Nvidia 1080Ti驱动、CUDA及CUDNN安装)

    安装步骤 在开始安装之前,我要说明一下,这个Ubuntu18.04系统的安装,使用的连接线(就是服务器和电脑显示器的连接线)必须两头都是VGA连接线,不能使用VGA转HDMI连接线,也不能用DVI转D ...

  5. 无锁版以时间为GUID的方法

    之前的博客 将时间作为GUID的方法 中,我使用了锁.我在实际的使用中,错将锁的释放放在了if语句中,这纯粹是我的失误,导致了很严重的错误.因此我在想是否有无锁的将时间作为GUID的方式,答案是使用I ...

  6. hdu 6214 : Smallest Minimum Cut 【网络流】

    题目链接 ISAP写法 #include <bits/stdc++.h> using namespace std; typedef long long LL; namespace Fast ...

  7. springboot创建拦截器

    一.创建一个MyIntercepor实现HandlerInterceptor接口的类             二.创建一个WebMvcConfig实现WebMvcConfigurer的类       ...

  8. 快速掌握Eclipse Plugin / RCP开发思想

    本文转载:https://my.oschina.net/drjones/blog/280337 引言 本文不是快速入门的文章,只面向有一定基础的开发人员,至少看这篇文章之前你应该了解什么是Eclips ...

  9. python-jsonpath、findall返回值提取

    findall import re """ "d"表示取数字0-9, "D"表示不要数字, "w"在正则里面代 ...

  10. js 将时间戳转为日期格式

    最近项目需要在前端将一个13位的时间戳显示成日期格式,在网上查了很多都不符合要求,只有一个是能满足要求的,在这记录一下,说不定以后还用的着. 13位时间戳改为yyyy-MM-dd HH-mm-ss 格 ...