spring 任务调度(定时任务)

spring 任务调度(定时任务)

本文将告诉你如何使用spring的任务调度。主要使用@Scheduled注解

文章需要会使用maven

第一步 pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.xxz</groupId>
    <artifactId>scheduled-task-test</artifactId>
    <version>1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

第二步 定时任务类(ScheduledTasks)

package org.xxz.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class ScheduledTasks {

    @Scheduled(fixedRate = 5000)
    public void now() {
        log.info("The time is now {}", new Date());
    }
}

@Scheduled有三种类型参数fixedRate, fixedDelay, cron
fixedRate 表示每隔多少毫秒执行一次
fixedDelay 表示任务执行完成后隔多少毫秒执行一次
cron 定时任务表达式

第三步 启动类(Application)

package org.xxz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

第四步 打包运行

cd scheduled-task-test
mvn clean package
java -jar target/scheduled-task-test-1.0.jar

扩展知识:如果不想使用spring的任务调度,可以使用jdk自带的任务调度类

ScheduledExecutorService#schedule
ScheduledExecutorService#scheduleAtFixedRate
ScheduledExecutorService#scheduleWithFixedDelay

今天的分享就到这里了。谢谢阅读。

原文地址:https://blog.uyiplus.com/2018/spring-scheduling-tasks/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,234评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,997评论 6 342
  • 博客原文 徒手翻译spring framework 4.2.3官方文档的第33章,若有翻译不当之处请指正。 定时任...
    rabbitGYK阅读 5,705评论 4 24
  • scheduler定时调度系统是大多行业项目都需要的,传统的spring-job模式,个人感觉已经out了,因为存...
    安琪拉_4b7e阅读 2,881评论 4 6
  • 江城,连绵阴雨,蓦然发现今年是我在这个城市度过的第六个冬天了,想起昨天是腊八,都说“过了腊八就是年”,不禁慨然岁月...
    很好的杨小阳阅读 424评论 4 5