美文网首页Spring Boot学习
SpringBoot定时任务

SpringBoot定时任务

作者: JokerJin | 来源:发表于2018-10-01 14:07 被阅读0次

    项目中很多时候会使用到定时任务,这篇文章介绍一下springboot整合定时任务。

    springboot整合定时任务其实就两点,
    1.创建一个能被定时任务类,方法上加入@Scheduled注解
    2.在启动类application上加入@EnableScheduling注解

    代码如下,pom文件我只加入了devtools,其实不加入也可以

    <?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应用支持热部署,无需手动重启Spring Boot应用。 -->
            <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);
        }
    }
    

    定时任务类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表示一秒执行一次
        private void test() {
            System.out.println("执行定时任务的时间是:"+new Date());
        }
    }
    

    到这里启动项目,可以看到控制台如下


    需要注意的是@Scheduled(cron = “0/1 * * * * ?”)中cron的值根据自己实际需要去写,如果需要可以去下面的网站去弄。
    http://cron.qqe2.com/

    相关文章

      网友评论

        本文标题:SpringBoot定时任务

        本文链接:https://www.haomeiwen.com/subject/rpfnoftx.html