Spring/SpringBoot实现定时任务的4种方式

洼地云 tuoyidashi.png

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

一、cron表达式规则定义

Crontab模式的表达式是六个以空格分隔的字段的列表:代表秒,分钟,小时,日,月,星期。月份和星期名称可以作为英文名称的前三个字母给出。

二、定时任务使用

1.在@Scheduled注解中使用fixedDelayfixedRate

两者区别:

  • fixedRate: 使Spring定期运行任务,即使最后一次调用可能仍在运行;
  • fixedDelay: 当最后一次执行完成时,专门控制下一次执行时间;
@Scheduled(fixedDelay =5000)
public void demoServiceMethod () {
    System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date());
}

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

2.在@Scheduled注解中使用cron表达式

cron是源自Unix cron实用程序的功能,根据您的要求有多种选择。

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

3.在properties文件中使用cron表达式

使用properties文件配置,可以使定时任务与源代码脱钩,因此更改定时任务更容易。

使用示例:

Java类

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.xsd
        http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd
        http://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>

4.在context配置中使用cron表达式

使用示例:

Java类

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.xsd
        http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd
        http://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>

参考文章:

  1. Spring @Scheduled – 4 Ways to Schedule Tasks
  2. Spring-Cron
  3. Spring-Scheduling Tasks Guides
赞(0)
未经允许禁止转载:优米格 » Spring/SpringBoot实现定时任务的4种方式

评论 抢沙发

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系

登录

找回密码

注册