理论

在分布式场景下,实现同步转异步的方式有三种方式:
1.异步线程池执行;比如借助@Asyn注解,放到spring自带的线程池中去执行;
2.放到消息队列中,在消费者的代码中异步的消费,执行相关的逻辑;
3.基于spring的事件机制,触发事件,在监听器里实现相关逻辑;

spring中自带了事件的支持,核心类是ApplicationEventPublisher;

事件包括三个要点:下面是一个demo的实现,理论结合实战。

1 事件的定义;

package com.springbootpractice.demoevent.event;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEvent;/*** @说明 吃饭事件* @作者 carter* @创建时间 2019年07月12日 13:12**/
public class EatEvent extends ApplicationEvent {private static final Logger logger = LoggerFactory.getLogger(EatEvent.class);private Boolean eatFinished;public EatEvent(Boolean eatFinished) {super(eatFinished);this.eatFinished = eatFinished;}public void callGirlFriend() {logger.info("美女,吃完饭了,来收拾一下吧!");}public void callBrothers() {logger.info("兄弟们,吃完饭了,来打dota !");}public Boolean getEatFinished() {return eatFinished;}
}

2 事件监听的定义;

package com.springbootpractice.demoevent.event.listener;import com.springbootpractice.demoevent.event.EatEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;import java.util.Objects;/*** 说明: XEvent的事件监听器** @author carter* 创建时间 2019年07月12日 13:19**/
@Component
public class EatEventListener implements ApplicationListener<EatEvent> {private static final Logger logger = LoggerFactory.getLogger(EatEventListener.class);@Overridepublic void onApplicationEvent(EatEvent xEvent) {if (Objects.isNull(xEvent)) {return;}if (xEvent.getEatFinished()) {xEvent.callGirlFriend();logger.info("xxxx,女朋友拒绝收拾!");xEvent.callBrothers();logger.info("满人了,下次带你!");}}
}

3 发布事件;

package com.springbootpractice.demoevent.web;import com.springbootpractice.demoevent.event.EatEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** 说明 测试控制器** @author carter* 创建时间 2019年07月12日 13:23**/
@RestController
public class TestController {private final ApplicationEventPublisher applicationEventPublisher;@Autowiredpublic TestController(ApplicationEventPublisher applicationEventPublisher) {this.applicationEventPublisher = applicationEventPublisher;}@GetMapping(path = "/eatOver")public Object eatOver() {EatEvent xEvent = new EatEvent(true);applicationEventPublisher.publishEvent(xEvent);return "eat over and publish event success";}}

无需多余的配置,springmvc直接支持的;

实战

完整代码地址

转载于:https://www.cnblogs.com/snidget/p/11364806.html

spring的事件机制实战相关推荐

  1. Spring的事件机制

    Spring的事件机制 Spring的事件机制 自定义一个事件 发布一个事件 监听器 深入Spring源码 Spring的事件机制 自定义一个事件 通过继承ApplicationEvent自定义事件 ...

  2. Spring中ApplicationContext的事件机制

    ApplicationContext事件机制是观察者设计模式的实现,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext事件 ...

  3. Spring事件机制

    一.事件驱动模型简介 事件驱动模型也就是我们常说的观察者,或者发布-订阅模型:理解它的几个关键点: 首先是一种对象间的一对多的关系:最简单的如交通信号灯,信号灯是目标(一方),行人注视着信号灯(多方) ...

  4. Spring高手之路7——事件机制与监听器的全面探索

    文章目录 1. Spring中的观察者模式 2. 监听器 2.1 实现ApplicationListener接口创建监听器 2.2 @EventListener注解创建监听器 2.3 对比Applic ...

  5. Spring 与 Spring Boot 中的事件机制

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 温安适 来源 | https://my.osc ...

  6. Spring的事件发布机制

    一:Spring的事件发布 ApplicationContext提供了针对Bean的事件传播功能,其中的主角是publishEvent()方法,通过这个方法可以将事件通知给系统内的监听器(需实现App ...

  7. Spring事件机制详解

    一.前言 说来惭愧,对应Spring事件机制之前只知道实现 ApplicationListener 接口,就可以基于Spring自带的事件做一些事情(如ContextRefreshedEvent),但 ...

  8. spring boot 源码分析(七) 事件机制 之 SpringApplicationEvent

    2019独角兽企业重金招聘Python工程师标准>>> 一.前言 前面的文章我们讲解了一下spring boot配置文件加载的相关源码分析,下面我们将从源码角度讲解一下spring  ...

  9. 框架源码专题:Spring的事件监听、发布机制 ApplicationListener

    文章目录 1.Spring内置事件 2.自定义事件 3.事件监听器 4.事件发布 publishEvent 4.Spring事件原理 5. 面试题:怎么样可以在所有Bean创建完后做扩展代码? 6. ...

最新文章

  1. LINUX共享内存使用常见陷阱与分析(转)
  2. java删除第一个节点_访问单个节点的删除(Java)
  3. Leet Code OJ 112. Path Sum [Difficulty: Easy]
  4. linux的lsof命令详解
  5. 出现 java.lang.NullPointerException 的几种原因、可能情况
  6. 卖萌屋新闻联播栏目,倾情上线~
  7. 前后台交互经常使用的技术汇总(后台:Java技术,前台:Js或者Jquery)
  8. c语言饿结构_C语言的四种程序结构
  9. 路由器太远手机接收不到信号怎么办
  10. 小米手机开启开发者模式以及INSTALL_FAILED_USER_RESTRICTED报错处理
  11. matlab的解线性方程组
  12. HTML制作虾米音乐,最新虾米音乐电台调用代码
  13. 蓝牙音频传输格式-AAC
  14. 台式计算机图形设置,如何打开计算机图形设置以提高游戏质量?
  15. springboot工程中生成二维码(Java)
  16. Android 练习项目 ——简单记账软件的实现
  17. 基于Python+MySQL的书店销售管理管理子系统设计
  18. 在 dart fluter 中使用 typedef
  19. 官宣!清华副校长薛其坤将任南方科技大学校长
  20. oracle使用(五)表空间创建、删除以及删除后数据文件还存在的问题

热门文章

  1. php概率计算_PHP指定概率算法
  2. win7旗舰版安装不了python_怎样在Win7 64位旗舰版安装Python+Eclipse开发环境
  3. mysqls压力测试怎么用_MySQL压力测试工具使用
  4. g++ 安装python_以后再也不用“教程”:让人举一反三的python配置环境过程 + 超简单原理概括!...
  5. python与sqlite3_sqlite3与python2.5,pysqlite和apsw有什么区别
  6. android 在什么情况下会主动gc_Python 什么情况下会生成 pyc 文件?
  7. C语言实现面向接口编程
  8. 如何优雅地检测类型/表达式有效性?
  9. C 之父:精通C 很难,但你一天之内就能学习使用C
  10. python抢货程序_写个Python程序上下班抢个顺风单