事件驱动架构

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

核心概念

事件驱动架构(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

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

相关推荐
独立开阀者_FwtCoder1 小时前
Nginx 正式拥抱现代 JavaScript
前端·javascript·架构
多啦C梦a16 小时前
【适合小白篇】什么是 SPA?前端路由到底在路由个啥?我来给你聊透!
前端·javascript·架构
Codebee16 小时前
OneCode 3.0: 注解驱动的Spring生态增强方案
后端·设计模式·架构
Java技术小馆17 小时前
RPC vs RESTful架构选择背后的技术博弈
后端·面试·架构
腾讯云开发者17 小时前
架构师如何与 AI 共生进化?2025 腾讯云架构师峰会来揭晓!
人工智能·架构
krysliang18 小时前
qiankun使用教程(从项目搭建到容器化部署)
前端·架构
代码改变世界ctw18 小时前
ARM汇编编程(AArch64架构)第13课:多核启动与调度
汇编·arm开发·架构
Goboy19 小时前
温故而知新,忆 Spring Bean 加载全流程
后端·面试·架构
武子康20 小时前
AI炼丹日志-30-新发布【1T 万亿】参数量大模型!Kimi‑K2开源大模型解读与实践
人工智能·gpt·ai·语言模型·chatgpt·架构·开源
Codebee21 小时前
OneCode 3.0 VFS客户端驱动(SDK)技术解析:从架构到实战
java·后端·架构