詳解Springboot中的異步、定時、郵件任務
一、異步任務
1、編寫一個類AsyncService
異步處理還是非常常用的,比如我們在網(wǎng)站上發(fā)送郵件,后臺會去發(fā)送郵件,此時前臺會造成響應不動,直到郵件發(fā)送完畢,響應才會成功,所以我們一般會采用多線程的方式去處理這些任務。
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);
}
}
二、郵件任務
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ā)送郵件服務器 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);
}

復雜郵件
@Test
void contextLoads2() throws MessagingException {
//一個復雜的郵件
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);
}

三、定時任務
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表達式練習
/*
【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的異步、定時、郵件任務的文章就介紹到這了,更多相關(guān)Springboot異步定時郵件任務內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中JsonObject與JsonArray轉(zhuǎn)換方法實例
在項目日常開發(fā)中常常會遇到JSONArray和JSONObject的轉(zhuǎn)換,很多公司剛?cè)肼毜男∶刃聲ㄔ谶@里,下面這篇文章主要給大家介紹了關(guān)于java中JsonObject與JsonArray轉(zhuǎn)換方法的相關(guān)資料,需要的朋友可以參考下2023-04-04
SpringCloud3.x集成BigQuery的代碼實現(xiàn)
Google BigQuery 是一種高性能、可應用于大數(shù)據(jù)分析的公主云數(shù)據(jù)庫服務,Spring Cloud 提供了完善的工具和核心功能,可以進行泛動分布應用構(gòu)建,本文給大家介紹了SpringCloud3.x集成BigQuery的代碼實現(xiàn),需要的朋友可以參考下2025-01-01
SpringBoot實現(xiàn)application配置信息加密
在配置文件中,我們有開發(fā)環(huán)境配置和生產(chǎn)環(huán)境配置,而生產(chǎn)環(huán)境的配置信息是需要做好防護的,避免外泄,所以本文為大家整理了application配置信息加密的方法,需要的可以參考下2023-07-07
淺談SpringBoot項目如何讓前端開發(fā)提高效率(小技巧)
這篇文章主要介紹了淺談SpringBoot項目如何讓前端開發(fā)提高效率(小技巧),主要介紹了Swagger和Nginx提高效率的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04
Java實現(xiàn)數(shù)據(jù)庫圖片上傳功能詳解
這篇文章主要為大家詳細介紹了如何使用Java實現(xiàn)數(shù)據(jù)庫圖片上傳功能,包含從數(shù)據(jù)庫拿圖片傳遞前端渲染,感興趣的小伙伴可以跟隨小編一起學習一下2025-03-03
JAVA利用HttpClient進行HTTPS接口調(diào)用的方法
本篇文章主要介紹了JAVA利用HttpClient進行HTTPS接口調(diào)用的方法,具有一定的參考價值,有興趣的可以了解一下2017-08-08

