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种 ...
随机推荐
- 12C新功能:在线移动数据文件 (Doc ID 1566797.1)
12C New Feature : Move a Datafile Online (Doc ID 1566797.1) APPLIES TO: Oracle Database - Enterprise ...
- idea中导入别人的vue项目并运行
1. 下载node.js 在搭建vue的开发环境之前,先下载node.js,下载地址:https://nodejs.org/en/ https://blog.csdn.net/antma/articl ...
- Centos 7 下安装 Jenkins
Jenkins介绍 Jenkins是一个开源的支持自动化构建.部署等任务的平台.基本上可以说是持续集成(CI).持续发布(CD)不可或缺的工具. 安装Java环境 CentOS 7 安装 JAVA环境 ...
- ASP.NET Core - 基于IHttpContextAccessor实现系统级别身份标识
问题引入: 通过[ASP.NET Core[源码分析篇] - 认证]这篇文章中,我们知道当请求通过认证模块时,会给当前的HttpContext赋予当前用户身份标识,我们在需要授权的控制器中打上[Aut ...
- ZooKeeper(三):请求处理链路的创建过程解析
我们知道,zk就是一个个处理链组成的. 但是,这些处理链是在什么创建的呢? ZooKeeper 中有三种角色的服务节点存在: Leader, Follower, Observer . 而每个服务节点的 ...
- Elasticsearch 6.x版本全文检索学习之集群调优建议
1.系统设置要到位,遵照官方建议设置所有的系统参数. https://www.elastic.co/guide/en/elasticsearch/reference/6.7/setup.html 部署 ...
- ASP.NET MVC教程三:ASP.NET MVC部署方式
ASP.NET MVC编写的程序需要部署到IIS上面才能进行访问,部署方式分为两种. 一.直接用源代码部署 第一种方式可以直接使用源代码进行部署.部署步骤: 1.新建网站 在IIS里面选择网站,然后右 ...
- Selenium(十一):设置元素等待、上传文件、下载文件
1. 设置元素等待 前面我们接触了几个元素等待方法,sleep.implicitly_wait方法,这一章我们就来整体学一下. 现在大多数Web应用程序使用的都是AJAX技术.当浏览器加载页面时,页面 ...
- Python 你见过三行代码的爬虫吗
------------恢复内容开始------------ 每次讲爬虫的时候都会从“发送请求” 开始讲,讲到解析页面的时候可能大部分读者都会卡住,因为这部分确实需要一点XPATH或者CSS选择器的前 ...
- linux-创建/使用快照/克隆(类似windows中备份还原)
一. 创建/使用快照 1.什么是快照 说的直白一点,就是创建一个备份. 当执行了不可逆的错误操作后,可以通过快照用来恢复系统 2.创建快照的3种模式 挂载状态下创建快照 开机状态下创建 ...