180609-Spring之事件驱动机制的简单使用

文章链接:https://liuyueyi.github.io/hexblog/hexblog/2018/06/09/180609-Spring之事件驱动机制的简单使用/
Spring之事件驱动机制的简单使用
关于事件的发起与相应,在客户端的交互中可算是非常频繁的事情了,关于事件的发布订阅,在Java生态中,EventBus可谓是非常有名了,而Spring也提供了事件机制,本文则主要介绍后端如何在Spring的环境中,使用事件机制
I. 使用姿势
主要借助org.springframework.context.ApplicationEventPublisher#publishEvent(org.springframework.context.ApplicationEvent) 来发布事件,而接受方,则直接在处理的方法上,添加 @@EventListener注解即可
1. 事件定义
发布一个事件,所以第一件事就是要定义一个事件,对Spring而言,要求自定义的事件继承自ApplicationEvent类, 一个简单的demo如下
public class NotifyEvent extends ApplicationEvent {
@Getter
private String msg;
public NotifyEvent(Object source, String msg) {
super(source);
this.msg = msg;
}
}
2. 发布事件
发布时间则比较简单,直接拿到ApplicationContext实例,执行publish方法即可,如下面给出一个简单的发布类
@Component
public class NotifyPublisher implements ApplicationContextAware {
private ApplicationContext apc;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.apc = applicationContext;
}
// 发布一个消息
public void publishEvent(int status, String msg) {
if (status == 0) {
apc.publishEvent(new NotifyEvent(this, msg));
} else {
apc.publishEvent(new NewNotifyEvent(this, msg, ((int) System.currentTimeMillis() / 1000)));
}
}
}
3. 事件监听器
在方法上添加注解即可,如下
@Component
public class NotifyQueueListener {
@EventListener
public void consumerA(NotifyEvent notifyEvent) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("A: " + Thread.currentThread().getName() + " | " + notifyEvent.getMsg());
}
@EventListener
public void consumerB(NewNotifyEvent notifyEvent) {
System.out.println("B: " + Thread.currentThread().getName() + " | " + notifyEvent.getMsg());
}
@EventListener
public void consumerC(NotifyEvent notifyEvent) {
System.out.println("C: " + Thread.currentThread().getName() + " | " + notifyEvent.getMsg());
}
}
II. 疑问及解答
1. 发布与监听器的关联
上面给出了使用的姿势,看起来并不复杂,也比较容易使用,但是一个问题需要在使用之前弄明白了,发布事件和监听器是怎么关联起来的呢?
- 根据方法的参数类型执行
那么如果发布者,推送的是一个NotifyEvent类型的事件,那么接收者是怎样的呢?
- 参数为NotifyEvent以及其子类的监听器,都可以接收到消息
测试用例如下:
NewNotifyEvent 继承自上面的NotifyEvent
public class NewNotifyEvent extends NotifyEvent {
@Getter
private int version;
public NewNotifyEvent(Object source, String msg) {
super(source, msg);
}
public NewNotifyEvent(Object source, String msg, int version) {
super(source, msg);
this.version = version;
}
}
然后借助上面的消息发布者发送一个消息
@Test
public void testPublishEvent() throws InterruptedException {
notifyPublisher.publishEvent(1, "新的发布事件! NewNotify");
System.out.println("---------");
notifyPublisher.publishEvent(0, "旧的发布事件! Notify");
}
输出结果如下,对于NewNotifyEvent, 参数类型为NotifyEvent的consumerA, consumerC都可以接收到
A: main | 新的发布事件! NewNotify
C: main | 新的发布事件! NewNotify
B: main | 新的发布事件! NewNotify
---------
A: main | 旧的发布事件! Notify
C: main | 旧的发布事件! Notify
2. 消息接收的顺序
上面消息处理是串行的,那么先后顺序怎么确定? (下面的答案不确定,有待深入源码验证!!!)
- 先扫描到的bean先处理
- 同一个bean中,按精确匹配,先后定义顺序进行
3. 异步消费
对于异步消费,即在消费者方法上添加一个@Async注解,并需要在配置文件中,开启异步支持
@Async
@EventListener
public void processNewNotifyEvent(NewNotifyEvent newNotifyEvent) {
System.out.println("new notifyevent: " + newNotifyEvent.getMsg() + " : " + newNotifyEvent.getVersion());
}
配置支持
@Configuration
@EnableAsync
public class AysncListenerConfig implements AsyncConfigurer {
/**
* 获取异步线程池执行对象
*
* @return
*/
@Override
public Executor getAsyncExecutor() {
return new ThreadPoolExecutor(5, 10, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(),
new DefaultThreadFactory("test"), new ThreadPoolExecutor.CallerRunsPolicy());
}
}
III. 其他
一灰灰Blog: https://liuyueyi.github.io/hexblog
一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛
声明
尽信书则不如,已上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激
- 微博地址: 小灰灰Blog
- QQ: 一灰灰/3302797840
扫描关注

