事件驱动架构

事件驱动模型是基于观察者模式或者发布订阅模式实现的。

核心概念

事件驱动架构(Event-Driven Architecture,EDA)是一种基于事件和异步通信的架构模式。其核心概念包括以下几个方面:

事件(Event): 事件是系统中发生的事情或状态变化的表示。它可以是用户操作、传感器数据、消息等。事件通常包含有关事件发生的上下文信息。

事件发布者(Event Publisher)

事件订阅者(Event Subscriber)

事件通道(Event Channel): 事件通道是事件发布者和事件订阅者之间的通信媒介。它可以是消息队列、事件总线、消息中间件等,用于在系统中传递事件。

异步通信: 事件驱动架构中的通信是异步的,即事件发布者和订阅者之间不需要立即响应对方。这使得系统更加灵活、可伸缩,能够更好地处理分布式环境中的事件流。

状态管理: 一些事件可能导致系统中的状态发生变化。因此,事件驱动架构通常需要有效的状态管理机制,以确保系统能够正确地响应事件并保持一致性。

spring实现事件驱动架构的组件

ApplicationEventPublisher

ApplicationEventPublisherAware接口的实现类通常是由Spring容器自动注入ApplicationEventPublisher的。Spring容器在实例化Bean的过程中,如果发现某个Bean实现了ApplicationEventPublisherAware接口,就会自动调用其setApplicationEventPublisher方法,将ApplicationEventPublisher注入到该Bean中。

复制代码
@Component
public class CustomEventPublisher implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher eventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }

    public void publishEvent(String message) {
        CustomEvent customEvent = new CustomEvent(this, message);
        eventPublisher.publishEvent(customEvent);
    }
}

ApplicationEventPublisher的publishEvent方法是用于将事件发布给所有对该事件感兴趣的监听器。

Event

事件(Event): 事件是在系统中发生的某种事情,它是一个普通的POJO类,通常继承自ApplicationEvent。

复制代码
import org.springframework.context.ApplicationEvent;

public class CustomEvent extends ApplicationEvent {
    // 事件相关的信息
    public CustomEvent(Object source, String message) {
        super(source);
        // 初始化事件信息...
    }
}

Listener

当事件被发布时,所有实现了ApplicationListener接口的监听器都会接收到该事件,并且放入listener的onapplicationevent方法通过instanceof来判断是否是自己处理的event。

复制代码
@Component
public class CustomEventListener implements ApplicationListener<CustomEvent> {

    @Override
    public void onApplicationEvent(CustomEvent event) {
        if (event instanceof CustomEvent) {
            // 处理自己感兴趣的事件...
        }
    }
}

其他的一些类:只使用上述三个类即可

如果你使用Spring框架进行事件驱动开发,并且只关心简单的事件发布和监听,通常情况下你不需要直接使用ApplicationEventMulticaster或其默认实现SimpleApplicationEventMulticaster。Spring框架会在后台自动处理这些细节,你只需关注事件的定义、发布和监听即可。

不使用spring,使用java原生类进行完整的逻辑开发会使用到的类

EventObject

复制代码
public class EventObject implements java.io.Serializable {

    @java.io.Serial
    private static final long serialVersionUID = 5516075349620653480L;

    /**
     * The object on which the Event initially occurred.
     */
    protected transient Object source;

    /**
     * Constructs a prototypical Event.
     *
     * @param source the object on which the Event initially occurred
     * @throws IllegalArgumentException if source is null
     */
    public EventObject(Object source) {
        if (source == null)
            throw new IllegalArgumentException("null source");

        this.source = source;
    }

    /**
     * The object on which the Event initially occurred.
     *
     * @return the object on which the Event initially occurred
     */
    public Object getSource() {
        return source;
    }

    /**
     * Returns a String representation of this EventObject.
     *
     * @return a String representation of this EventObject
     */
    public String toString() {
        return getClass().getName() + "[source=" + source + "]";
    }
}

EventListener

什么也没就是一个接口标识

相关推荐
Pasregret22 分钟前
多级缓存架构深度解析:从设计原理到生产实践
缓存·架构
国科安芯27 分钟前
面向高性能运动控制的MCU:架构创新、算法优化与应用分析
单片机·嵌入式硬件·安全·架构·机器人·汽车·risc-v
Java技术小馆2 小时前
SpringBoot中暗藏的设计模式
java·面试·架构
Lei活在当下3 小时前
【现代 Android APP 架构】01. APP 架构综述
android·设计模式·架构
前端大白话3 小时前
深入理解 JavaScript 中 async 函数与 await 关键字的执行奥秘
前端·javascript·架构
gs801408 小时前
深度解析:从12306看混合云架构下的高并发系统设计
架构·12306
weixin_307779139 小时前
分层设计数据仓库的架构和设计高效数据库系统的方法
数据仓库·架构
喝拿铁写前端13 小时前
从圣经Babel到现代编译器:没开玩笑,普通程序员也能写出自己的编译器!
前端·架构·前端框架
KdanMin15 小时前
AR行业应用案例与NXP架构的结合
架构·ar
艾厶烤的鱼16 小时前
架构-计算机网络
计算机网络·架构