一、異步任務(wù)
1、編寫一個類AsyncService
異步處理還是非常常用的,比如我們在網(wǎng)站上發(fā)送郵件,后臺會去發(fā)送郵件,此時前臺會造成響應(yīng)不動,直到郵件發(fā)送完畢,響應(yīng)才會成功,所以我們一般會采用多線程的方式去處理這些任務(wù)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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、開啟異步
1
2
3
4
5
6
7
8
9
|
@Async //告訴spring這是一個異步方法 public void hello(){ try { System.out.println( "數(shù)據(jù)處理中~" ); Thread.sleep( 3000 ); //停止三秒 } catch (InterruptedException e) { e.printStackTrace(); } } |
1
2
3
4
5
6
7
|
@EnableAsync //開啟異步注解功能 @SpringBootApplication public class Springboot09TestApplication { public static void main(String[] args) { SpringApplication.run(Springboot09TestApplication. class , args); } } |
二、郵件任務(wù)
1、引入依賴
1
2
3
4
|
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> |
2、配置mail
1
2
3
4
5
6
7
8
|
#用戶名 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、測試
簡單郵件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Autowired JavaMailSenderImpl mailSender; @Test void contextLoads() { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setSubject( "你好,rk" ); //郵件標(biāo)題 mailMessage.setText( "測試郵件" ); //郵件內(nèi)柔 mailMessage.setTo( "r1624603357@126.com" ); //收件人郵箱 mailMessage.setFrom( "1624603357@qq.com" ); //發(fā)件人郵箱 mailSender.send(mailMessage); } |
復(fù)雜郵件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@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類
1
2
3
4
5
6
7
8
9
10
11
|
@Service public class ScheduledService { //秒 分 時 日 月 周幾 //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....." ); } } |
項目啟動后每天20:28:00執(zhí)行hello方法
2、添加注解
1
2
3
4
5
6
7
8
|
@EnableAsync //開啟異步注解功能 @EnableScheduling //開啟定時功能注解 @SpringBootApplication public class Springboot09TestApplication { public static void main(String[] args) { SpringApplication.run(Springboot09TestApplication. class , args); } } |
cron表達(dá)式練習(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)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/m0_46979453/article/details/121082374