一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Springboot通過Scheduled實現(xiàn)定時任務(wù)代碼

Springboot通過Scheduled實現(xiàn)定時任務(wù)代碼

2021-02-22 11:05lirenqing Java教程

這篇文章主要介紹了Springboot通過Scheduled實現(xiàn)定時任務(wù)代碼,具有一定參考價值,需要的朋友可以了解下。

定時任務(wù)一般會存在中大型企業(yè)級項目中,為了減少服務(wù)器、數(shù)據(jù)庫的壓力往往會采用時間段性的去完成某些業(yè)務(wù)邏輯。比較常見的就是金融服務(wù)系統(tǒng)推送回調(diào),一般支付系統(tǒng)訂單在沒有收到成功的回調(diào)返回內(nèi)容時會持續(xù)性的回調(diào),這種回調(diào)一般都是定時任務(wù)來完成的。還有就是報表的生成,我們一般會在客戶訪問量過小的時候來完成這個操作,那往往都是在凌晨。這時我們也可以采用定時任務(wù)來完成邏輯。SpringBoot為我們內(nèi)置了定時任務(wù),我們只需要一個注解就可以開啟定時為我們所用了。

在開發(fā)中,定時任務(wù)是常見的功能,在spring boot 下開發(fā)定時任務(wù)其實很簡單,具體代碼如下:

1、配置依賴包pom.xml

由于默認的maven倉庫經(jīng)常訪問不了,這里采用了阿里云的maven倉庫鏡像。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?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、定制任務(wù)場景

定時任務(wù)實現(xiàn),提供固定周期、固定周期延遲間隔和制定時間點執(zhí)行等場景。采用@Scheduled注解進行標(biāo)注。

ExampleTimer.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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秒執(zhí)行,當(dāng)執(zhí)行完后2秒再執(zhí)行
    @Scheduled(initialDelay = 1000, fixedDelay = 2000)
        public void timerInit() {
        System.out.println("init : "+dateFormat.format(new Date()));
    }
    //每天20點16分50秒時執(zhí)行
    @Scheduled(cron = "50 16 20 * * ?")
        public void timerCron() {
        System.out.println("current time : "+ dateFormat.format(new Date()));
    }
}

3、啟動應(yīng)用程序

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

SpringBootScheduledApplication.java

?
1
2
3
4
5
6
7
8
9
10
11
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、輸出結(jié)果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

總結(jié)

以上就是本文關(guān)于Springboot通過Scheduled實現(xiàn)定時任務(wù)代碼的全部內(nèi)容,希望對大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

原文鏈接:https://www.cnblogs.com/lirenqing/p/6596557.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 草莓香蕉榴莲丝瓜秋葵绿巨人在线看 | www在线免费观看 | 日韩高清在线免费看 | 国产福利一区二区三区四区 | 双子母性本能在线观看 | 456在线观看| 香蕉视频在线观看网站 | 亚洲精品福利你懂 | 青草视频在线观看免费视频 | 视频一区二区三区欧美日韩 | 欧美va天堂va视频va在线 | 好爽好深好猛好舒服视频上 | 成人日批视频 | chinese男同志videos | 非洲特级特黄aa大片 | les在宿舍吃她奶 | 色婷婷综合缴情综六月 | 亚洲欧洲淘宝天堂日本 | 国产福利视频一区二区微拍视频 | 第一次破苞h | 婷婷丁香视频 | 日本sss在线高清观看 | 日本视频在线播放 | 天天色天 | 日韩激情视频在线观看 | 女人被爽到呻吟娇喘的视频动态图 | 日韩久久影院 | 国产视频自拍一区 | 高中生放荡日记高h娜娜 | 91制片厂 果冻传媒 天美传媒 | 特a级片| 我的家教老师 | 99在线免费观看视频 | 91传媒在线观看 | 国产拍拍视频一二三四区 | 国产大片线上免费观看 | 黑人好大| 亚飞与亚基国语1080p在线观看 | 小鸟酱喷水 | 成年女人免费 | 日韩欧美一区二区三区四区 |