詳解Springboot中的異步、定時(shí)、郵件任務(wù)
一、異步任務(wù)
1、編寫一個(gè)類AsyncService
異步處理還是非常常用的,比如我們?cè)诰W(wǎng)站上發(fā)送郵件,后臺(tái)會(huì)去發(fā)送郵件,此時(shí)前臺(tái)會(huì)造成響應(yīng)不動(dòng),直到郵件發(fā)送完畢,響應(yīng)才會(huì)成功,所以我們一般會(huì)采用多線程的方式去處理這些任務(wù)。
package com.rk.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
public void hello(){
try {
System.out.println("數(shù)據(jù)處理中~");
Thread.sleep(3000);//停止三秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
2、編寫一個(gè)AsyncController類
package com.rk.controller;
import com.rk.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@GetMapping("/hello")
public String hello(){
asyncService.hello();
return "success";
}
}
現(xiàn)在啟動(dòng)項(xiàng)目進(jìn)行測(cè)試,三秒后才會(huì)出現(xiàn)success,現(xiàn)在還不是異步
3、開啟異步
@Async//告訴spring這是一個(gè)異步方法
public void hello(){
try {
System.out.println("數(shù)據(jù)處理中~");
Thread.sleep(3000);//停止三秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@EnableAsync//開啟異步注解功能
@SpringBootApplication
public class Springboot09TestApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot09TestApplication.class, args);
}
}
二、郵件任務(wù)
1、引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2、配置mail
#用戶名 spring.mail.username=1624603357@qq.com #密碼 spring.mail.password=yblyxhvmnsurbbci #發(fā)送郵件服務(wù)器 spring.mail.host=smtp.qq.com #開啟加密驗(yàn)證 ssl spring.mail.properties.mail.smtp.ssl.enable=true
3、測(cè)試
簡(jiǎn)單郵件
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("你好,rk");//郵件標(biāo)題
mailMessage.setText("測(cè)試郵件");//郵件內(nèi)柔
mailMessage.setTo("r1624603357@126.com");//收件人郵箱
mailMessage.setFrom("1624603357@qq.com");//發(fā)件人郵箱
mailSender.send(mailMessage);
}

復(fù)雜郵件
@Test
void contextLoads2() throws MessagingException {
//一個(gè)復(fù)雜的郵件
MimeMessage mimeMessage = mailSender.createMimeMessage();
//組裝
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//正文
helper.setSubject("你好,rk");
helper.setText("<h1 style='color:red'>測(cè)試郵件</h1>",true);
//附件
helper.addAttachment("1.png",new File("D:\\QLDownloadGame\\2\\1.png"));
helper.addAttachment("rk.docx",new File("E:\\桌面\\rk.docx"));
// 發(fā)/收件人
helper.setTo("r1624603357@126.com");
helper.setFrom("1624603357@qq.com");
//發(fā)送
mailSender.send(mimeMessage);
}

三、定時(shí)任務(wù)
1、編寫一個(gè)ScheduledService類
@Service
public class ScheduledService {
//秒 分 時(shí) 日 月 周幾
//0 * * * * MON-FRI
//注意cron表達(dá)式的用法; 每天20:28 0秒執(zhí)行該方法
@Scheduled(cron = "0 28 20 * * 0-7")
public void hello(){
System.out.println("現(xiàn)在是20:28");
System.out.println("hello.....");
}
}
項(xiàng)目啟動(dòng)后每天20:28:00執(zhí)行hello方法
2、添加注解
@EnableAsync//開啟異步注解功能
@EnableScheduling//開啟定時(shí)功能注解
@SpringBootApplication
public class Springboot09TestApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot09TestApplication.class, args);
}
}
cron表達(dá)式練習(xí)
/*
【0 0/5 14,18 * * ?】每天14點(diǎn)整和18點(diǎn)整,每隔5分鐘執(zhí)行一次
【0 15 10 ? * 1-6】每個(gè)月的周一-周六10:15分執(zhí)行一次
【0 0 2 ? * 6L】每個(gè)月的最后一個(gè)周六凌晨2點(diǎn)執(zhí)行一次
【0 0 2 LW * ?】每個(gè)月的最后一個(gè)工作日凌晨2點(diǎn)執(zhí)行一次
【0 0 2-4 ? * 1#1】每個(gè)月的第一個(gè)周一凌晨2點(diǎn)到4點(diǎn)期間,每個(gè)整點(diǎn)都執(zhí)行一次
*/
到此這篇關(guān)于Springboot的異步、定時(shí)、郵件任務(wù)的文章就介紹到這了,更多相關(guān)Springboot異步定時(shí)郵件任務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Quarkus中RESTEasy?Reactive集成合并master分支
這篇文章主要為大家介紹了Quarkus中RESTEasy?Reactive集成合并master分支的詳細(xì)作用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
Spring中如何獲取request的方法匯總及其線程安全性分析
這篇文章主要給大家介紹了關(guān)于Spring中如何獲取request的方法匯總及其線程安全性分析的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
SpringBoot實(shí)現(xiàn)國(guó)際化的操作步驟
國(guó)際化(Internationalization) 是指為了適應(yīng)不同語(yǔ)言、文化和地區(qū)的用戶,使軟件能夠方便地進(jìn)行本地化修改的過(guò)程,本文介紹了SpringBoot 國(guó)際化功能的簡(jiǎn)單使用,感興趣的朋友可以參考下2024-02-02
淺談java字符串比較到底應(yīng)該用==還是equals
這篇文章主要介紹了淺談java字符串比較到底應(yīng)該用==還是equals,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
淺談SpringBoot項(xiàng)目打成war和jar的區(qū)別
這篇文章主要介紹了淺談SpringBoot項(xiàng)目打成war和jar的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

