• springboot-异步、定时、邮件任务


    一、异步任务

    1、创建项目

    2、创建一个service包

    3、创建一个类AsyncService

    异步处理还是非常常用的,比如我们在网站上发送邮件,后台会去发送邮件,此时前台会造成响应不动,直到邮件发送完毕,响应才会成功,所以我们一般会采用多线程的方式去处理这些任务。

    编写方法,假装正在处理数据,使用线程设置一些延时,模拟同步等待的情况;

    4、编写controller包

    5、编写AsyncController类

    1. package com.example.springboottask.controller;
    2. import com.example.springboottask.service.AsyncService;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.web.bind.annotation.GetMapping;
    5. import org.springframework.web.bind.annotation.RestController;
    6. @RestController
    7. public class AsyncController {
    8. @Autowired
    9. AsyncService asyncService;
    10. @GetMapping("/hello")
    11. public String hello(){
    12. asyncService.hello();
    13. return "success";
    14. }
    15. }

    6、访问http://localhost:8080/hello进行测试,3秒后出现success,这是同步等待的情况。

    问题:我们如果想让用户直接得到消息,就在后台使用多线程的方式进行处理即可,但是每次都需要自己手动去编写多线程的实现的话,太麻烦了,我们只需要用一个简单的办法,在我们的方法上加一个简单的注解即可,如下:

    7、给hello方法添加@Async注解;

    1. @EnableAsync //开启异步注解功能

    1. package com.example.springboottask.service;
    2. import org.springframework.scheduling.annotation.Async;
    3. import org.springframework.stereotype.Service;
    4. @Service
    5. public class AsyncService {
    6. @Async
    7. public void hello(){
    8. try {
    9. Thread.sleep(3000);
    10. } catch (InterruptedException e) {
    11. e.printStackTrace();
    12. }
    13. System.out.println("业务进行中....");
    14. }
    15. }

     @Async注解用于声明一个方法是异步的,在Spring框架中使用。这个注解的主要作用是将方法的执行委托给Spring的任务执行器(TaskExecutor),以便在单独的线程中运行,从而实现异步处理。使用@Async注解可以提升应用程序的性能,特别是在处理耗时较长的操作时,因为它允许调用者不必等待方法执行完毕就可以继续执行其他操作。

    SpringBoot就会自己开一个线程池,进行调用!但是要让这个注解生效,我们还需要在主程序上添加一个注解@EnableAsync ,开启异步注解功能;

     二、定时任务

    项目开发中经常需要执行一些定时任务,比如需要在每天凌晨的时候,分析一次前一天的日志信息,Spring为我们提供了异步执行任务调度的方式,提供了两个接口。

    • TaskExecutor接口

    • TaskScheduler接口

    两个注解:

    • @EnableScheduling

    • @Scheduled

    cron表达式:

    测试步骤:

    1、创建一个ScheduledService

    我们里面存在一个hello方法,他需要定时执行,怎么处理呢?

    1. package com.example.springmvcexamples.example07.timer;
    2. import lombok.extern.slf4j.Slf4j;
    3. import org.springframework.scheduling.annotation.Scheduled;
    4. import org.springframework.stereotype.Component;
    5. @Component
    6. @Slf4j
    7. public class MyTimer {
    8. @Scheduled(cron = "0 0 8 10 * ?")
    9. public void paySalary() {
    10. log.debug("Your salary has been paid!");
    11. }
    12. }

    2、这里写完定时任务之后,我们需要在主程序上增加@EnableScheduling 开启定时任务功能

    1. package com.example.springmvcexamples.example07.timer;
    2. import lombok.extern.slf4j.Slf4j;
    3. import org.springframework.scheduling.annotation.Scheduled;
    4. import org.springframework.stereotype.Component;
    5. @Component
    6. @Slf4j
    7. public class MyTimer {
    8. @Scheduled(cron = "0 0 8 10 * ?")
    9. public void paySalary() {
    10. log.debug("Your salary has been paid!");
    11. }
    12. }

    三、邮件任务

    邮件发送,在我们的日常开发中,也非常的多,Springboot也帮我们做了支持

    • 邮件发送需要引入spring-boot-start-mail

    • SpringBoot 自动配置MailSenderAutoConfiguration

    • 定义MailProperties内容,配置在application.yml中

    • 自动装配JavaMailSender

    • 测试邮件发送

    测试:

    1、引入pom依赖

    1. org.springframework.boot
    2. spring-boot-starter-mail

    2、配置文件:

    1. spring.mail.username=33430657322@qq.com
    2. spring.mail.password=你的qq授权码
    3. spring.mail.host=smtp.qq.com
    4. # qq需要配置ssl
    5. spring.mail.properties.mail.smtp.ssl.enable=true

    获取授权码:在QQ邮箱中的设置->账户->开启pop3和smtp服务

    3、Spring单元测试

    1. package com.example.springboottask;
    2. import jakarta.mail.MessagingException;
    3. import jakarta.mail.internet.MimeMessage;
    4. import org.junit.jupiter.api.Test;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.boot.test.context.SpringBootTest;
    7. import org.springframework.mail.SimpleMailMessage;
    8. import org.springframework.mail.javamail.JavaMailSenderImpl;
    9. import org.springframework.mail.javamail.MimeMessageHelper;
    10. import java.io.File;
    11. @SpringBootTest
    12. class SpringbootTaskApplicationTests {
    13. @Autowired
    14. JavaMailSenderImpl mailSender;
    15. @Test
    16. public void contextLoads() {
    17. //邮件设置1:一个简单的邮件
    18. SimpleMailMessage message = new SimpleMailMessage();
    19. message.setSubject("通知-明天来这听课"); // 设置邮件主题
    20. message.setText("今晚7:30开会"); // 设置邮件正文
    21. message.setTo("24736743@qq.com"); // 设置收件人邮箱
    22. message.setFrom("24736743@qq.com"); // 设置发件人邮箱
    23. mailSender.send(message); // 发送邮件
    24. }
    25. @Test
    26. public void contextLoads2() throws MessagingException {
    27. //邮件设置2:一个复杂的邮件
    28. MimeMessage mimeMessage = mailSender.createMimeMessage(); // 创建一个MimeMessage对象
    29. MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); // 创建一个MimeMessageHelper对象,用于构建复杂邮件
    30. helper.setSubject("通知-明天来这听课"); // 设置邮件主题
    31. helper.setText("今天 7:30来开会",true); // 设置邮件正文,支持HTML格式
    32. //发送附件
    33. helper.addAttachment("1.jpg",new File("")); // 添加附件1
    34. helper.addAttachment("2.jpg",new File("")); // 添加附件2
    35. helper.setTo("24736743@qq.com"); // 设置收件人邮箱
    36. helper.setFrom("24736743@qq.com"); // 设置发件人邮箱
    37. mailSender.send(mimeMessage); // 发送邮件
    38. }
    39. }

  • 相关阅读:
    微信小程序手写时间间隔组件,可设置间隔时间一分钟,半小时,一小时的间隔
    【百度AI_人脸识别】图片对比相似度、人脸对比登录(调摄像头)
    JavaScript系列:JS实现复制粘贴文字以及图片
    FFmpeg抓取RTSP图像进行图像分析
    046:mapboxGL加载天地图路网图+标记(wmts方式)
    mysql为什么使用B+树
    一文带你了解【抽象类和接口】
    JavaScript基本语法详解
    基于JAVA SpringBoot的网课管理系统设计与实现源码
    1455. 检查单词是否为句中其他单词的前缀
  • 原文地址:https://blog.csdn.net/qq_62377885/article/details/136486100