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=

随便看看

使用nebula把联想个人云存储映射到当前网络使用的方法

整个过程涉及三个主机。一个是家中台式电脑的家庭内网ip为192.168.69.101,另一个是公司的笔记本电脑mac,另一是具有公共ip的服务器。云存储在家中,家在同一个intranet中,IP地址为192.168.69.100。最终的效果是,我可以在mac上直接访问云存储192.168.69.100的smb服务,通过nat渗透,同一个城市可以达到星云下载地...

Java Android 二进制文件读写

=-1){out.write;}返回。toByteArray();}1.2分段读取二进制文件,一次读取1024字节,byte[]buffer=newbyte[1024];PrivatevoidreadLocalFile()throwsIOException{StringfileName=“abcd.raw”;InputStreaminputStream=ge...

禅道从windows迁移到linux

windows下图片路径/zentao/www/data/upload/1备份到Linux下路径/opt/zbox/app/zentao/www/data/upload/1二、Linux下安装禅道注意一定要安装相同版本的禅道2.1、安装禅道有很多方法,禅道官网也有详细说明,这里主要讲linux用一键安装包及遇到的问题2.2、下载安装包禅道官网下载界面很乱,大...

Jdk升级到11引起的问题:程序包javax.xml.bind.annotation不存在

您可以看到ELDict类中有一个引用:importjavax。xml。绑定注释XmlAttribute;虽然未使用,但它会导致mvn编译错误。在在线绑定中搜索“包javax.xml.bind.nannotation不存在”。结果是:包javax。xml。bind Annotation不存在-CSDN论坛2009年12月2日·无法编译使用jaxb的类,因为软件...

用arduino做一个智能垃圾桶

这些天我几乎很忙。我有一些时间继续打扰我的arduino。上一次我从TB购买arduino套件时,有一个人体热能感应模块,用于感应人体接近信号。今天我们用这个做一个简单的智能垃圾桶。要实现的功能是:当有人靠近时,垃圾可以自动打开盖子,当人离开时,盖子可以自动关闭。1、 所需材料和工具:1 Arduino SCM我使用Arduino Nano 2人体热能传感模...

layui使用layui-excel扩展导出xlsx格式文件

layui-excel扩展导出的文件可用office打开,正常显示;直接用table带的导出功能,导出的文件用office打开显示乱码。--导出表不展示--˃78910layui.config.use(['table','form','laydate','excel'],function(){11varform=layui.form;12vartable=l...