180609-Spring之事件驱动机制的简单使用的更多相关文章
- 简单说说spring的事务机制,以及是如何管理的?
事务管理可以帮助我们保证数据的一致性,对应企业的实际应用很重要. Spring的事务机制包括声明式事务和编程式事务. 编程式事务管理:Spring推荐使用TransactionTemplate,实际开 ...
- Spring Event事件驱动
Spring事件驱动模型,简单来说类似于Message-Queue消息队列中的Pub/Sub发布/订阅模式,也类似于Java设计模式中的观察者模式. 自定义事件 Spring的事件接口位于org.sp ...
- JS事件驱动机制
还记得当初学JAVA-GUI编程时学习过事件监听机制,此时再学习JavaScript中的事件驱动机制,不免简单.当初学习时也是画过原理图,所以从原理图开始吧! js是采用事件驱动(event-driv ...
- Nginx——事件驱动机制(雷霆追风问题,负载均衡)
事件处理框架 所有的worker进程都在ngx_worker_process_cycle方法中循环处理事件,处理分发事件则在ngx_worker_process_cycle方法中调用ngx_proce ...
- Spring事务传播机制和数据库隔离级别
Spring事务传播机制和数据库隔离级别 转载 2010年06月26日 10:52:00 标签: spring / 数据库 / exception / token / transactions / s ...
- 【转】Java异常总结和Spring事务处理异常机制浅析
异常的概念和Java异常体系结构 异常是程序运行过程中出现的错误.本文主要讲授的是Java语言的异常处理.Java语言的异常处理框架,是Java语言健壮性的一个重要体现. Thorwable类所有异常 ...
- 7 -- Spring的基本用法 -- 3... Spring 的核心机制 : 依赖注入
7.3 Spring 的核心机制 : 依赖注入 Spring 框架的核心功能有两个. Spring容器作为超级大工厂,负责创建.管理所有的Java对象,这些Java对象被称为Bean. Spring容 ...
- Java异常总结和Spring事务处理异常机制浅析
异常的概念和Java异常体系结构 异常是程序运行过程中出现的错误.本文主要讲授的是Java语言的异常处理.Java语言的异常处理框架,是Java语言健壮性的一个重要体现. Thorwable类所有异常 ...
- Redis源代码解析:13Redis中的事件驱动机制
Redis中.处理网络IO时,採用的是事件驱动机制.但它没有使用libevent或者libev这种库,而是自己实现了一个很easy明了的事件驱动库ae_event,主要代码只400行左右. 没有选择l ...
随机推荐
- css3的代替图片的三角形
1.小三角形(与边框结合,不兼容IE8) .callout{ position: relative; width: 100px; height: 100px; background: #fce6ed; ...
- LightOJ 1203--Guarding Bananas(二维凸包+内角计算)
1203 - Guarding Bananas PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 M ...
- Python Monitoring UPS with SNMPWALK
##Background My co-worker told me he needed to monitor UPS with SNMP module but he only can get hexa ...
- 自己写的代码实现Session
package com.zq.web.context.windows; import java.util.HashMap;import java.util.Map; import org.apache ...
- python__PIP : 安装第三方库
pipy国内镜像目前有: http://pypi.douban.com/ 豆瓣 http://pypi.hustunique.com/ 华中理工大学 http://pypi.sdutlinux.o ...
- linux系统快速安装宝塔
宝塔面板分linux面板和windows面板,安装宝塔linux面板首先要访问宝塔官网查看对应版本进行选择 宝塔面板的安装需要注意的地方有: 1.纯净系统 2.确保是干净的操作系统,没有安装过其它环境 ...
- Java开发小技巧(六):使用Apache POI读取Excel
前言 在数据仓库中,ETL最基础的步骤就是从数据源抽取所需的数据,这里所说的数据源并非仅仅是指数据库,还包括excel.csv.xml等各种类型的数据接口文件,而这些文件中的数据不一定是结构化存储的, ...
- ubuntu 防止软件包自动更新
阻止软件包升级 有两种方法阻止软件包升级,使用dpkg,或者在Woody中使用APT. 使用dpkg,首先导出软件包选择列表: dpkg --get-selections \* > select ...
- 最新Altium_Designer_Beta_18.7.is AD18安装教程及破解说明
下解Altium_Designer带破解的压缩包. 下载链接:https://pan.baidu.com/s/1TlPHtSthJKxLcXWcCR-q-g 密码:bt0g 解压缩Altium_Des ...
- 推荐软件7 taskbar numberer,结果get了WIN相关的快捷键
作为键盘控,Win+数字直达任务栏上的应用已经让我欣喜.接下来我的问题就是每次要数数字才能确定是哪个数字,期间我尝试过按常用顺序进行排序并尝试记住它们.直到我想也许应该有个软件可以在任务栏图标处贴上一 ...