欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年11月27日 11:47:46   作者:lirenqing  
這篇文章主要介紹了Springboot通過Scheduled實(shí)現(xiàn)定時(shí)任務(wù)代碼,具有一定參考價(jià)值,需要的朋友可以了解下。

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

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

1、配置依賴包pom.xml

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

<?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倉(cāng)庫(kù) -->
  <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ù)場(chǎng)景

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

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秒執(zhí)行,當(dāng)執(zhí)行完后2秒再執(zhí)行
	@Scheduled(initialDelay = 1000, fixedDelay = 2000)
	    public void timerInit() {
		System.out.println("init : "+dateFormat.format(new Date()));
	}
	//每天20點(diǎn)16分50秒時(shí)執(zhí)行
	@Scheduled(cron = "50 16 20 * * ?")
	    public void timerCron() {
		System.out.println("current time : "+ dateFormat.format(new Date()));
	}
}

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

啟動(dòng)程序,需要增加@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、輸出結(jié)果

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實(shí)現(xiàn)定時(shí)任務(wù)代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:

Spring boot跨域設(shè)置實(shí)例詳解

快速了解Spring Boot

淺談Springboot之于Spring的優(yōu)勢(shì)

如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

  • 深入理解Java三大特性中的多態(tài)

    深入理解Java三大特性中的多態(tài)

    多態(tài)性是對(duì)象多種表現(xiàn)形式的體現(xiàn)。在面向?qū)ο笾?,最常見的多態(tài)發(fā)生在使用父類的引用來引用子類的對(duì)象。下面這篇文章主要給大家深入的介紹了Java三大特性中多態(tài)的相關(guān)資料,有需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • Java?Web應(yīng)用小案例之實(shí)現(xiàn)用戶登錄功能全過程

    Java?Web應(yīng)用小案例之實(shí)現(xiàn)用戶登錄功能全過程

    在Java開發(fā)過程中實(shí)現(xiàn)用戶的注冊(cè)功能是最基本的,這篇文章主要給大家介紹了關(guān)于Java?Web應(yīng)用小案例之實(shí)現(xiàn)用戶登錄功能的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • 深入探討JAVA中的異常與錯(cuò)誤處理

    深入探討JAVA中的異常與錯(cuò)誤處理

    這篇文章詳細(xì)介紹了JAVA中的異常與錯(cuò)誤處理,有需要的朋友可以參考一下
    2013-09-09
  • Mybatis深度整合Mysql的Json字段問題

    Mybatis深度整合Mysql的Json字段問題

    這篇文章主要介紹了Mybatis深度整合Mysql的Json字段問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java如何基于反射獲取對(duì)象屬性信息

    Java如何基于反射獲取對(duì)象屬性信息

    這篇文章主要介紹了Java如何基于反射獲取對(duì)象屬性信息,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 基于Socket類以及ServerSocket類的實(shí)例講解

    基于Socket類以及ServerSocket類的實(shí)例講解

    下面小編就為大家?guī)硪黄赟ocket類以及ServerSocket類的實(shí)例講解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • 關(guān)于@ApiImplicitParams、ApiImplicitParam的使用說明

    關(guān)于@ApiImplicitParams、ApiImplicitParam的使用說明

    這篇文章主要介紹了關(guān)于@ApiImplicitParams、ApiImplicitParam的使用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • mybatis-plus 如何判斷參數(shù)是否為空并作為查詢條件

    mybatis-plus 如何判斷參數(shù)是否為空并作為查詢條件

    這篇文章主要介紹了mybatis-plus 如何判斷參數(shù)是否為空并作為查詢條件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.Transformer異常

    Caused by: java.lang.ClassNotFoundException: org.apache.comm

    這篇文章主要介紹了Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type異常,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • JAVA“無法驗(yàn)證證書。將不執(zhí)行該應(yīng)用程序?!碧崾窘鉀Q辦法

    JAVA“無法驗(yàn)證證書。將不執(zhí)行該應(yīng)用程序。”提示解決辦法

    這篇文章主要給大家介紹了關(guān)于JAVA“無法驗(yàn)證證書,將不執(zhí)行該應(yīng)用程序”提示的解決辦法,要解決Java無法驗(yàn)證證書的問題,可以嘗試下本文的方法,需要的朋友可以參考下
    2024-03-03

最新評(píng)論