spring Cloud 定時任務 @Scheduled

本文主要記錄:如何使用spring的@Scheduled註解實現定時做業,基於spring cloudjava

1)pom.xml 文件引入相關依賴、spring-maven插件web

<?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.vip.qa</groupId>
    <artifactId>springboot-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

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

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

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

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

2)定時任務類spring

@Component:類註冊成beanapache

@Scheduled:定時任務,可選固定時間、cron表達式等類型springboot

cron表達式 每位的意義:Seconds Minutes Hours DayofMonth Month DayofWeekmaven

package com.vip.qa.vop.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by danny.yao on 2017/10/19.
 */
@Component
public class ScheduledTasks {

    private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);
    private static final SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Scheduled(fixedRate = 5000)
    public void scheduledDemo(){
        logger.info("scheduled - fixedRate - print time every 5 seconds:{}", formate.format(new Date()) );
    }

    /**
     "0/5 * *  * * ?"   每5秒觸發
     "0 0 12 * * ?"    天天中午十二點觸發
     "0 15 10 ? * *"    天天早上10:15觸發
     "0 15 10 * * ?"    天天早上10:15觸發
     "0 15 10 * * ? *"    天天早上10:15觸發
     "0 15 10 * * ? 2005"    2005年的天天早上10:15觸發
     "0 * 14 * * ?"    天天從下午2點開始到2點59分每分鐘一次觸發
     "0 0/5 14 * * ?"    天天從下午2點開始到2:55分結束每5分鐘一次觸發
     "0 0/5 14,18 * * ?"    天天的下午2點至2:55和6點至6點55分兩個時間段內每5分鐘一次觸發
     "0 0-5 14 * * ?"    天天14:00至14:05每分鐘一次觸發
     "0 10,44 14 ? 3 WED"    三月的每週三的14:10和14:44觸發
     "0 15 10 ? * MON-FRI"    每一個周1、周2、周3、周4、週五的10:15觸發
     */
    @Scheduled(cron="0/10 * *  * * ?")
    public void scheduledCronDemo(){
        logger.info("scheduled - cron - print time every 10 seconds:{}", formate.format(new Date()) );
    }
}
View Code

3)應用程序入口類Applicationide

@SpringBootApplication:等於原來的 (默認屬性)@Configuration + @EnableAutoConfiguration + @ComponentScanspring-boot

@EnableScheduling:啓用定時任務ui

package com.vip.qa.vop;

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

/**
 * Created by danny.yao on 2017/10/19.
 */
@SpringBootApplication
@EnableScheduling
public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }
}
View Code

4)運行入口類,能夠看到結果spa

相關文章
相關標籤/搜索