使用SpringBoot实现异步和邮件发送任务

1.实现异步任务

  1. 搭建环境,创建一个Springboot项目(添加WEB依赖)

  2. 创建一个AsyncService类:

    package com.ddf.service;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class AsyncService {
    
        //创建一个线程方法
        public void hello(){
            try {
                //停止3秒钟
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("数据正在处理");
        }
    }
    

3.创建Controller类,实现访问页面:(当我们请求服务器是,首页会调用这个线程方法,停止三秒钟)

package com.ddf.controller;

import com.ddf.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("/index")
    public String index(){
        asyncService.hello();   //停止三秒,请求网站时会转圈
        return "ok";
    }
}
  1. 实现异步功能:添加**@Async**:告诉Spring这是一个异步方法

    package com.ddf.controller;
    
    import com.ddf.service.AsyncService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class AsyncController {
    
        @Autowired
        AsyncService asyncService;
    
        //告诉Spring这是一个异步方法
        @Async
        @RequestMapping("/index")
        public String index(){
            asyncService.hello();   //停止三秒,请求网站时会转圈
            return "ok";
        }
    }
    

    然后在Springboot启动类上添加:@EnableAsync注解:开启异步功能

    package com.ddf;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableAsync;
    
    @EnableAsync    //开启异步功能
    @SpringBootApplication
    public class SpringbootTestApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootTestApplication.class, args);
        }
    }
    

2.邮件任务(实现邮件发送)

  1. 引入项目jar包

    <!--javax.email-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    
  2. 配置邮件相关的配置属性

    spring.mail.username=814655997@qq.com
    spring.mail.password=xgevwoibjtudbaij
    #发送的服务器地址
    spring.mail.host=smtp.qq.com
    
    #开启加密验证
    spring.mail.properties.mail.smtp.ssl.enabel=true
    
  3. 创建测试类,实现简单的邮件发送

    package com.ddf;
    
    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.JavaMailSender;
    import org.springframework.mail.javamail.JavaMailSenderImpl;
    
    import javax.mail.internet.MimeMessage;
    import javax.xml.crypto.dsig.SignatureMethod;
    
    @SpringBootTest
    class SpringbootTestApplicationTests {
    
        //调用邮件发送接口
        @Autowired
        JavaMailSenderImpl mailSender;
    
    
        @Test
        void contextLoads() {
    
            //发送一个简单的邮件
            //创建一个邮件发送消息对象
            SimpleMailMessage mailMessage = new SimpleMailMessage();
    
            //邮件标题
            mailMessage.setSubject("ddf你好啊");
            //邮件标内容
            mailMessage.setText("邮件测试发送");
    
            //发送给谁
            mailMessage.setTo("814655997@qq.com");
            //服务器地址,类似于pop3服务
            mailMessage.setFrom("814655997@qq.com");
            //添加需要添加的信息
            mailSender.send(mailMessage);
        }
    }
    
  4. 实现复杂的邮件发送

       @Test
        void contextLoads2() throws MessagingException {
    
            //发送一个复杂的邮件
            //创建一个邮件发送对象
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            //组装
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            //标题
            helper.setSubject("ddf你好啊");
            //为true时解析html格式
            helper.setText("<p style='color:red'>测试第二次发送</p>",true);
    
            //添加图片
            helper.addAttachment("1.jpg",new File("C:\\Users\\11111\\Desktop\\img\\images\\1.jpg"));
            helper.addAttachment("2.jpg",new File("C:\\Users\\11111\\Desktop\\img\\images\\1.jpg"));
    
            //发送给谁
            helper.setTo("814655997@qq.com");
            //服务器地址,类似于pop3服务
            helper.setFrom("814655997@qq.com");
    
            //添加需要添加的信息
            mailSender.send(mimeMessage);
        }
    
  5. 实现封装成一个方法,可灵活使用

    //封装
        public void sendMail(Boolean flag,String subject,String text) throws MessagingException {
            //发送一个复杂的邮件
            //创建一个邮件发送对象
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            //组装,为true时支持多文本上传
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,flag);
            //标题
            helper.setSubject(subject);
            //为true时解析html格式
            helper.setText(text,true);
    
            //添加图片
            helper.addAttachment("1.jpg",new File("C:\\Users\\11111\\Desktop\\img\\images\\1.jpg"));
            helper.addAttachment("2.jpg",new File("C:\\Users\\11111\\Desktop\\img\\images\\1.jpg"));
    
            //发送给谁
            helper.setTo("814655997@qq.com");
            //服务器地址,类似于pop3服务
            helper.setFrom("814655997@qq.com");
    
            //添加需要添加的信息
            mailSender.send(mimeMessage);
        }
    

版权声明:本文为nwxshmo原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。