一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第4天,点击查看活动详情。

Spring 为任务调度和基于使用@Scheduled 注释的 cron 表达式的异步方法执行提供了极好的支持。可以将@Scheduled 注释与触发器元数据一起添加到方法中。在这篇文章中,我将以4种不同的方式展示@Scheduled 功能的使用方法。

一、Spring @Scheduled Annotation

@ scheduled注释用于任务调度。触发器信息需要与这个注释一起提供。

您可以使用属性 fixedDelay/fixedRate/cron 来提供触发信息。

  • fixedRate 使 Spring 定期运行任务,即使最后一次调用仍在运行
  • fixedDelay 特别控制最后一次执行结束时的下一次执行时间。
  • Cron 是一个源自 Unix cron 实用工具的特性,并且根据您的需求有各种选项。

示例用法如下:

``` @Scheduled Usages @Scheduled(fixedDelay =30000) public void demoServiceMethod () {... }

@Scheduled(fixedRate=30000) public void demoServiceMethod () {... }

@Scheduled(cron="0 0 * * * *") public void demoServiceMethod () {... } ```

1.2 如何启用@Scheduled 注释

要在 spring 应用程序中使用@Scheduled,必须首先在 applicationConfig.xml 文件中定义 xml 名称空间和模式位置定义。还添加任务: 注释驱动,以支持基于注释的任务调度。

``` applicationConfig.xml xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task/ http://www.springframework.org/schema/task/spring-task-3.0.xsd

```

上面的添加是必要的,因为我们将使用基于注释的配置。

1.3 使用@Scheduled 注释

下一步是在类中创建一个类和一个方法,如下所示:

DemoService.java public class DemoService {  @Scheduled(cron="*/5 * * * * ?")  public void demoServiceMethod() {    System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); } }

在上面的例子中

  • 使用@Scheduled 注释反过来会使 Spring 容器理解这个注释下面的方法将作为作业运行。
  • 记住,带@Scheduled 注释的方法不应该有传递给它们的参数。
  • 它们也不应该返回任何值
  • 如果希望在@Scheduled 方法中使用外部对象,应该使用自动连接将它们注入到 DemoService 类中,而不是将它们作为参数传递给@Scheduled 方法。

二、固定的延时和频率使用@Scheduled

在这个方法中,fixedDelay 属性与@Scheduled 注释一起使用。

举例:

``` DemoServiceBasicUsageFixedDelay.java package com.howtodoinjava.service;

import java.util.Date; import org.springframework.scheduling.annotation.Scheduled;

public class DemoServiceBasicUsageFixedDelay {  @Scheduled(fixedDelay = 5000)  //@Scheduled(fixedRate = 5000) //Or use this  public void demoServiceMethod() {    System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); } } ```

应用程序配置如下:

``` applicationContext.xml < ?xml  version="1.0" encoding="UTF-8"?>

```

三、配合cron表达式使用@Scheduled

在此方法中,cron 属性与@Scheduled 注释一起使用。

举例:

``` DemoServiceBasicUsageCron.java package com.howtodoinjava.service;

import java.util.Date; import org.springframework.scheduling.annotation.Scheduled;

public class DemoServiceBasicUsageCron {  @Scheduled(cron="*/5 * * * * ?")  public void demoServiceMethod() {    System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); } } ```

应用程序配置如下:

``` applicationContext.xml < ?xml  version="1.0" encoding="UTF-8"?>

```

四、使用properties文件配置Cron

在这个方法中,cron 属性与@Scheduled 注释一起使用。此属性的值必须是 cron 表达式,如前面的方法所示,但是,此 cron 表达式将在属性文件中定义,相关属性的键将用于@Scheduled 注释。

这将使 cron 表达式与源代码分离,从而使更改变得容易。

``` DemoServicePropertiesExample.java package com.howtodoinjava.service;

import java.util.Date; import org.springframework.scheduling.annotation.Scheduled;

public class DemoServicePropertiesExample {

@Scheduled(cron = "${cron.expression}")  public void demoServiceMethod() {    System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); }

} ```

应用程序配置如下:

``` applicationContext.xml

```

五、使用context配置Cron

该方法在属性文件中配置 cron 表达式,在配置文件中使用 cron 表达式的属性键配置作业调度。主要的变化是您不需要在任何方法上使用@Scheduled 注释。方法配置也是在应用程序配置文件中完成的。

举例:

