SpringBoot利用事件监听实现异步发送163邮件

4 阅读2分钟

1. 定义事件

首先,你需要定义一个事件类。这个类通常继承自ApplicationEvent

import org.springframework.context.ApplicationEvent;

public class CustomEvent extends ApplicationEvent {
    public CustomEvent(Object source) {
        super(source);
    }
}

2. 创建事件监听器

接下来,创建一个监听器来监听和处理事件。监听器是一个普通的Spring组件,可以使用@Component注解。

在监听器内部,使用@EventListener注解来标记处理特定事件的方法。

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class CustomEventListener {

    @EventListener
    public void onApplicationEvent(CustomEvent event) {
        // 处理事件
        System.out.println("Received custom event - " + event);
    }
}

3. 发布事件

最后,你需要发布事件。这可以在应用程序的任何地方完成,比如在服务层或控制器中。

使用ApplicationEventPublisher来发布事件。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

@Service
public class CustomEventPublisher {

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    public void publishCustomEvent(final String message) {
        CustomEvent customEvent = new CustomEvent(this);
        // 可以设置更多的属性
        applicationEventPublisher.publishEvent(customEvent);
    }
}

具体演示

以下是展示定义事件、监听事件,并使用@Async注解异步发送163邮件功能。

首先,你需要添加Spring Boot的邮件依赖到你的pom.xml文件中:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

然后,你可以定义一个事件类来表示需要发送邮件的情况:

import org.springframework.context.ApplicationEvent;

public class SendEmailEvent extends ApplicationEvent {
    private String to;
    private String subject;
    private String text;

    public SendEmailEvent(Object source, String to, String subject, String text) {
        super(source);
        this.to = to;
        this.subject = subject;
        this.text = text;
    }

}

接下来,创建一个异步的邮件服务:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class AsyncEmailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Async
    public void sendEmail(SendEmailEvent event) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("your163email@163.com");
        message.setTo(event.getTo());
        message.setSubject(event.getSubject());
        message.setText(event.getText());
        javaMailSender.send(message);
    }
}

在你的application.propertiesapplication.yml文件中配置邮件服务器的属性:

spring.mail.host=smtp.163.com
spring.mail.username=your163email@163.com
spring.mail.password=yourpassword
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

最后,创建一个事件监听器来监听SendEmailEvent事件,并调用异步邮件服务:

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class EmailEventListener {

    @Autowired
    private AsyncEmailService asyncEmailService;

    @EventListener
    public void onApplicationEvent(SendEmailEvent event) {
        asyncEmailService.sendEmail(event);
    }
}

现在,当你需要发送邮件时,只需发布一个SendEmailEvent事件即可:

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;

@Component
public class EmailEventPublisher implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    public void publishSendEmailEvent(String to, String subject, String text) {
        SendEmailEvent sendEmailEvent = new SendEmailEvent(this, to, subject, text);
        applicationEventPublisher.publishEvent(sendEmailEvent);
    }
}

在应用程序中可以通过注入EmailEventPublisher并调用publishSendEmailEvent方法来异步发送邮件。

至此异步发送邮件功能完成。

欢迎访问我的(夏壹分享)公众号 和 博客(sanzhishu)后缀top