异步、邮件、定时任务

异步、邮件、定时任务,第1张

异步、邮件、定时任务

目录

14. 异步、邮件、定时任务

14.1 异步任务

14.2 邮件任务

14.3 定时任务


14. 异步、邮件、定时任务 14.1 异步任务

编写一个业务测试

文件路径:com--dzj--service--AsynService.java

@Service
public class AsynService {
 
    @Async   //告诉spring这是一个异步的方法
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据正在处理...");
    }
}

 在主启动器类上开启异步注解功能

@EnableAsync  //开启异步注解功能
@SpringBootApplication

 编写测试接口

@RestController
public class AsynController {
 
    @Autowired
    AsynService asynService;
 
    @RequestMapping("/hello")
    public String hello(){
        asynService.hello();//停止三秒才执行,页面转圈~
        return "OK";
    }
}
14.2 邮件任务

导入相关依赖


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

在邮箱设置中开启POP3/SMTP服务

点击我已发送,获得授权码

编写相关配置文件

application.yaml

spring:
  mail:
    username: [email protected]
    password:             #填写上面的授权码作为密码
    host: smtp.qq.com
    properties:
      mail:
        smtp:
          ssl:
            enable: true  # 开启加密验证

 在测试类中测试邮件发送

package com.dzj;
 
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.MimeMessageHelper;
 
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
 
@SpringBootTest
class Springboot09AsynchronousApplicationTests {
 
    @Autowired
    JavaMailSender mailSender;
 
    @Test
    void contextLoads() {
 
        //一个简单邮件的发送
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("支付宝到账80万元");  //邮件主题
        mailMessage.setText("项目收入");            //邮件正文
        mailMessage.setTo("[email protected]");         //收件人
        mailMessage.setFrom("[email protected]");       //发件人
        mailSender.send(mailMessage);
    }
 
    @Test
    void contextLoads2() throws MessagingException {
        //一个复杂邮件的发送,组装
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //正文
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject("支付宝到账100万元");  //邮件主题
        helper.setText("

工资收入

",true); //邮件正文 //附件 helper.addAttachment("1.jpg",new File("E:\Good Study\dengzj.jpg")); helper.addAttachment("2.jpg",new File("E:\Good Study\dengzj.jpg")); helper.setTo("[email protected]"); //收件人 helper.setFrom("[email protected]"); //发件人 mailSender.send(mimeMessage); } }

运行测试!

14.3 定时任务

文件路径:com--dzj--service--ScheduledService.java

编写业务测试类

package com.dzj.service;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
 
@Service
public class ScheduledSercvice {
 
    @Autowired
    JavaMailSender mailSender;
    
 
    @Scheduled(cron="0/10 * * * * ?")
    public void hello(){
        System.out.println("你被执行了。。。");
    }
 
    @Scheduled(cron="0/10 * * * * ?")
    public void sendMail(){
        //一个简单邮件的发送
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("支付宝到账80万元");
        mailMessage.setText("项目收入");
        mailMessage.setTo("[email protected]");
        mailMessage.setFrom("[email protected]");
        mailSender.send(mailMessage);
        System.out.println("邮件已发送");
    }
}

 在主启动器类上开启定时注解功能

@EnableScheduling //开启定时注解功能
@SpringBootApplication

 运行测试!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5676875.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存