前言该文章是我学习使用SpirngEvent的过程,现在只写了SpringEvent的应用,后续会写一篇从源码关注SpringEvent的实现过程 。
SpringEvent的介绍SpringEvent在我认为是一个解决业务解耦的办法,运用了观察者模式,用于 当一个业务的更改后,需要改变其他业务的状态 。例如一个商品的下单,需要修改商品的库存,以及商家的消息发送等等 。之前我做这种业务解耦的时候,使用的时消息队列进行解耦,但如果只是为了解耦而整合了消息队列,就有点大了,我认为,可以使用此方式需要满足下面的条件:
- 当做完业务的时候,不清楚有多少个子业务要进行更改,例如安防设备的报警,初期可能只是发送消息到持有设备的用户,页面显示报警状态,后期或许会涉及到同步到其他平台业务、和其他安防设备产生连锁报警业务等等 。
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));}}复制代码效果文章插图
异步实现目前有两种方式可以实现异步,
- 一种是使用@EnableAsync和@Async启动异步 。
- 一种是给Springboot的默认实现 SimpleAsyncEventMulticaster 类中的 taskExecutor 字段赋值一个线程池 。
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());}}复制代码
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 护士/护理人员的完整简历范文 护士简历范文
- |职场中有哪些常见的套路?
- |职场上,面子不是所谓的客气,而是让自己有了自信,让自己成功
- 养狗好处vs养狗坏处 养宠物的利弊
- 配音|余生,做一个能扛事的成年人
- 秋作文400字18篇作文 秋天的作文400字
- 翡翠手镯|翡翠手镯的价值,傻瓜教程,教你这样看懂翡翠手镯的色泽
- 短发|爱美的美女看过来,你知道秋天为什么要补水吗?
- 无基础唱歌技巧和发声方式教学 学习唱歌的方法
- 全球变暖的危害有哪些 全球变暖的后果
