使用方法

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

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. echarts markLine 辅助线非直线设置

    效果图: 用例option: option = { title: { text: '未来一周气温变化', subtext: '纯属虚构' }, tooltip: { trigger: 'axis' } ...

  2. js放大镜特效

    在平时网上商城购物时,我们能够通过放大镜效果来使我们看图片能够更加的清楚,今天我就来给大家分享一下我学习的放大镜特效 下图是原图的样子                                 ...

  3. Redis持久化rdb&aof

    Redis持久化rdb&aof 前言 持久化:即把数据存储于断电后不会丢失的设备中,通常是硬盘 常见的持久化方式: 主从:通过从服务器保持持久化,如mongoDB的replication se ...

  4. 45. Jump Game II (JAVA)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. 008-kvm虚拟化管理平台WebVirtMgr部署-完整记录(1)

    公司机房有一台2U的服务器(64G内存,32核),由于近期新增业务比较多,测试机也要新增,服务器资源十分有限.所以打算在这台2U服务器上部署kvm虚拟化,虚出多台VM出来,以应对新的测试需求. 当KV ...

  6. centos .7x service iptables save 错误解决方案

    保存转发规则的时候,发现service iptables save 无效,而且报错[root@localhost bin]# service iptables saveThe service comm ...

  7. 【串线篇】spring boot启动配置原理

    几个重要的事件回调机制 配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunListener ...

  8. Python图谱

    Reference: https://time.geekbang.org/column/article/94311

  9. synchronized 与 lock 的区别

    synchronized 和 lock 的用法区别 synchronized(隐式锁):在需要同步的对象中加入此控制,synchronized 可以加在方法上,也可以加在特定代码块中,括号中表示需要锁 ...

  10. Golang在京东列表页实践总结

    Golang在京东列表页实践总结 作者:张洪涛 10余年软件开发和设计经验,曾就职于搜狐.搜狗.前matrixjoy公司联合创始人.甘普科技CTO. 目前线上状态 基于搜索实现: 全量数据,搜索结果不 ...