当前位置:网站首页>异步、郵件、定時三大任務
异步、郵件、定時三大任務
2022-07-03 01:05:00 【奔走的王木木Sir】
异步任務
創建
service
包,在其下創建類AsyncService
偽造正在處理數據,導致線程延時,模擬同步等待。
package com.hxl.service; import org.springframework.stereotype.Service; @Service public class AsyncService { public void hello(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("數據處理中...."); } }
創建
controller
包,在其下創建類AsyncController
package com.hxl.controller; import com.hxl.service.AsyncService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class AsyncController { @Autowired AsyncService asyncService; @RequestMapping("/hello") public String hello(){ asyncService.hello(); return "沒毛病了"; } }
訪問測試
http://localhost:8080/hello
,此時可以發現網頁等待3秒之後會出現沒毛病了
的字樣。這就是同步等待的情况。為了讓用戶體驗好,先讓用戶得到消息,然後後臺使用多線程的方式處理結果。我們需要加一個注解,這樣,Springboot就會開一個線程池,進行調用。
//告訴Spring這是一個异步方法 @Async public void hello(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("數據處理中...."); }
還需要在主啟動程序上添加一個注解,讓异步注解開啟
@EnableAsync //開啟异步注解功能 @SpringBootApplication public class SpringbootAsyncApplication { public static void main(String[] args) { SpringApplication.run(SpringbootAsyncApplication.class, args); } }
此時測試發現,網頁會瞬間打開,但是後臺數據會持續進行,直到數據處理結束。
郵件任務
引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
在其中可以看到相關的郵件發送依賴
查看
JavaMailSenderImpl
找到之後,看配置文件也就是properties
下的,我們可以看到相關的配置將QQ郵箱裏面的服務打開,我們使用IMAP/SMTP服務
配置文件
[email protected] spring.mail.password=自己的QQ授權碼 spring.mail.host=smtp.qq.com spring.mail.properties.mail.smtp.ssl.enable=true
測試
package com.hxl; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @SpringBootTest class SpringbootAsyncApplicationTests { @Autowired JavaMailSenderImpl mailSender; @Test void contextLoads() { //一個簡單的郵件 SimpleMailMessage message = new SimpleMailMessage(); message.setSubject("明天放假"); message.setText("直接10天小長假"); message.setTo("[email protected]"); message.setFrom("[email protected]"); mailSender.send(message); } @Test public void contextLoads2() throws MessagingException { //一個複雜的郵件 MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject("明天放假"); helper.setText("<b style='color:red'>你想啥呢</b>",true); //發送附件 helper.addAttachment("1.jpg",new File("文件的路徑")); helper.addAttachment("2.jpg",new File("比如:D:\\2.jpg")); helper.setTo("[email protected]"); helper.setFrom("[email protected]"); mailSender.send(mimeMessage); } }
然後就可以看到我們的郵件發送成功了。
定時任務
TaskExecutor接口
TaskScheduler接口
兩個注解:
- @EnableScheduling //開啟定時功能的注解,在主啟動類中
- @Scheduled //什麼時候執行
測試
創建
Service
包,創建類ScheduledService
package com.hxl.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class ScheduledService { //在一個特定的時間執行這個方法 //cron錶達式 //秒 分 時 日 月 周幾 //下面這句話就是從星期一到星期日,的0秒 @Scheduled(cron = "0 * * * * 0-7") public void hello(){ System.out.println("hello,你被執行了"); } }
在主程序上增加@EnableScheduling 開啟定時任務功能
package com.hxl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; @EnableAsync //開啟异步注解功能 @EnableScheduling //開啟定時功能的注解 @SpringBootApplication public class SpringbootAsyncApplication { public static void main(String[] args) { SpringApplication.run(SpringbootAsyncApplication.class, args); } }
然後就可以了
cron錶達式
cron:https://baike.baidu.com/item/cron/10952601?fr=aladdin
cron的生成:https://www.bejson.com/othertools/cron/
边栏推荐
- 深度剖析数据在内存中的存储
- Vulkan performance and refinement
- Hdu3507 (slope DP entry)
- 指针初阶(基础)
- Sentry developer contribution Guide - configure pycharm
- The difference between tail -f, tail -f and tail
- Arduino开发之按键检测与正弦信号输出
- FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
- Rust string slicing, structs, and enumeration classes
- Solve the cache problem of reactnative using WebView
猜你喜欢
Vulkan practice first bullet
【AutoSAR 五 方法论】
FPGA - 7 Series FPGA internal structure clocking -04- multi area clock
How to systematically learn machine learning
excel IF公式判断两列是否相同
leetcode-2280:表示一个折线图的最少线段数
[case sharing] let the development of education in the new era advance with "number"
1.12 - Instructions
Infrared thermography temperature detection system based on arm rk3568
Win10 多种方式解决无法安装.Net3.5的问题
随机推荐
Inversion de l'intervalle spécifié dans la liste des liens
数学建模之线性规划(含MATLAB代码)
这不平凡的两年,感谢我们一直在一起!
Illustrated network: what is virtual router redundancy protocol VRRP?
链表中的节点每k个一组翻转
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
[love crash] neglected details of gibaro
【AutoSAR 十二 模式管理】
Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
Vulkan is not a "panacea"“
[AUTOSAR I overview]
Vulkan practice first bullet
1038 Recover the Smallest Number
Leetcode-871: minimum refueling times
Deep analysis of data storage in memory
Leetcode 294. Flip game II (game theory)
leetcode-2115:从给定原材料中找到所有可以做出的菜
[AUTOSAR VI description document]
FPGA - 7 Series FPGA internal structure clocking -04- multi area clock
How to systematically learn machine learning