``` DemoServiceXmlConfig.java package com.howtodoinjava.service;

import java.util.Date;

public class DemoServiceXmlConfig {  public void demoServiceMethod() {    System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); }

} ```

应用程序配置如下:

``` applicationContext.xml

```

Spring @Scheduled 使用详解相关推荐

  1. SpringBoot定时任务@Scheduled注解详解

    SpringBoot定时任务@Scheduled注解详解 项目开发中,经常会遇到定时任务的场景,Spring提供了@Scheduled注解,方便进行定时任务的开发 概述 要使用@Scheduled注解 ...

  2. Spring JdbcTemplate方法详解

    2019独角兽企业重金招聘Python工程师标准>>> Spring JdbcTemplate方法详解 标签: springhsqldbjava存储数据库相关sql 2012-07- ...

  3. Spring Boot 配置文件详解

    2019独角兽企业重金招聘Python工程师标准>>> 第二篇 : Spring Boot配置文件详解 文章首发于微信公众号<程序员果果> 地址:https://mp.w ...

  4. spring2.0和spring2.5及以上版本的jar包区别 spring jar 包详解

    spring jar 包详解 spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了 spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环 ...

  5. Spring的lazy-init详解

    Spring中lazy-init详解 ApplicationContext实现的默认行为就是在启动服务器时将所有singleton bean提前进行实例化 (也就是依赖注入).提前实例化意味着作为初始 ...

  6. spring boot配置文件详解

    spring boot配置文件详解 application.properties是spring-boot的核心配置文件,这个配置文件基本可以取代我们ssm或者ssh里面的所有的xml配置文件. 当我们 ...

  7. 一文搞定 Spring Data Redis 详解及实战

    转载自  一文搞定 Spring Data Redis 详解及实战 SDR - Spring Data Redis的简称. Spring Data Redis提供了从Spring应用程序轻松配置和访问 ...

  8. Spring Boot 单元测试详解+实战教程

    转载自   Spring Boot 单元测试详解+实战教程 Spring Boot 的测试类库 Spring Boot 提供了许多实用工具和注解来帮助测试应用程序,主要包括以下两个模块. spring ...

  9. Spring事务管理详解_基本原理_事务管理方式

    Spring事务管理详解_基本原理_事务管理方式 1. 事务的基本原理 Spring事务的本质其实就是数据库对事务的支持,使用JDBC的事务管理机制,就是利用java.sql.Connection对象 ...

最新文章

  1. 来看看企业如何拥抱混合云?
  2. python详细基础教程-Python基础教程,Python入门教程(非常详细)
  3. Pandas简明教程:一、Pandas简介与安装
  4. 文巾解题 1833. 雪糕的最大数量
  5. 论大型信息系统集成项目的人力资源管理
  6. 在 Android 上使用协程(二):Getting started
  7. jboss-5.1.0_JBoss AS 7.1.0.Final“ Thunder”发布-Java EE 6 Full Profile认证!
  8. html里面怎么ul加高度,div里面嵌套了ul,为什么div的高度小于ul高度
  9. [NOIP2010]关押罪犯
  10. sqlserver数据导入hdfs和hive的解决方案
  11. $科大讯飞开放平台——语音听写接口的使用
  12. 大学入学计算机基础考试考什么,大学入学考试计算机基础考试.docx
  13. Module ‘“xx.vue“‘ has no default export.Vetur(1192)
  14. 酸奶糖酸比的计算机控制,PAL-BX丨ACID F5 五种水果糖酸度计
  15. 【C语言学习】C语言入门
  16. photoshop制作白发教程:可爱女孩黑发变白发
  17. 记一次IOS与H5-SPA页面的交互经验
  18. 解决maven打jar包时不把依赖打包进去的问题
  19. Android 手势拦截的实现(简化水平、垂直手势操作的拦截处理)
  20. 六子棋(客家六子棋)

热门文章

  1. 一文详说idea创建自定义的xml文件,比如创建mybatisConfig.xml等
  2. 小程序 身份认证服务器,如何实现微信小程序与.net core应用服务端的无状态身份验证...
  3. 10 大黑客专用的 Linux 操作系统
  4. 抠图:基于单个原色通道
  5. 怎么解决视频时摄像头显示的画面显示绿色人影的问题
  6. spring 注解练习
  7. 手把手教你做一个电子胸牌
  8. 人工智能知识全面讲解:机器学习的类型
  9. SQL INSERT INTO的用法
  10. C/C++基础查漏补缺(一)----------寒假学习笔记(一)