Springboot中使用Scheduled做定时任务

摘要:
在开发中,定时任务是常见的功能,在springboot下开发定时任务其实很简单,具体代码如下:1、配置依赖包pom.xml由于默认的maven仓库经常访问不了,这里采用了阿里云的maven仓库镜像。--阿里云maven仓库--˃publicaliyunnexushttp://maven.aliyun.com/nexus/content/groups/public/truepublicaliyunnexushttp://maven.aliyun.com/nexus/content/groups/public/truefalseorg.springframework.bootspring-boot-starter-parent1.4.5.RELEASEUTF-8UTF-81.8org.springframework.bootspring-boot-starter-weborg.projectlomboklomboktrueorg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-maven-plugin2、定制任务场景定时任务实现,提供固定周期、固定周期延迟间隔和制定时间点执行等场景。采用@Scheduled注解进行标注。

在开发中,定时任务是常见的功能,在spring boot 下开发定时任务其实很简单,具体代码如下:

1、配置依赖包pom.xml

由于默认的maven仓库经常访问不了,这里采用了阿里云的maven仓库镜像。

<?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>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-scheduled</name>
    <description>Demo project for Spring Boot</description>

    <!--阿里云maven仓库 -->
    <repositories>
        <repository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.5.RELEASE</version>
        <relativePath /> <!--lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

2、定制任务场景

定时任务实现,提供固定周期、固定周期延迟间隔和制定时间点执行等场景。采用@Scheduled注解进行标注。

ExampleTimer.java

packagecom.example;

importjava.text.SimpleDateFormat;
importjava.util.Date;

importorg.springframework.scheduling.annotation.Scheduled;
importorg.springframework.stereotype.Component;

@Component
public classExampleTimer {
     SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

        @Scheduled(fixedRate = 10000)
        public voidtimerRate() {
            System.out.println(dateFormat.format(newDate()));
        }
        
        //第一次延迟1秒执行,当执行完后2秒再执行
        @Scheduled(initialDelay = 1000, fixedDelay = 2000)
        public voidtimerInit() {
            System.out.println("init : "+dateFormat.format(newDate()));
        }

        //每天20点16分50秒时执行
        @Scheduled(cron = "50 16 20 * * ?")
        public voidtimerCron() {
            System.out.println("current time : "+ dateFormat.format(newDate()));
        }
}

3、启动应用程序

启动程序,需要增加@EnableScheduling注解.

SpringBootScheduledApplication.java

packagecom.example;

importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
importorg.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public classSpringBootScheduledApplication {

    public static voidmain(String[] args) {
        SpringApplication.run(SpringBootScheduledApplication.class, args);
    }
}

4、输出结果

20:16:27
init : 20:16:28
init : 20:16:30
init : 20:16:32
init : 20:16:34
init : 20:16:36
20:16:37
init : 20:16:38
init : 20:16:40
init : 20:16:42
init : 20:16:44
init : 20:16:46
20:16:47
init : 20:16:48
current time : 20:16:50
init : 20:16:50
init : 20:16:52
init : 20:16:54

免责声明:文章转载自《Springboot中使用Scheduled做定时任务》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇25个Apache性能优化技巧推荐IOS 数据存储之 FMDB 详解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

fullcalendar日历控件知识点集合

除非对于极少的特殊需求,fullcalendar向我们提供的接口不足以满足,才会去改动fullcalendar本身的js文件。这些会议安排一般是保存在server的,在每次页面载入时,fullcalendar得到会议安排的集合,然后依照当中的日期去把事件描绘到日历相应的地方。...

ThinkPHP

ThinkPHP的开发模式是define//Debug mode define//当运行模式控制器的操作系统找不到请求的方法时,它将定位__Empty()方法处理。使用此机制,我们可以统一处理用户请求的所有不存在的操作。模块分组大A函数和大R函数有什么区别?关联数组易于操作,信息量相对较大...

Delete from join 用法

delete(别名)fromtblA(别名)leftjointblb(别名)on。。。...

当微信小程序遇到AR(二)

当微信小程序遇到AR,会擦出怎么样的火花?期待与激动......通过该教程,可以从基础开始打造一个微信小程序的AR框架,所有代码开源,提供大家学习。注册地址=˃注册成功之后,需要下载微信小程序开发工具。下载地址=˃目前笔者的开发环境是:Windows10下载的微信小程序版本为:RCv1.0.2.1909111 打开,微信开发者工具之后,会看到如下的页面。...

等保2.0四级安全要求

平等保护2.0四级安全要求四级安全保护能力:应能够在统一的安全战略下,防止恶意攻击、严重自然灾害和来自国家一级、敌对组织和资源丰富的威胁源的其他严重危害造成的资源损害。它应该能够及时检测和监控攻击和安全事件,所有功能都可以快速恢复。以下粗体字段是平等保护的第4级和第3级之间的差异,应予以更多注意。...

C# winform开发嵌套Chrome内核浏览器(WebKit.net)开发(一)

//Www.cnblogs.com/Maxq/p/6566558.htmlWebKit.net是WebKit的一个net包。使用它,。net程序可以非常方便地集成和使用webkit作为加载网页的容器。EventArgse){WebKit.WebKitBrowser=newWebKitBrowser();this.Controls.Add(浏览器);...