異步任務(wù)
-
創(chuàng)建一個(gè)Hello項(xiàng)目
-
創(chuàng)建一個(gè)類AsyncService
異步處理還是非常常用的,比如我們?cè)诰W(wǎng)站上發(fā)送郵件,后臺(tái)會(huì)去發(fā)送郵件,此時(shí)前臺(tái)會(huì)造成響應(yīng)不動(dòng),直到郵件發(fā)送完畢,響應(yīng)才會(huì)成功,所以我們一般會(huì)采用多線程的方式去處理這些任務(wù)。
編寫方法,假裝正在處理數(shù)據(jù),使用線程設(shè)置一些延時(shí),模擬同步等待的情況;
@Service public class AsyncService { public void hello(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("業(yè)務(wù)進(jìn)行中...."); } }
-
編寫AsyncController類
我們?nèi)懸粋€(gè)Controller測(cè)試一下
@RestController public class AsyncController { @Autowired private AsyncService asyncService; @GetMapping("/hello") public String hello(){ asyncService.hello(); return "OK"; } }
-
訪問http://localhost:8080/hello進(jìn)行測(cè)試,3秒后出現(xiàn)OK,這是同步等待的情況。
問題:我們?nèi)绻胱層脩糁苯拥玫较?,就在后臺(tái)使用多線程的方式進(jìn)行處理即可,但是每次都需要自己手動(dòng)去編寫多線程的實(shí)現(xiàn)的話,太麻煩了,我們只需要用一個(gè)簡(jiǎn)單的辦法,在我們的方法上加一個(gè)簡(jiǎn)單的注解即可,如下:
-
給hello方法添加
@Async
注解;//告訴Spring這是一個(gè)異步方法 @Async public void hello(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("業(yè)務(wù)進(jìn)行中...."); }
SpringBoot就會(huì)自己開一個(gè)線程池,進(jìn)行調(diào)用!但是要讓這個(gè)注解生效,我們還需要在主程序上添加一個(gè)注解
@EnableAsync
,開啟異步注解功能;@EnableAsync //開啟異步注解功能 @SpringBootApplication public class SpringbootTaskApplication { public static void main(String[] args) { SpringApplication.run(SpringbootTaskApplication.class, args); } }
7、重啟測(cè)試,網(wǎng)頁(yè)瞬間響應(yīng),后臺(tái)代碼依舊執(zhí)行!
郵件任務(wù)
郵件發(fā)送,在我們的日常開發(fā)中,也非常的多,Springboot也幫我們做了支持
- 郵件發(fā)送需要引入
spring-boot-start-mail
- SpringBoot 自動(dòng)配置
MailSenderAutoConfiguration
- 定義
MailProperties
內(nèi)容,配置在application.yml
中 - 自動(dòng)裝配
JavaMailSender
- 測(cè)試郵件發(fā)送
測(cè)試:
-
引入pom依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
看它引入的依賴,可以看到 jakarta.mail
<dependency> <groupId>com.sun.mail</groupId> <artifactId>jakarta.mail</artifactId> <scope>compile</scope> </dependency>
-
查看自動(dòng)配置類:MailSenderAutoConfiguration
這個(gè)類中存在bean,
JavaMailSenderImpl
然后我們?nèi)タ聪屡渲梦募?/p>
@ConfigurationProperties(prefix = "spring.mail") public class MailProperties { private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; private String host; private Integer port; private String username; private String password; private String protocol = "smtp"; private Charset defaultEncoding = DEFAULT_CHARSET; private Map<String, String> properties = new HashMap<>(); private String jndiName; //set、get方法省略。。。 }
-
配置文件:
spring: mail: username: 2356731504@qq.com password: 你的qq授權(quán)碼 host: smtp.qq.com properties: mail: smtp: ssl: enable: true # qq需要配置ssl
獲取授權(quán)碼:在QQ郵箱中的設(shè)置->賬戶->開啟pop3和smtp服務(wù)
-
Spring單元測(cè)試文章來源:http://www.zghlxwxcb.cn/news/detail-645040.html
package com.liming; 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 HelloApplicationTests { @Autowired private JavaMailSenderImpl javaMailSender; //郵件設(shè)置1:一個(gè)簡(jiǎn)單的郵件 @Test void contextLoads() { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setSubject("黎明,你好"); // 主題 mailMessage.setText("這是郵件發(fā)送測(cè)試。。。"); // 正文 mailMessage.setTo("2356731504@qq.com"); // 發(fā)送給誰(shuí) mailMessage.setFrom("2356731504@qq.com"); // 誰(shuí)發(fā) javaMailSender.send(mailMessage); } // 一個(gè)復(fù)雜的郵件 @Test void contextLoads2() throws MessagingException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); //組裝 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); //正文 helper.setSubject("黎明,你好~plus"); helper.setText("<p style='color:red'>這是郵件發(fā)送測(cè)試</p>", true); //附件 helper.addAttachment("1.jpg", new File("D:\\Users\\Pictures\\Saved Pictures\\SAP-ABAP.jpg")); helper.addAttachment("2.jpg", new File("D:\\Users\\Pictures\\Saved Pictures\\SAP-ABAP.jpg")); helper.setTo("2356731504@qq.com"); helper.setFrom("2356731504@qq.com"); javaMailSender.send(mimeMessage); } }
文章來源地址http://www.zghlxwxcb.cn/news/detail-645040.html
到了這里,關(guān)于SpringBoot 異步、郵件任務(wù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!