spring boot 异步发送邮件
发送邮件由于是一个耗时的操作,有可能需要一个几十秒的操作,但是呢,接口 是一个瞬间完成的,为了不影响接口的性能,所以需要对发送邮件的操作进行异步操作,我们这里呢,首先我们要引入发送邮件的测试模块。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
引入包后呢,我们去配置需要的邮件相关的配置,
mail:
host: smtp.qq.com
port: 587
username: 952943386@qq.com
password: 需要在你用的邮箱那里配置
default-encoding: utf-8
properties:
mail:
smtp:
socketFactoryClass: javax.net.ssl.SSLSocketFactory
debug: true
这样配置完毕之后呢,就可以发送邮件了,我们利用异步,首先我们先编写一个发送邮件的接口
public interface EmailServer {
void sendemail(String subject,String from ,String touserEmail,String text);
}
我们去实现这个接口,
@Component
@Service
public class EmailServerImpl implements EmailServer {
@Autowired
private JavaMailSender javaMailSender;
@Async("taskExecutor")
@Override
public void sendemail(String subject, String from, String touserEmail, String text) {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject(subject);
message.setFrom(from);
message.setTo(touserEmail);
message.setSentDate(new Date());
message.setText(text);
javaMailSender.send(message);
}
}
实现完毕后呢,我们这里已经配置完毕了,我们就可以在正常 的业务去调用了。
我这里改的找回密码的逻辑。
if (user.getEmail() != null) {
emailServerl.sendemail("全栈测试平台修改密码",user.getEmail(),
user.getEmail(),"你的密码修改成功,用户名:" + user.getUsername());
}
这里,需要在上面去引入
@Autowired
private EmailServer emailServerl;
这样我们就已经开发完毕了,我们还需要配置启动的时候,启动异步。
SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class}, scanBasePackages = "pan")
@EnableScheduling
@EnableAsync//增加这里即可,
public class PlanApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(PlanApplication.class, args);
}
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(PlanApplication.class);
}
@Bean
MeterRegistryCustomizer meterRegistryCustomizer(MeterRegistry meterRegistry) {
return meterRegistry1 -> {
meterRegistry.config()
.commonTags("application", "Tenantapp");
};
}
}
配置完毕后,我们需要配置下异步任务的配置
@Configuration
public class TaskPoolConfig {
@Bean("taskExecutor")
public Executor taskExecutor () {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心线程数10:线程池创建时候初始化的线程数
executor.setCorePoolSize(10);
// 最大线程数20:
executor.setMaxPoolSize(15);
// 缓冲队列200:
executor.setQueueCapacity(200);
// 允许线程的空闲时间60秒:
executor.setKeepAliveSeconds(60);
// 线程池名的前缀:
executor.setThreadNamePrefix("taskExecutor-");
/*
线程池对拒绝任务的处理策略:这里采用了CallerRunsPolicy策略,
*/
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 设置线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean
executor.setWaitForTasksToCompleteOnShutdown(true);
// 设置线程池中任务的等待时间,如果超过这个时候还没有销毁就强制销毁,
executor.setAwaitTerminationSeconds(600);
return executor;
}
}
这样,我们就完成了整体的代码开发,我们去调用下我们的api测试下

测试完毕,接口返回正常,我们去看下,我们的日志有没有执行我们的发送邮件。
Hibernate: select user0_.id as id1_70_, user0_.admin as admin2_70_, user0_.email as email3_70_, user0_.errornum as errornum4_70_, user0_.freeze as freeze5_70_, user0_.freezetime as freezeti6_70_, user0_.iphone as iphone7_70_, user0_.password as password8_70_, user0_.status as status9_70_, user0_.token as token10_70_, user0_.username as usernam11_70_ from user user0_ where user0_.username=?
。。。。。。。。 250 OK: queued as.
DEBUG SMTP: message successfully delivered to mail server
QUIT
221 Bye.
日志打印,我们看下正常我们应该可以看到邮件的,打开qq邮箱,收到了这封邮件。

