1. 程式人生 > >spring Cloud 定時任務 @Scheduled

spring Cloud 定時任務 @Scheduled

spl 應用程序 int 結果 每分鐘 created log version -s

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

1)pom.xml 文件引入相關依賴、spring-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.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)定時任務類

@Component:類註冊成bean

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

技術分享
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"    每個周一、周二、周三、周四、周五的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)應用程序入口類Application

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

@EnableScheduling:啟用定時任務

技術分享
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)運行入口類,可以看到結果

技術分享

spring Cloud 定時任務 @Scheduled