• 异步、邮件、定时三大任务


    异步任务

    1. 创建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("数据处理中....");
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
    2. 创建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 "没毛病了";
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
    3. 访问测试http://localhost:8080/hello,此时可以发现网页等待3秒之后会出现没毛病了的字样。这就是同步等待的情况。

    4. 为了让用户体验好,先让用户得到消息,然后后台使用多线程的方式处理结果。我们需要加一个注解,这样,Springboot就会开一个线程池,进行调用。

      //告诉Spring这是一个异步方法
      @Async
      public void hello(){
          try {
              Thread.sleep(3000);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
          System.out.println("数据处理中....");
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10

      还需要在主启动程序上添加一个注解,让异步注解开启

      @EnableAsync //开启异步注解功能
      @SpringBootApplication
      public class SpringbootAsyncApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(SpringbootAsyncApplication.class, args);
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    5. 此时测试发现,网页会瞬间打开,但是后台数据会持续进行,直到数据处理结束。

    邮件任务

    1. 引入依赖

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-mail</artifactId>
      </dependency>
      
      • 1
      • 2
      • 3
      • 4

      在其中可以看到相关的邮件发送依赖

      在这里插入图片描述

    2. 查看JavaMailSenderImpl找到之后,看配置文件也就是properties下的,我们可以看到相关的配置

    3. 将QQ邮箱里面的服务打开,我们使用IMAP/SMTP服务

      在这里插入图片描述

    4. 配置文件

      spring.mail.username=XXX@qq.com
      spring.mail.password=自己的QQ授权码
      spring.mail.host=smtp.qq.com
      spring.mail.properties.mail.smtp.ssl.enable=true
      
      • 1
      • 2
      • 3
      • 4
    5. 测试

      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("XXXX@qq.com");
              message.setFrom("XXXX@qq.com");
              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("XXXX@qq.com");
              helper.setFrom("XXXX@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
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50

      然后就可以看到我们的邮件发送成功了。

    定时任务

    TaskExecutor接口
    
    TaskScheduler接口
    
    • 1
    • 2
    • 3

    两个注解:

    • @EnableScheduling //开启定时功能的注解,在主启动类中
    • @Scheduled //什么时候执行

    测试

    1. 创建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,你被执行了");
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
    2. 在主程序上增加@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);
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
    3. 然后就可以了

    cron表达式

    cron:https://baike.baidu.com/item/cron/10952601?fr=aladdin

    cron的生成:https://www.bejson.com/othertools/cron/

  • 相关阅读:
    被视为“救世主”的架构师,普遍缺失了哪些基础能力?
    基础篇 | 03 | 如何提升用户体验
    实际业务处理 Kafka 消息丢失、重复消费和顺序消费的问题
    jenkins-pipeline语法总结(最全)
    java进阶专栏的学习指南
    Linux之线程概念
    html制作网页案例代码----(故宫博物馆9页)特效很多
    机器学习笔记之高斯网络(二)高斯贝叶斯网络
    青岛地铁交通咨询系统
    线性表应用(非递减合并、分解链表、删除线性表)
  • 原文地址:https://blog.csdn.net/qq_43585922/article/details/125569294