??wei_shuo的個人主頁
??wei_shuo的學(xué)習(xí)社區(qū)
??Hello World !
任務(wù)
異步任務(wù)
Java異步指的是在程序執(zhí)行過程中,某些任務(wù)可以在后臺進(jìn)行,而不會阻塞程序的執(zhí)行。通常情況下,Java異步使用線程池來實(shí)現(xiàn),將任務(wù)放入線程池中,等待線程池中的線程執(zhí)行這些任務(wù)。Java異步可以提高程序的性能和并發(fā)能力,尤其是在處理IO密集型任務(wù)時,可以大大減少等待時間,提高程序的響應(yīng)速度。常見的Java異步實(shí)現(xiàn)方式包括Future、CompletableFuture、RxJava等
- service/AsyncService.java
package com.wei.service; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; /** * @ClassName AsyncService * @Description TODO * @Author wei_shuo * @Date 2023/5/9 18:30 * @Version 1.0 */ @Service public class AsyncService { //告訴Spring這是異步方法 @Async public void hello(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("數(shù)據(jù)正在處理……"); } }
- controller/AsyncController.java
package com.wei.controller; import com.wei.service.AsyncService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @ClassName AsyncController * @Description TODO * @Author wei_shuo * @Date 2023/5/9 18:33 * @Version 1.0 */ @RestController public class AsyncController { @Autowired AsyncService asyncService; @RequestMapping("/hello") public String hello(){ asyncService.hello(); return "OK"; } }
- SpringbootTestApplication啟動類配置注解
package com.wei; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication //開啟異步注解功能 @EnableAsync public class SpringbootTestApplication { public static void main(String[] args) { SpringApplication.run(SpringbootTestApplication.class, args); } }
郵件任務(wù)
- 導(dǎo)入依賴
<!--javax.mail--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
- application.properties
spring.mail.username=1096075493@qq.com spring.mail.password=cjkglgqynqwabaed spring.mail.host=smtp.qq.com #開啟加密驗(yàn)證 spring.mail.properties.mail.smtp.ssl.enable=true
- Test測試
package com.wei; 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 SpringbootTestApplicationTests { @Autowired(required=false) JavaMailSenderImpl javaMailSender; //簡單郵件 @Test void contextLoads() { SimpleMailMessage mailMessage = new SimpleMailMessage(); //主題 mailMessage.setSubject("wei_shuo"); //內(nèi)容 mailMessage.setText("Hello,World!"); mailMessage.setFrom("1096075493@qq.com"); mailMessage.setTo("1096075493@qq.com"); javaMailSender.send(mailMessage); } //復(fù)雜郵件 @Test void contextLoads2() throws MessagingException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); //組裝 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true); helper.setSubject("wei_shuo-plus"); helper.setText("<p style='color:red'>Hello,World-Plus</p>",true); //附件 helper.addAttachment("1.jpg",new File("C:\\Users\\ws199\\Desktop")); helper.setFrom("1096075493@qq.com"); helper.setTo("1096075493@qq.com"); javaMailSender.send(mimeMessage); } /** * * @param html * @param subject * @param text * @throws MessagingException */ //封裝方法 public void sendMail(Boolean html,String subject,String text) throws MessagingException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); //組裝 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,html); helper.setSubject(subject); helper.setText(text,true); //附件 helper.addAttachment("1.jpg",new File("C:\\Users\\ws199\\Desktop")); helper.setFrom("1096075493@qq.com"); helper.setTo("1096075493@qq.com"); javaMailSender.send(mimeMessage); } }
定時任務(wù)
- SpringbootTestApplication.java
package com.wei; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication //開啟異步注解功能 @EnableAsync //開啟定時功能的注解 @EnableScheduling public class SpringbootTestApplication { public static void main(String[] args) { SpringApplication.run(SpringbootTestApplication.class, args); } }
- ScheduledService.java
package com.wei.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; /** * @ClassName ScheduledService * @Description TODO * @Author wei_shuo * @Date 2023/5/10 18:23 * @Version 1.0 */ @Service public class ScheduledService { //固定時間,執(zhí)行代碼 @Scheduled(cron = "0/2 * * * * *") public void hello(){ System.out.println("Hello,你被執(zhí)行了……"); } }
?? 結(jié)語:創(chuàng)作不易,如果覺得博主的文章賞心悅目,還請——
點(diǎn)贊
??收藏
??評論
??文章來源:http://www.zghlxwxcb.cn/news/detail-599585.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-599585.html
到了這里,關(guān)于SpringBoot原理分析 | 任務(wù):異步、郵件、定時的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!