• SpringBoot 24 任务机制


    24.1 异步任务


    就是它允许多个事件同时发生,而不是说只有那么一个事件执行到底。从这个角度来看,所谓的异步 好像就是 多线程。

    为什么需要异步任务:因为 对于 用户来说,在前端体验的时候,最好做到 0 等待!用户需要的可能不是 一个事务的处理过程和最终结果。而是及时的反馈信息。像其他的东西,你可以异步的给用户去做,做完后再说。但是及时的反馈 才是 用户当下 最想要的。

    所以 异步任务是必须有的!这是为了 提高 用户的体验而来的。

    SpringBoot 在处理异步任务的时候,还蛮简单的。我们只需要 告诉 Spring 哪些方法是 异步的即可

    原理:使用 @Async 标注 一个方法,那么 SpringBoot 就会 开启一个线程池,用一个线程 去 执行这个方法。在这之前,你还必须在 SpringBootApplication 那个启动方法上 使用注解 @EnableAsync 开启异步任务

    • 未经过 异步任务处理的
    package top.muquanyu.service;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class AsyncService {
        public void hello(){
    
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
    
            System.out.println("数据正在处理");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    package top.muquanyu.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import top.muquanyu.service.AsyncService;
    
    @RestController
    public class AsyncController {
    
        @Autowired
        AsyncService asyncService;
    
        @RequestMapping("/hello")
        public String hello(){
            asyncService.hello();
            return "停止三秒对于前端来说太可怕了额";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    它确实是 等待了 三秒。。。然后 才加载 这个 页面。反悔了这段 字符串。

    在这里插入图片描述

    • 使用 异步任务后

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    页面得到 秒加载,然后 那个 异步任务 在 异步的执行。这就是我们想要的效果。而 SpringBoot 轻轻松松的 用两个 注解 就实现了。


    24.2 邮件任务

    为什么需要邮件任务:因为虽然已经过时了,但是邮箱依然是 最简单 最好用的 验证。比如 你 弄个注册功能,或者找回密码功能,都可以用邮箱来做 验证。

    1. 导入依赖
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-mailartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 开启 qq 邮箱 POP3/SMTP 服务 并且 生成授权码

    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述

    1. 配置 application.yaml 文件
    spring:
      mail:
        username: 99565687@qq.com
        password: xxxx
        host: smtp.qq.com
    
        # 开启加密验证
        properties:
          mail:
            smtp:
              ssl:
                enable: true
    
    debug: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    1. 编写 简单的邮件任务 程序
    package top.muquanyu.springboot10tasks;
    
    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;
    
    @SpringBootTest
    class Springboot10TasksApplicationTests {
    
        @Autowired(required = false)
        JavaMailSenderImpl mailSender;
    
        @Test
        void contextLoads() {
    
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
    
            simpleMailMessage.setSubject("邮件任务测试"); // 邮件的主题
            simpleMailMessage.setText("邮件任务测试的内容部分,这里应该可以看到文字描述。");
            simpleMailMessage.setTo("3063111@qq.com"); // 发送给谁
            simpleMailMessage.setFrom("99565687@qq.com"); // 邮件的 来源 是谁
            mailSender.send(simpleMailMessage);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    你会发现 发送邮件 还是需要 一点儿 时间的,我们大概等了 五秒,邮件才发送出去,我 另外一个 QQ 接收到的时候,大概 已经是 第十秒了。

    在这里插入图片描述
    在这里插入图片描述

    1. 编写 复杂的邮件任务 程序

    如果我们 要 在 内容里面 写 图片、视频、音频等,其实 可以 解析 html 的代码。并且 如果 我们想要 发送的邮件 携带附件,又该如何实现呢?

    答:mimeMessage 这个东西 就支持 html 的解析,还有 附件的 携带发送。

    @Test
        void test() throws MessagingException {
    
            // 一个复杂的邮件
            MimeMessage mimeMessage = mailSender.createMimeMessage();
    
            // 组装
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true,"utf-8");
    
            mimeMessageHelper.setSubject("复杂邮件测试");
            // 如果能解析 html 代码,那么 就基本上 什么都可以实现了
            // 比如说 图片 音频 视频 都是可以的
            mimeMessageHelper.setText("

    支持 html 代码解析

    "
    ,true); // 添加 附件 mimeMessageHelper.addAttachment("头像.jpg", new File("C:\\Users\\muqua\\Desktop\\MuQuanyu\\个人信息\\头像.jpg")); mimeMessageHelper.setTo("3063111@qq.com"); // 发送给谁 mimeMessageHelper.setFrom("99565687@qq.com"); // 邮件的 来源 是谁 mailSender.send(mimeMessage); }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    在这里插入图片描述
    在这里插入图片描述


    24.3 定时任务

    定时任务为什么存在:定时任务可以说是 理所应当的一个需求,作为人类来讲,定时这个概念是很有用处的。比如什么时间点去做什么事,或者提醒你做什么。(抢购、公告……)

    TaskExecutor 接口:就一个方法,可以用 lambda 表达式

    TaskScheduler 接口

    1. @EnableScheduling 开启定时功能的注解

    在这里插入图片描述

    1. @Scheduled(cron = "* * * * * ?") 秒 分 时 日 月 周几

    在这里插入图片描述

    推荐 大家使用 cron表达式在线生成器

    30 15 10 * * ?:每天 10点 15分 30 秒 的时候,执行一次。
    30 0/5 10,18 * * ?:每天 10点或18点,每隔 5 分钟 执行一次。
    0 15 10 ? * 1-6:每个月的 周一 到 周六 10点 15 分 执行一次。

  • 相关阅读:
    提升前端性能的 JavaScript 技巧
    同时显示上下两层凸包特征的可视化程序
    vite和webpack的对比
    生产计划体系完整解决方案(2) : 复杂大规模问题之 - 分区规划
    Linux—vmstat命令详解
    4-乙炔基三苯胺,CAS号:205877-26-5
    如何在CSDN写笔记_写笔记前的插件安装
    使用消息队列解决RTOS多线程同时打印串口乱序问题
    猿创征文|C++来时路 _ 重温经典之C++类和对象 | 三大特性之一 - 封装 | 腾讯面试题
    【单片机】msp430g2553单片机, 用TA0定时器,让小灯P1.6呼吸灯,P1.6是TA0.1
  • 原文地址:https://blog.csdn.net/qq_52606908/article/details/126152132