詳解Springboot中的異步、定時、郵件任務(wù)
一、異步任務(wù)
1、編寫一個類AsyncService
異步處理還是非常常用的,比如我們在網(wǎng)站上發(fā)送郵件,后臺會去發(fā)送郵件,此時前臺會造成響應(yīng)不動,直到郵件發(fā)送完畢,響應(yīng)才會成功,所以我們一般會采用多線程的方式去處理這些任務(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、編寫一個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)在啟動項目進行測試,三秒后才會出現(xiàn)success,現(xiàn)在還不是異步
3、開啟異步
@Async//告訴spring這是一個異步方法
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 #開啟加密驗證 ssl spring.mail.properties.mail.smtp.ssl.enable=true
3、測試
簡單郵件
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("你好,rk");//郵件標題
mailMessage.setText("測試郵件");//郵件內(nèi)柔
mailMessage.setTo("r1624603357@126.com");//收件人郵箱
mailMessage.setFrom("1624603357@qq.com");//發(fā)件人郵箱
mailSender.send(mailMessage);
}

復(fù)雜郵件
@Test
void contextLoads2() throws MessagingException {
//一個復(fù)雜的郵件
MimeMessage mimeMessage = mailSender.createMimeMessage();
//組裝
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//正文
helper.setSubject("你好,rk");
helper.setText("<h1 style='color:red'>測試郵件</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);
}

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

