当前位置:网站首页>异步、郵件、定時三大任務
异步、郵件、定時三大任務
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/
边栏推荐
猜你喜欢
The difference between tail -f, tail -f and tail
指针初阶(基础)
Key detection and sinusoidal signal output developed by Arduino
[AUTOSAR eight OS]
[C language] branch and loop statements (Part 1)
Leetcode-849: maximum distance to the nearest person
Deep analysis of data storage in memory
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
全志A40i/T3如何通过SPI转CAN
Explain the basic concepts and five attributes of RDD in detail
随机推荐
tail -f 、tail -F、tailf的区别
[AUTOSAR nine c/s principle Architecture]
Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
The difference between relational database and non relational database
Vulkan practice first bullet
File operation io-part2
excel去除小数点后面的数据,将数字取整
How to convert Quanzhi a40i/t3 to can through SPI
leetcode-224:基本计算器
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
Win10 can't be installed in many ways Problems with NET3.5
Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
拥抱平台化交付的安全理念
Understanding and distinguishing of some noun concepts in adjustment / filtering
2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)
递归处理组织的几种情况
Test shift right: Elk practice of online quality monitoring
Array and collection performance comparison
Correctly distinguish the similarities and differences among API, rest API, restful API and web service
指针进阶(一)