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. ...
随机推荐
- mysql Access denied for user root @localhost (using password:YES)错误
C:\AppServ\MySQL> mysql -u root -p Enter password: ERROR 1045 (28000): Access denied for user 'r ...
- 个人信息——头像更换(拍照或相册上传)~ 微信小程序
微信小程序中 在用户信息中关于用户头像更换(拍照或相册上传)功能实现. 图像点击触发事件: <image src='{{personImage}}' bindtap='changeAvatar' ...
- dynamics crm跳转到手机版本的页面
https://community.dynamics.com/crm/f/117/t/210393 https://community.dynamics.com/crm/f/117/t/118414 ...
- PAT 1124 Raffle for Weibo Followers
1124 Raffle for Weibo Followers (20 分) John got a full mark on PAT. He was so happy that he decide ...
- linux所有命令不能用显示-bash: ls: command not found
所有的命令都显示找不到了,原因是修改了/etc/profile造成的 解决方法 1.修正属性文件中的错误 /usr/bin/vi /etc/profile 2.生效属性文件 source /etc/p ...
- Windows10 下安装 Apache2.4+PHP7.1+MySQL5.7
这个教程主要是分享如何快速组建WAMP开发环境,对于软件的详细配置,自行参考文档或搜索. Visual C++ Redistributable for Visual Studio 2015 下载地址: ...
- c# Winform Chart入门
额外参考链接:http://www.cnblogs.com/greenerycn/archive/2008/10/27/microsoft-chart.html winform 仪表盘相关下载链接:/ ...
- 【Jquery+Express.js】 submit() 方法提交form
前端页面 .html 生成一个动态模块 Modal <div class="modal fade" id="addStaff" tabindex=&quo ...
- python 变量之小整数池跟大整数池
在python中定义变量会有:id,type,value.对于==比较的是value,对于is比较的是id. 因此,对于相同value的变量,它的type相同,但是它的id值可能不一样.对于相同id的 ...
- input radio单选框绑定change事件
html页面: <input type="radio" name="sex" value="female">female < ...