spring的事件机制实战
理论
在分布式场景下,实现同步转异步的方式有三种方式:
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);
@Override
public 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;
@Autowired
public 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直接支持的;
实战
spring的事件机制实战的更多相关文章
- Spring的事件机制详解
同步事件和异步事件 同步事件:在一个线程里,按顺序执行业务,做完一件事再去做下一件事. 异步事件:在一个线程里,做一个事的同事,可以另起一个新的线程执行另一件事,这样两件事可以同时执行. 用一个例子来 ...
- spring的事件机制
事件机制作为一种编程机制,在许多语言中都提供了支持.JAVA语言也不例外,java中的事件机制的参与者有3种角色: 1.event object 2.event source 3.event list ...
- Spring ApplicationContext的事件机制
ApplicationContext的事件机制是观察者设计模式的实现,通过 ApplicationEvent 类和 ApplicationListener 接口,可以实现 ApplicationCon ...
- 事件机制-Spring 源码系列(4)
事件机制-Spring 源码系列(4) 目录: Ioc容器beanDefinition-Spring 源码(1) Ioc容器依赖注入-Spring 源码(2) Ioc容器BeanPostProcess ...
- spring事件机制
前置知识补充: 程序里面所谓的“上下文”就是程序的执行环境,打个比方:你就相当于web程序,你的房子就相当于web程序的上下文,你可以在家里放东西,也可以取东西,你的衣食住行都依赖这个房子,这个房子就 ...
- Tomcat与Spring中的事件机制详解
最近在看tomcat源码,源码中出现了大量事件消息,可以说整个tomcat的启动流程都可以通过事件派发机制串起来,研究透了tomcat的各种事件消息,基本上对tomcat的启动流程也就有了一个整体的认 ...
- 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean
7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...
- Spring的事件监听机制
最近公司在重构广告系统,其中核心的打包功能由广告系统调用,即对apk打包的调用和打包完成之后的回调,需要提供相应的接口给广告系统.因此,为了将apk打包的核心流程和对接广告系统的业务解耦,利用了spr ...
- Spring笔记(7) - Spring的事件和监听机制
一.背景 事件机制作为一种编程机制,在很多开发语言中都提供了支持,同时许多开源框架的设计中都使用了事件机制,比如SpringFramework. 在 Java 语言中,Java 的事件机制参与者有3种 ...
随机推荐
- 自建KMS服务器激活Windows office
一.KMS服务器环境介绍 操作系统Centos 7.x Windows Server 也是可以的,此处以Centos7为例安装,使用默认1688端口号 二.服务端安装过程: 1.下载安装包(这 ...
- 选择IT公司的雇主提问
做为IT从业人员,我们去一家公司时,判断一家公司的专业性时,可以通过以下提问获得反馈: 技术问题 1.这个项目使用了哪些技术(语言,框架,库)?2.应用程序是一体化架构还是微服务架构?3.采用了哪些设 ...
- Ribbon负载均衡及Feign消费者调用服务
微服务调用Ribbon 简介 前面讲了eureka服务注册与发现,但是结合eureka集群的服务调用没讲. 这里的话 就要用到Ribbon,结合eureka,来实现服务的调用: Ribbon是Netf ...
- 小程序--log居中 失焦获取表单中的值
value="{{username}}" 绑定值 值在js文件的data中 pa==>Vant-Weap中表单中的值,不是双向绑定的. 你获取值后, 值并没有在对用的data ...
- 2019-2020-1 20199305《Linux内核原理与分析》第十一周作业
ShellShock 攻击实验 (一)何为ShellShock? 2014年9月24日,Bash中发现了一个严重漏洞shellshock,该漏洞可用于许多系统,并且既可以远程也可以在本地触发,这项漏洞 ...
- BZOJ2820/LG2257 YY的GCD 莫比乌斯反演
问题描述 BZOJ2820 LG2257 题解 求 \(\sum\limits_{i=1}^{n}{\sum\limits_{j=1}^{m}{[gcd(i,j)==p]}}\) ,其中 \(p\)为 ...
- BZOJ3894/LG4313 文理分科 新建点最小割
问题描述 BZOJ3894 LG4313 题解 显然一个人只能选文/理 -> 一个人只能属于文(S).理(T)集合中的一个 可以把选择文得到 \(art\) 的收益看做选择文失去 \(scien ...
- Java,该学什么?
本人大学学的是生物技术专业,毕业后入坑Java. 最近有人问我是如何转行的,需要学一些什么.我在网上看到一篇帖子,觉得写得很全.如果是我来写,可能还写不了这么全的.在此分享给网友. 2019秋招几个月 ...
- 【ST开发板评测】使用Python来开发STM32F411
前言 板子申请了也有一段时间了,也快到评测截止时间了,想着做点有意思的东西,正好前一段时间看到过可以在MCU上移植MicroPython的示例,就自己尝试一下,记录移植过程. MicroPython是 ...
- Java描述设计模式(04):抽象工厂模式
本文源码:GitHub·点这里 || GitEE·点这里 一.抽象工厂模式 1.生活场景 汽车生产根据用户选择的汽车类型,指定不同的工厂进行生产,选择红旗轿车,就要使用中国工厂,选择奥迪轿车,就要使用 ...