原文: 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提供触发信息。 在哪里:
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 命名空间和模式位置定义。 还添加task:annotation-driven以启用基于注解的任务调度。
applicationConfig.xml
xmlns:task="http://www.springframework.org/schema/task"http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.0.xsd<task:annotation-driven>
上面的添加是必要的,因为我们将使用基于注解的配置。
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方法。
2. fixedDelay和fixedRate
在此方法中,fixedDelay属性与@Scheduled注解一起使用。 或者,也可以使用fixedRate。
示例类如下所示:
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 thispublic 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"?><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"xmlns:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"><task:annotation-driven /><bean id="demoServiceBasicUsageFixedDelay" class="com.howtodoinjava.service.DemoServiceBasicUsageFixedDelay"></bean></beans>
3. 使用 Cron 表达式的@scheduled
在此方法中,cron属性与@Scheduled注解一起使用。 此属性的值必须是 cron 表达式。
示例类如下所示:
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"?><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"xmlns:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"><task:annotation-driven /><bean id="demoServiceBasicUsageCron" class="com.howtodoinjava.service.DemoServiceBasicUsageCron"></bean></beans>
4. 属性文件中的 Cron 表达式
在此方法中,cron属性与@Scheduled注解一起使用。 此属性的值必须为 cron 表达式,如先前方法 BUT 一样,该 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
<?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"xmlns:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"><task:annotation-driven /><util:properties id="applicationProps" location="application.properties" /><context:property-placeholder properties-ref="applicationProps" /><bean id="demoServicePropertiesExample" class="com.howtodoinjava.service.DemoServicePropertiesExample"></bean></beans>
5. 上下文配置中的 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
<?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"xmlns:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"><task:annotation-driven /><util:properties id="applicationProps" location="application.properties" /><context:property-placeholder properties-ref="applicationProps" /><bean id="demoServiceXmlConfig" class="com.howtodoinjava.service.DemoServiceXmlConfig" /><task:scheduled-tasks><task:scheduled ref="demoServiceXmlConfig" method="demoServiceMethod" cron="#{applicationProps['cron.expression']}"></task:scheduled></task:scheduled-tasks></beans>
让我知道我是否遗漏任何东西。
学习愉快!
参考:
http://forum.springsource.org/showthread.php?83053-Feature-Scheduled-with-Value-cron-expression
