日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Springboot的异步、定时、邮件任务

發布時間:2025/3/12 javascript 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Springboot的异步、定时、邮件任务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、異步任務

? 1、編寫一個類AsyncService
? 異步處理還是非常常用的,比如我們在網站上發送郵件,后臺會去發送郵件,此時前臺會造成響應不動,直到郵件發送完畢,響應才會成功,所以我們一般會采用多線程的方式去處理這些任務。

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("數據處理中~");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 {@AutowiredAsyncService asyncService;@GetMapping("/hello")public String hello(){asyncService.hello();return "success";} }

? 現在啟動項目進行測試,三秒后才會出現success,現在還不是異步

? 3、開啟異步

@Async//告訴spring這是一個異步方法public void hello(){try {System.out.println("數據處理中~");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=6566243357@qq.com #密碼 spring.mail.password=yblyxhvmnsurbbci #發送郵件服務器 spring.mail.host=smtp.qq.com #開啟加密驗證 ssl spring.mail.properties.mail.smtp.ssl.enable=true

? 3、測試

? 簡單郵件

@AutowiredJavaMailSenderImpl mailSender;@Testvoid contextLoads() {SimpleMailMessage mailMessage = new SimpleMailMessage();mailMessage.setSubject("你好,rk");//郵件標題mailMessage.setText("測試郵件");//郵件內柔mailMessage.setTo("r544603357@126.com");//收件人郵箱mailMessage.setFrom("6566243357@qq.com");//發件人郵箱mailSender.send(mailMessage);}

? 復雜郵件

@Testvoid 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"));// 發/收件人helper.setTo("r1624603357@126.com");helper.setFrom("1624603357@qq.com");//發送mailSender.send(mimeMessage);}

三、定時任務

? 1、編寫一個ScheduledService類

@Service public class ScheduledService {//秒 分 時 日 月 周幾//0 * * * * MON-FRI//注意cron表達式的用法; 每天20:28 0秒執行該方法@Scheduled(cron = "0 28 20 * * 0-7")public void hello(){System.out.println("現在是20:28");System.out.println("hello.....");} }

? 項目啟動后每天20:28:00執行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分鐘執行一次 【0 15 10 ? * 1-6】每個月的周一-周六10:15分執行一次 【0 0 2 ? * 6L】每個月的最后一個周六凌晨2點執行一次 【0 0 2 LW * ?】每個月的最后一個工作日凌晨2點執行一次 【0 0 2-4 ? * 1#1】每個月的第一個周一凌晨2點到4點期間,每個整點都執行一次 */

總結

以上是生活随笔為你收集整理的Springboot的异步、定时、邮件任务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。