这样我们的异步发送邮件就修改成功了,完成了我们异步的发送邮件开发。后续封装下发送其他类型的模块,就可以完成了,我们的异步发送邮件的
spring boot 异步发送邮件的更多相关文章
- Spring Boot异步发送邮件和请求拦截器配置
用户登录流程图: 在spring拦截器中进行鉴权操作: 控制器的拦截: import com.mooc.house.common.model.User; import org.springframew ...
- Spring Boot 2发送邮件手把手图文教程
原文:http://www.itmuch.com/spring-boot/send-email/ 本文基于:Spring Boot 2.1.3,理论支持Spring Boot 2.x所有版本. 最近有 ...
- Spring Boot 之发送邮件
Spring Boot 之发送邮件 简介 API 配置 实战 引入依赖 配置邮件属性 Java 代码 完整示例 引申和引用 简介 Spring Boot 收发邮件最简便方式是通过 spring-boo ...
- Spring Boot (17) 发送邮件
添加依赖 <!--发送邮件 --> <dependency> <groupId>org.springframework.boot</groupId> & ...
- Spring Boot 异步请求和异步调用,一文搞定
一.Spring Boot中异步请求的使用 1.异步请求与同步请求 特点: 可以先释放容器分配给请求的线程与相关资源,减轻系统负担,释放了容器所分配线程的请求,其响应将被延后,可以在耗时处理完成(例如 ...
- SpringBoot系列:Spring Boot异步调用@Async
在实际开发中,有时候为了及时处理请求和进行响应,我们可能会多任务同时执行,或者先处理主任务,也就是异步调用,异步调用的实现有很多,例如多线程.定时任务.消息队列等, 这一章节,我们就来讲讲@Async ...
- Spring boot 中发送邮件
参考:https://blog.csdn.net/qq_39241443/article/details/81293939 添加依赖: <dependency> <groupId&g ...
- Spring Boot 异步运用
使用@Async标签 导入包 org.springframework.scheduling.annotation.Async 并配置并发线程池asyncTaskConfig 实现AsyncConfig ...
- Spring Boot 异步调用
添加一个类ThreadPoolConfig.java package com.cjcx.inter.framework.config; import org.springframework.conte ...
随机推荐
- Python数据科学手册(2) NumPy入门
NumPy(Numerical Python 的简称)提供了高效存储和操作密集数据缓存的接口.在某些方面,NumPy 数组与 Python 内置的列表类型非常相似.但是随着数组在维度上变大,NumPy ...
- Deeplink推广,打开率很低怎么办?
但凡做TOC业务的多多少少都会用到Deeplink,这是一个重要的运营手段.但用了Deeplink却没有达到预期的目标,打开率不尽人意,你有没有想过到底是什么原因? 在Deeplink这条路上,我们当 ...
- Spark入门(四)--Spark的map、flatMap、mapToPair
spark的RDD操作 在上一节Spark经典的单词统计中,了解了几个RDD操作,包括flatMap,map,reduceByKey,以及后面简化的方案,countByValue.那么这一节将介绍更多 ...
- Docker基本概念及架构
一.Docker基本概念 Docker是一个开源的容器引擎,基于Go 语言并遵从 Apache2.0 协议开源.Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布 ...
- 035.集群安全-Pod安全
一 Pod安全 1.1 PodSecurityPolicy启用 为了更精细地控制Pod对资源的使用方式,Kubernetes从1.4版本开始引入了PodSecurityPolicy资源对象对Pod的安 ...
- Collection-接口中的方法(新手)
/* Collection 接口中的方法 ArrayList implements List 数组列表 实现 列表 List extends Collection 列表 继承 数组列表*///导入包. ...
- Mol Cell Proteomics. | A Targeted Mass Spectrometry Strategy for Developing Proteomic Biomarkers: A Case Study of Epithelial Ovarian Cancer(利用靶向质谱策略进行上皮性卵巢癌病例的蛋白质组生物标志物研究) (解读人:王聚)
文献名:利用靶向质谱策略进行上皮性卵巢癌病例的蛋白质组生物标志物研究 期刊名:Molecular & Cellular Proteomics 发表时间:(2019年7月) IF:5.41 单位 ...
- JavaWeb----Cookie&Session
## 会话技术 1.会话:一次会话中包含多次请求和响应. * 第一次会话:浏览器第一次给服务器资源发送请求,会话建立,直到有一方断开为止. 2.功能:再一次会话的范围内的多次请求间,共享数据 3. ...
- 单调栈-Maximum Width Ramp
2020-01-23 19:39:26 问题描述: 问题求解: public int maxWidthRamp(int[] A) { Stack<Integer> stack = new ...
- word2vec 和 glove 模型的区别
2019-09-09 15:36:13 问题描述:word2vec 和 glove 这两个生成 word embedding 的算法有什么区别. 问题求解: GloVe (global vectors ...