原文: https://howtodoinjava.com/spring-core/spring-scheduled-annotation/

Spring 使用@Scheduled注解为基于 cron 表达式的任务调度和异步方法执行提供了出色的支持。 可以将@Scheduled注解与触发器元数据一起添加到方法中。

在本文中,我将展示以 4 种不同方式使用@Scheduled功能的方法。

阅读更多: Spring timer 任务

1. Spring @Scheduled注解

@Scheduled注解用于任务调度。 触发信息需要与此注解一起提供。

1.1. fixedDelay vs FixedRate vs cron

您可以使用属性fixedDelay / fixedRate / cron提供触发信息。 在哪里:

  1. fixedRate使 Spring 定期运行任务,即使最后一次调用可能仍在运行。
  2. fixedDelay特别控制最后一次执行完成时的下一个执行时间。
  3. cron是源自 Unix cron 实用程序的功能,并根据您的要求提供各种选项。

用法示例如下:

@Scheduled Usages

  1. @Scheduled(fixedDelay =30000)
  2. public void demoServiceMethod () {... }
  3. @Scheduled(fixedRate=30000)
  4. public void demoServiceMethod () {... }
  5. @Scheduled(cron="0 0 * * * *")
  6. public void demoServiceMethod () {... }

1.2. 如何启用@Scheduled注解

要在 spring 应用程序中使用@Scheduled,必须首先在applicationConfig.xml文件中定义 xml 命名空间和模式位置定义。 还添加task:annotation-driven以启用基于注解的任务调度。

applicationConfig.xml

  1. xmlns:task="http://www.springframework.org/schema/task"
  2. http://www.springframework.org/schema/task
  3. http://www.springframework.org/schema/task/spring-task-3.0.xsd
  4. <task:annotation-driven>

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

1.3. 使用@Scheduled注解

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

DemoService.java

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

在上述示例中:

  1. 反过来,使用@Scheduled注解将使 Spring 容器了解该注解下面的方法将作为作业运行。
  2. 请记住,用@Scheduled注解的方法不应将参数传递给它们。
  3. 他们也不应返回任何值。
  4. 如果希望在@Scheduled方法中使用外部对象,则应使用自动装配将它们注入DemoService类,而不是将它们作为参数传递给@Scheduled方法。

2. fixedDelayfixedRate

在此方法中,fixedDelay属性与@Scheduled注解一起使用。 或者,也可以使用fixedRate

示例类如下所示:

DemoServiceBasicUsageFixedDelay.java

  1. package com.howtodoinjava.service;
  2. import java.util.Date;
  3. import org.springframework.scheduling.annotation.Scheduled;
  4. public class DemoServiceBasicUsageFixedDelay
  5. {
  6. @Scheduled(fixedDelay = 5000)
  7. //@Scheduled(fixedRate = 5000) //Or use this
  8. public void demoServiceMethod()
  9. {
  10. System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date());
  11. }
  12. }

应用程序配置如下所示:

applicationContext.xml

  1. < ?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:task="http://www.springframework.org/schema/task"
  5. xmlns:util="http://www.springframework.org/schema/util"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd
  9. http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd
  10. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
  11. <task:annotation-driven />
  12. <bean id="demoServiceBasicUsageFixedDelay" class="com.howtodoinjava.service.DemoServiceBasicUsageFixedDelay"></bean>
  13. </beans>

3. 使用 Cron 表达式的@scheduled

在此方法中,cron属性与@Scheduled注解一起使用。 此属性的值必须是 cron 表达式。

示例类如下所示:

DemoServiceBasicUsageCron.java

  1. package com.howtodoinjava.service;
  2. import java.util.Date;
  3. import org.springframework.scheduling.annotation.Scheduled;
  4. public class DemoServiceBasicUsageCron
  5. {
  6. @Scheduled(cron="*/5 * * * * ?")
  7. public void demoServiceMethod()
  8. {
  9. System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date());
  10. }
  11. }

应用程序配置如下所示:

applicationContext.xml

  1. < ?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:task="http://www.springframework.org/schema/task"
  5. xmlns:util="http://www.springframework.org/schema/util"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd
  9. http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd
  10. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
  11. <task:annotation-driven />
  12. <bean id="demoServiceBasicUsageCron" class="com.howtodoinjava.service.DemoServiceBasicUsageCron"></bean>
  13. </beans>

4. 属性文件中的 Cron 表达式

在此方法中,cron属性与@Scheduled注解一起使用。 此属性的值必须为 cron 表达式,如先前方法 BUT 一样,该 cron 表达式将在属性文件中定义,并且相关属性的键将用于@Scheduled注解。

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

DemoServicePropertiesExample.java

  1. package com.howtodoinjava.service;
  2. import java.util.Date;
  3. import org.springframework.scheduling.annotation.Scheduled;
  4. public class DemoServicePropertiesExample {
  5. @Scheduled(cron = "${cron.expression}")
  6. public void demoServiceMethod()
  7. {
  8. System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date());
  9. }
  10. }

应用程序配置如下所示:

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:task="http://www.springframework.org/schema/task"
  5. xmlns:util="http://www.springframework.org/schema/util"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd
  9. http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd
  10. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
  11. <task:annotation-driven />
  12. <util:properties id="applicationProps" location="application.properties" />
  13. <context:property-placeholder properties-ref="applicationProps" />
  14. <bean id="demoServicePropertiesExample" class="com.howtodoinjava.service.DemoServicePropertiesExample"></bean>
  15. </beans>

5. 上下文配置中的 Cron 表达式

在这种方法中,在属性文件中配置了 cron 表达式,并使用属性密钥为 cron 表达式在配置文件中配置了作业调度。 主要变化是,您无需在任何方法上使用@Scheduled注解。 方法配置也在应用程序配置文件中完成。

示例类如下所示:

DemoServiceXmlConfig.java

  1. package com.howtodoinjava.service;
  2. import java.util.Date;
  3. public class DemoServiceXmlConfig
  4. {
  5. public void demoServiceMethod()
  6. {
  7. System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date());
  8. }
  9. }

应用程序配置如下所示:

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:task="http://www.springframework.org/schema/task"
  5. xmlns:util="http://www.springframework.org/schema/util"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd
  9. http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd
  10. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
  11. <task:annotation-driven />
  12. <util:properties id="applicationProps" location="application.properties" />
  13. <context:property-placeholder properties-ref="applicationProps" />
  14. <bean id="demoServiceXmlConfig" class="com.howtodoinjava.service.DemoServiceXmlConfig" />
  15. <task:scheduled-tasks>
  16. <task:scheduled ref="demoServiceXmlConfig" method="demoServiceMethod" cron="#{applicationProps['cron.expression']}"></task:scheduled>
  17. </task:scheduled-tasks>
  18. </beans>

下载源码

让我知道我是否遗漏任何东西。

学习愉快!

参考:

http://forum.springsource.org/showthread.php?83053-Feature-Scheduled-with-Value-cron-expression