当前位置:网站首页>异步、郵件、定時三大任務
异步、郵件、定時三大任務
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包,在其下創建類AsyncControllerpackage 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包,創建類ScheduledServicepackage 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/
边栏推荐
猜你喜欢
![[overview of AUTOSAR three RTE]](/img/6a/0df33beb42f165af77a17b5d8b01e2.png)
[overview of AUTOSAR three RTE]

1.12 - Instructions
![[AUTOSAR I overview]](/img/e4/b97c6beebf6f431d2d7cf209c6683e.png)
[AUTOSAR I overview]
![[AUTOSAR 11 communication related mechanism]](/img/bf/834b0fad3a3e5bd9c1be04ba150f98.png)
[AUTOSAR 11 communication related mechanism]

【C语言】分支和循环语句(上)

安全运营四要素之资产、脆弱性、威胁和事件

详解RDD基本概念、RDD五大属性
![[C language] branch and loop statements (Part 1)](/img/47/6efcc59bd26e26f66c698635c26c8b.png)
[C language] branch and loop statements (Part 1)

Test shift right: Elk practice of online quality monitoring

Linear programming of mathematical modeling (including Matlab code)
随机推荐
这不平凡的两年,感谢我们一直在一起!
【AutoSAR 四 BSW概述】
详解RDD基本概念、RDD五大属性
Arduino开发之按键检测与正弦信号输出
Meaning of Tencent cloud free SSL certificate extension file
正确甄别API、REST API、RESTful API和Web Service之间的异同
安全运营四要素之资产、脆弱性、威胁和事件
[AUTOSAR XIII NVM]
[shutter] image component (configure local GIF image resources | load placeholder with local resources)
1.11 - 总线
拥抱平台化交付的安全理念
The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022
Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
Understanding and distinguishing of some noun concepts in adjustment / filtering
Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
(C语言)数据的存储
Vulkan practice first bullet
Win10 can't be installed in many ways Problems with NET3.5
[AUTOSAR 11 communication related mechanism]
Deep analysis of data storage in memory