SpringBoot定時(shí)任務(wù)

項(xiàng)目中很多時(shí)候會(huì)使用到定時(shí)任務(wù),這篇文章介紹一下springboot整合定時(shí)任務(wù)。

springboot整合定時(shí)任務(wù)其實(shí)就兩點(diǎn),
1.創(chuàng)建一個(gè)能被定時(shí)任務(wù)類,方法上加入@Scheduled注解
2.在啟動(dòng)類application上加入@EnableScheduling注解

代碼如下,pom文件我只加入了devtools,其實(shí)不加入也可以

<?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.lanxiaobai</groupId>
    <artifactId>springboot_timing</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot_timing</name>
    <description>springboot_timing</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.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</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    <!-- 使Spring Boot應(yīng)用支持熱部署,無需手動(dòng)重啟Spring Boot應(yīng)用。 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

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

</project>

application類代碼如下:

@SpringBootApplication
@EnableScheduling
public class SpringbootTimingApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootTimingApplication.class, args);
    }
}

定時(shí)任務(wù)類TestTimer

package com.lanxiaobai.timing.timer;

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

import java.util.Date;

/**
 * @author lanxiaobai
 * @Description
 * @project springboot_learn
 * @package com.lanxiaobai.timing.timer
 * @ClassName TestTimer
 * @email 18801417651@163.com
 * @date 2018/10/1
 */
@Component
public class TestTimer {
    @Scheduled(cron = "0/1 * * * * ?")//cron = "0/1 * * * * ?"  0/1表示一秒執(zhí)行一次
    private void test() {
        System.out.println("執(zhí)行定時(shí)任務(wù)的時(shí)間是:"+new Date());
    }
}

到這里啟動(dòng)項(xiàng)目,可以看到控制臺(tái)如下


需要注意的是@Scheduled(cron = “0/1 * * * * ?”)中cron的值根據(jù)自己實(shí)際需要去寫,如果需要可以去下面的網(wǎng)站去弄。
http://cron.qqe2.com/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容