SpringEvent的应用

前言该文章是我学习使用SpirngEvent的过程,现在只写了SpringEvent的应用,后续会写一篇从源码关注SpringEvent的实现过程 。
SpringEvent的介绍SpringEvent在我认为是一个解决业务解耦的办法,运用了观察者模式,用于 当一个业务的更改后,需要改变其他业务的状态  。例如一个商品的下单,需要修改商品的库存,以及商家的消息发送等等 。之前我做这种业务解耦的时候,使用的时消息队列进行解耦,但如果只是为了解耦而整合了消息队列,就有点大了,我认为,可以使用此方式需要满足下面的条件:

  • 当做完业务的时候,不清楚有多少个子业务要进行更改,例如安防设备的报警,初期可能只是发送消息到持有设备的用户,页面显示报警状态,后期或许会涉及到同步到其他平台业务、和其他安防设备产生连锁报警业务等等 。
SpringEvent的应用环境配置JDK8
Spring boot 2.6.10
业务场景当电脑启动的时候,电脑的自启程序需要启动,程序的服务也需要启动等等 。
实现创建ComputerStartEvent电脑启动事件类/** * 电脑启动事件类 */public class ComputerStartEvent extends ApplicationEvent {private ComputerEntity computerEntity;public ComputerEntity getComputerEntity() {return computerEntity;}public ComputerStartEvent(ComputerEntity source) {super(source);this.computerEntity=source;}}复制代码创建AutoStartupSoftwareListener自启软件启动监听类/**** 自启软件启动监听类*/@Componentpublic class AutoStartupSoftwareListener implements ApplicationListener<ComputerStartEvent> {@Overridepublic void onApplicationEvent(ComputerStartEvent event) {ComputerEntity computer=event.getComputerEntity();System.out.println("电脑"+computer.getName()+"的自启软件正在进行");}}复制代码创建ProgramServiceStartupListener程序服务启动监听类/** * 程序服务启动监听类 */@Componentpublic class ProgramServiceStartupListener implements ApplicationListener<ComputerStartEvent> {@Overridepublic void onApplicationEvent(ComputerStartEvent event) {ComputerEntity computer=event.getComputerEntity();System.out.println("电脑"+computer.getName()+"的程序服务正在启动");}}复制代码客户端代码poublic class ComputerService {@Resourceprivate ApplicationEventPublisher applicationEventPublisher;public void computerStart() {ComputerEntity computer=new ComputerEntity();;computer.setComputerId("dafdasf");computer.setName("电脑A");// 电脑启动操作System.out.println(computer.getName()+"电脑启动了");//发布电脑启动事件applicationEventPublisher.publishEvent(new ComputerStartEvent(computer));}}复制代码效果
SpringEvent的应用

文章插图
 
异步实现目前有两种方式可以实现异步,
  • 一种是使用@EnableAsync和@Async启动异步 。
  • 一种是给Springboot的默认实现 SimpleAsyncEventMulticaster 类中的 taskExecutor 字段赋值一个线程池 。
一、使用@EnableAsync和@Async启动异步【SpringEvent的应用】下面为了演示异步的效果,自启软件的监听类不使用异步,程序服务的监听类使用异步,然后每个方法打印当前线程的Id 。
AutoStartupSoftwareListener自启软件启动监听类/*** 自启软件启动监听类*/@Componentpublic class AutoStartupSoftwareListener implements ApplicationListener<ComputerStartEvent> {@Overridepublic void onApplicationEvent(ComputerStartEvent event) {ComputerEntity computer=event.getComputerEntity();System.out.println("电脑"+computer.getName()+"的自启软件正在进行");//打印线程IdSystem.out.println("AutoStartupSoftwareListener监听线程id:"+Thread.currentThread().getId());}}复制代码ProgramServiceStartupListener程序服务启动监听类/** * 程序服务启动监听类 */@EnableAsync@Componentpublic class ProgramServiceStartupListener implements ApplicationListener<ComputerStartEvent> {@Override@Asyncpublic void onApplicationEvent(ComputerStartEvent event) {ComputerEntity computer=event.getComputerEntity();System.out.println("电脑"+computer.getName()+"的程序服务正在启动");//打印线程IdSystem.out.println("ProgramServiceStartupListener监听线程id:"+Thread.currentThread().getId());}}复制代码客户端代码poublic class ComputerService {@Resourceprivate ApplicationEventPublisher applicationEventPublisher;public void computerStart() {ComputerEntity computer=new ComputerEntity();;computer.setComputerId("dafdasf");computer.setName("电脑A");// 电脑启动操作System.out.println(computer.getName()+"电脑启动了");//发布电脑启动事件applicationEventPublisher.publishEvent(new ComputerStartEvent(computer));//打印线程IdSystem.out.println("computerStart方法线程id:"+Thread.currentThread().getId());}}复制代码


推荐阅读