SpringBoot中实现消息的发布与订阅
一、前言
在项目开发过程中,经常会遇到一些除主线业务之外的其它业务,比如:在核心业务方法执行过后,去记录日志或者发送邮件和短信通知。但是对于用户来说这些操作都是不可见的,所以为了提高接口的响应速率以此提高用户的体验,我们可以使用到SpringBoot中的消息发布与订阅模式。
二、具体实现
为了更好的描述消息的发布与订阅,本文中将以模拟用户注册和登录成功之后发送通知消息来进行演示。
1.定义消息事件
通过继承Spring官方提供的 ApplicationEvent 接口定义自己的应用消息事件:
/**
* 应用消息事件
*/
public class ApplicationMessageEvent extends ApplicationEvent {
private static final long serialVersionUID = -945889113683338622L;
public ApplicationMessageEvent(Object source) {
super(source);
}
}
接下来分别定义注册成功和登录成功的消息事件,目的是区分不同的事件消息
定义注册成功的消息事件:
/**
* 注册成功消息事件
*/
public class UserRegisterEvent extends ApplicationMessageEvent{
private static final long serialVersionUID = -6027272882727163945L;
public UserRegisterEvent(Object source) {
super(source);
}
}
定义登录成功的消息事件:
/**
* 登录成功消息事件
*/
public class UserLoginEvent extends ApplicationMessageEvent{
private static final long serialVersionUID = -90742928698794549L;
public UserLoginEvent(Object source) {
super(source);
}
}
2.自定义线程池
用于指定异步处理消息时使用的线程池(非必须),如果不进行这一步操作,Spring会使用默认的线程池
@Configuration
public class TaskPoolConfig {
@Bean("myAsyncExecutor")
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(7);
executor.setMaxPoolSize(15);
executor.setQueueCapacity(50);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("my-async-executor-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
}
3.发送消息事件
发送事件消息时需要注入Spring官方提供的 ApplicationEventPublisher 类,并且调用 publishEvent 方法发送事件
@Service
@Slf4j
public class UserService {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
public void imitateLogin() {
log.info("用户成功登录");
applicationEventPublisher.publishEvent(new UserLoginEvent("发送一条登录成功的消息"));
}
public void imitateRegister() {
log.info("用户成功注册");
applicationEventPublisher.publishEvent(new UserRegisterEvent("发送一条注册成功的消息"));
}
}
4.监听消息事件
@EventListener:创建时间监听器
@Async(“myAsyncExecutor”):myAsyncExecutor为自定义的线程池
@Component
@Slf4j
public class ApplicationMessageListener {
@EventListener
@Async("myAsyncExecutor")
public void listenEvent(ApplicationMessageEvent event) {
Object source = event.getSource();
if (event instanceof UserLoginEvent) {
log.info("监听到登录成功消息:{}", source);
}
if (event instanceof UserRegisterEvent) {
log.info("监听到注册成功消息:{}", source);
}
}
}
5.开启异步线程
@EnableAsync:在主启动类上加上此注解表示开启异步线程
@SpringBootApplication
@EnableAsync
public class EventSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(EventSpringBootApplication.class, args);
}
}
6.运行测试
2023-04-19 16:59:23.504 INFO 33280 --- [nio-8888-exec-1] com.aben.event.service.UserService : 用户成功注册
2023-04-19 16:59:23.531 INFO 33280 --- [sync-executor-1] c.a.e.l.ApplicationMessageListener : 监听到注册成功消息:发送一条注册成功的消息
2023-04-19 16:59:47.921 INFO 33280 --- [nio-8888-exec-5] com.aben.event.service.UserService : 用户成功登录
2023-04-19 16:59:47.922 INFO 33280 --- [sync-executor-2] c.a.e.l.ApplicationMessageListener : 监听到登录成功消息:发送一条登录成功的消息