Spring Boot任務相關知識點

2021-10-06 22:48:50 字數 2009 閱讀 3117

非同步任務定時任務郵件任務
1、非同步任務

使用場景:

在執行業務方法的時候,為了返回的相應速度,就會用到非同步處理,啟用多執行緒的方式去跑業務中的任務

配置非同步任務的步驟:

第一步:需要在springboot的啟動類中開啟非同步的註解 ------@enableasync

第二步:需要在我們非同步處理業務的方法上新增上@async註解

例項:

/************spring boot的啟動入口類*************/

@enableasync //開啟非同步註解

public static void main(string arg)

}/************需要非同步呼叫的業務方法*************/

@service

public class asynservice catch (interruptedexception e)

system.out.println("處理資料中。。。");

}}/************實際業務呼叫方法*************/

@restcontroller

public class hellocontroller

}

2、定時任務

使用場景:

專案開發中經常需要執行一些定時任務,比如需要在每天凌晨的時候,分析一次前一天的日誌資訊。

配置定時任務的步驟:

第一步:需要在springboot啟動了上加上@enablescheduling //開啟基於註解的定時任務。

第二步:在需要跑定時任務的方式上加@scheduled(cron = "0 * * * * mon-fri") //代表周一到周五每整一分鐘執行一次。

例項:

/*****spring boot的啟動類*****/

@enableasync //開啟非同步註解

@enablescheduling //開啟基於註解的定時任務

public static void main(string arg)

}/************需要跑定時任務的業務方法****************/

@service

public class scheduledservice

}

cron表示式規則:

3、郵件任務

第一步:匯入jar包

org.springframework.boot

spring-boot-starter-mail

第二步:傳送者郵件配置

#郵件傳送相關配置資訊

spring.mail.username=

spring.mail.password=

spring.mail.properties.mail.smtp.ssl.enable=true

第三步:編寫業務**

@service

public class mailsendservice

/*** 複雜郵件傳送

*/public void test02() throws exception

}

springboot郵件任務

新增依賴 org.springframework.boot spring boot starter mail 設定郵箱主機 spring.mail.host smtp.qq.com 設定使用者名稱 spring.mail.username qq.com 設定密碼,該處的密碼是qq郵箱開啟smtp的授...

Spring boot自動任務

因為專案需要,需要設定自動任務來監測資料庫工單資訊,然後改變其狀態 在啟動類中加入開啟定時任務的註解 enablescheduling com.verymro.eam.dao scanbasepackages enablescheduling enableswagger2 public class ...

Spring Boot 非同步任務

可以使用非同步的方式載入方法 只需在專案入口上定義 enableasync 開啟非同步註解功能 以及某個需要非同步方法上面定義 async註解 即可1.專案入口上定義 enableasync 開啟非同步註解功能 enableasync 開啟非同步註解功能 public class 2.使用 asyn...