首頁  >  文章  >  Java  >  Springboot如何透過Scheduled實現定時任務

Springboot如何透過Scheduled實現定時任務

WBOY
WBOY轉載
2023-05-16 18:10:141316瀏覽

定時任務一般會存在中大型企業級專案中,為了減少伺服器、資料庫的壓力往往會採用時間段性的去完成某些業務邏輯。比較常見的就是金融服務系統推送回調,一般支付系統訂單在沒有收到成功的回調回傳內容時會持續性的回調,這種回調一般都是定時任務來完成的。還有就是報表的生成,我們通常會在客戶訪問量過小的時候來完成這個操作,那往往都是在凌晨。這時我們也可以採用定時任務來完成邏輯。 SpringBoot為我們內建了定時任務,我們只需要一個註解就可以開啟定時為我們所用了。

在開發中,定時任務是常見的功能,在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

package com.example;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ExampleTimer {
	SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
	@Scheduled(fixedRate = 10000)
	    public void timerRate() {
		System.out.println(dateFormat.format(new Date()));
	}
	//第一次延迟1秒执行,当执行完后2秒再执行
	@Scheduled(initialDelay = 1000, fixedDelay = 2000)
	    public void timerInit() {
		System.out.println("init : "+dateFormat.format(new Date()));
	}
	//每天20点16分50秒时执行
	@Scheduled(cron = "50 16 20 * * ?")
	    public void timerCron() {
		System.out.println("current time : "+ dateFormat.format(new Date()));
	}
}

3、啟動應用程式

#啟動程序,需要增加@EnableScheduling註解.

SpringBootScheduledApplication.java

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class SpringBootScheduledApplication {
	public static void main(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實現定時任務的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除