当前位置:网站首页>异步、郵件、定時三大任務
异步、郵件、定時三大任務
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/
边栏推荐
- Specified interval inversion in the linked list
- FPGA - 7 Series FPGA internal structure clocking -04- multi area clock
- 全志A40i/T3如何通过SPI转CAN
- 【AutoSAR 十二 模式管理】
- leetcode-2115:从给定原材料中找到所有可以做出的菜
- MySQL multi table joint deletion
- matlab查找某一行或者某一列在矩阵中的位置
- Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
- Inversion de l'intervalle spécifié dans la liste des liens
- Leetcode-224: basic calculator
猜你喜欢
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
excel去除小数点后面的数据,将数字取整
2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)
瑞萨电子RZ/G2L开发板上手评测
基于ARM RK3568的红外热成像体温检测系统
[AUTOSAR eight OS]
无向图的割点
数学建模之线性规划(含MATLAB代码)
[shutter] image component (configure local GIF image resources | load placeholder with local resources)
随机推荐
Delete duplicate elements in the ordered linked list -ii
指针进阶(一)
电话网络问题
[shutter] image component (configure local GIF image resources | load placeholder with local resources)
mysql 多表联合删除
excel IF公式判断两列是否相同
Linear programming of mathematical modeling (including Matlab code)
leetcode:701. 二叉搜索树中的插入操作【bst的插入】
百度智能云牵头打造智能云综合标准化平台
[AUTOSAR + IO Architecture]
Rust string slicing, structs, and enumeration classes
删除有序链表中重复的元素-II
这不平凡的两年,感谢我们一直在一起!
Vulkan practice first bullet
Leetcode-2115: find all the dishes that can be made from the given raw materials
1.11 - 总线
Deep analysis of data storage in memory
Sentry developer contribution Guide - configure pycharm
[AUTOSAR II appl overview]
瑞萨RZ/G2L ARM开发板存储读写速度与网络实测