Springboot中的事件Event
事件Event作为一种常用的线程通讯工具,在Springboot中可以方便地提供开发者进行线程交互。
1.事件定义

1 import org.springframework.context.ApplicationEvent;
2
3 /**
4 * WebSocket触发事件定义
5 * @author : Asp1rant
6 * @date : 19-3-11 上午10:33
7 */
8
9 public class WebsocketMessageEvent extends ApplicationEvent {
10
11 public String username;
12 public String message;
13
14 public WebsocketMessageEvent(Object source, String username, String message){
15 super(source);
16 this.username = username;
17 this.message = message;
18 }
19 }

2.编写服务,编写时添加接口供外部调用发布

1 import com.baosight.tos.util.event.WebsocketMessageEvent;
2 import org.springframework.beans.factory.annotation.Autowired;
3 import org.springframework.context.ApplicationContext;
4 import org.springframework.stereotype.Service;
5
6 /**
7 * Websocket事件服务
8 * @author : Asp1rant
9 * @date : 19-3-11 上午10:36
10 */
11
12 @Service
13 public class WebsocketMessageService {
14
15 @Autowired
16 ApplicationContext applicationContext;
17
18 public void pubWebsocketMessage(String username, String message){
19 applicationContext.publishEvent(new WebsocketMessageEvent(this, username, message));
20 }
21
22 }

3.事件监听,两种写法:
3.1 定义监听器

1 import org.springframework.context.ApplicationListener;
2
3 public class WebsocketEventListener implements ApplicationListener<WebsocketMessageEvent> {
4
5 @Override
6 public void onApplicationEvent(WebsocketMessageEvent websocketMessageEvent){
7 System.out.println("监听到WebsocketMessageEvent事件");
8 try {
9 Thread.sleep(2000);
10 } catch (InterruptedException e) {
11 e.printStackTrace();
12 }
13 }
14
15 }

3.2 注解EventListener
1 @EventListener
2 public synchronized void WebsocketListener(WebsocketMessageEvent event){
3 System.out.println("监听到WebsocketMessageEvent事件");
4 }
5 }
Springboot中的事件Event的更多相关文章
- JavaScript -- 时光流逝(八):js中的事件Event的使用
JavaScript -- 知识点回顾篇(八):js中的事件Event的使用 事件通常与函数配合使用,这样就可以通过发生的事件来驱动函数执行. (1) onabort : onabort 事件会在图像 ...
- javascript中的事件Event
一.事件流 1.事件流:描述的是从页面中接受事件的顺序 IE的事件流是事件冒泡流,Netscape的事件流是事件捕获流. 2.事件冒泡 IE的事件流叫做事件冒泡(event bubbing),即事件开 ...
- pythonl练习笔记——threading线程中的事件Event
1 事件Event 使用方法:e = threading.Event() Event对象主要用于线程间通信,确切地说是用于主线程控制其他线程的执行. Event事件提供了三个方法:wait等待.cle ...
- 【Spring】9、Spring中的事件Event
Spring的ApplicationContext 提供了支持事件和代码中监听器的功能. 我们可以创建bean用来监听在ApplicationContext 中发布的事件.ApplicationEve ...
- C#中的事件(event)处理机制
委托 语法 [访问修饰符] delegate 返回类型 委托名(); 委托的特点 类似于C++函数指针,但它是类型安全的:委托允许将方法作为参数进行传递:委托可用于定义回调方法:委托可以链接在一起:如 ...
- libevent源码学习(9):事件event
目录在event之前需要知道的event_baseevent结构体创建/注册一个event向event_base中添加一个event设置event的优先级激活一个event删除一个event获取指定e ...
- springboot 中事件监听模型的一种实现
目录 定义事件本身 定义事件源 定义监听者 一.需要实现 ApplicationListener 二.使用 @EventListener 注解 测试 项目结构 前言: 事件监听模型是一种常用的设计模式 ...
- 【Unity3D技巧】在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信 (二) : 引入中间层NotificationCenter
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 一对多的观察者模式机制有什么缺点? 想要查看 ...
- JavaScript 中的window.event代表的是事件的状态,jquery事件对象属性,jquery中如何使用event.target
http://wenda.haosou.com/q/1373868839069215 http://kylines.iteye.com/blog/1660236 http://www.cnblogs. ...
随机推荐
- HTML中的置换元素和非置换元素
我们都知道,行内元素不能够定义宽度和高度,但 img,input,button等标签作为行内元素却可以定义宽高,为什么呢?这就牵扯到了置换元素和非置换元素. 置换元素: w3c官方解释:“An ele ...
- 2019.3.16数据结构考试(Problem 1. rotinv)(循环逆序对)
Problem 1. rotinvInput file: rotinv.inOutput file: rotinv.outTime limit: 2 secondsMemory limit: 256 ...
- ArcGIS中删除“点”附带的对应“文本信息”
现状: 用ArcMap打开对应的.mxd文件,导入KML数据后,几何类型“点” - 每一个点都附带对应的文本信息“Placemark”,如下图: 问题:ArcGIS中如何 删除“点”附带的对应“文本信 ...
- springboot 默认异常处理
SpringBoot默认有自定义异常处理的体系,在做SpringBoot项目的时候,如果是抛出了运行时异常,springBoot并会对异常进行处理,返回如下异常信息: { "timestam ...
- C++文件输入和创建
#include <fstream> //头文件 ifstream inf; ofstream ouf; inf.open("zy4.txt", ios::out); ...
- 压测过程中出现ops断崖式下跌原因及排解
压测机器: 100台docker redis集群:16个分片 在开始压测的半个小时中,一直很稳定,ops稳定在20w左右.但是接下来突然ops断崖式下跌,ops降到了3w以下.然后持续一段时间,直至变 ...
- win10下安装vs2013无法安装解决方案
win10下安装vs2013无法安装解决方案 win+r,输入cmd进入命令行 进入界面后选择修复 进入vs_ultimate文件所在目录,输入: vs_ultimate /Uninstall ...
- learning makefile call func
- Linux 动态链接库 - dll劫持
如何使用动态链接库 Linux下打开使用动态链接库需要三步(实际上和windows下基本一样):1.加载动态链接库,通过调用库函数dlopen()获得链接库的句柄,对应于windows下的 AfxLo ...
- java 反射获取方法返回值类型
//ProceedingJoinPoint pjp //获取方法返回值类型 Object[] args = pjp.getArgs(); Class<?>[] paramsCls = ne ...