當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Springboot的异步、定时、邮件任务
生活随笔
收集整理的這篇文章主要介紹了
Springboot的异步、定时、邮件任务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、異步任務
? 1、編寫一個類AsyncService
? 異步處理還是非常常用的,比如我們在網站上發送郵件,后臺會去發送郵件,此時前臺會造成響應不動,直到郵件發送完畢,響應才會成功,所以我們一般會采用多線程的方式去處理這些任務。
? 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的异步、定时、邮件任务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 工业用微型计算机(7)-指令系统(4)
- 下一篇: linux内核杂记(18)-内核链表结构