SpringBoot 异步注解
1、Application 加注解 @EnableAsync 开启异步。
2、需要异步的方法上加注解 @Async。
--不能为静态方法
--调用处不能在同一个类里
https://blog.csdn.net/qq_36827957/article/details/105087856
https://www.cnblogs.com/xiaogblog/p/14011290.html
yml文件
async: executor: thread: core_pool_size: 5 max_pool_size: 15 queue_capacity: 30 keep_alive_seconds: 60 name_prefix: email-send-
mail:
host: ip
username: 邮箱名称
password: 密码
port: 25
default-encoding: UTF-8
EmailService
@Data @Async("emailSendExecutor") @Component @ConfigurationProperties(prefix = "spring.mail") public class EmailService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final JavaMailSender javaMailSender; private String username; public void sendSimpleMail(String toUser, String subject, String content) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(username); message.setTo(toUser); message.setSubject(subject); message.setText(content); javaMailSender.send(message); } public void sendMimeEmail(String toUser, String subject, String content) { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper messageHelper; try { messageHelper = new MimeMessageHelper(message, true); messageHelper.setFrom(username); messageHelper.setTo(toUser); messageHelper.setSubject(subject); messageHelper.setText(content, true); javaMailSender.send(message); logger.info("发送邮件成功"); } catch (MessagingException e) { e.printStackTrace(); logger.error("发送失败:" + e); } } }