事件驱动架构

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

核心概念

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

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

相关推荐
周杰伦_Jay3 小时前
【MCP开发部署流程表格分析】MCP架构解析、开发流程、部署方案、安全性分析
人工智能·深度学习·opencv·机器学习·架构·transformer
宠友信息3 小时前
从架构到体验:友猫社区平台的全栈技术解析与功能体系详解
架构
东城绝神3 小时前
《Linux运维总结:基于ARM64+X86_64架构CPU使用docker-compose一键离线部署redis 7.4.5容器版分片集群》
linux·运维·redis·架构·分片集群
hello_2503 小时前
容灾架构术语:RPO和RTO
架构
骇客野人3 小时前
【软考备考】 架构评估质量属性:性能、可用性、安全性、可修改性、可测试性、易用性等详细介绍
架构
JH30733 小时前
B/S架构、HTTP协议与Web服务器详解
前端·http·架构
杨筱毅3 小时前
【架构】MVP 对比 MVVM
架构
骇客野人3 小时前
【软考备考】物联网架构:感知层、网络层、平台层、应用层详解
物联网·架构
AI模块工坊6 小时前
AAAI 2025 | 即插即用,川大Mesorch刷新SOTA,用「介观」Transformer架构终结图像造假
人工智能·深度学习·计算机视觉·架构·transformer
周杰伦_Jay6 小时前
【OpenManus深度解析】MetaGPT团队打造的开源AI智能体框架,打破Manus闭源壁垒。包括架构分层、关键技术特点等内容
人工智能·深度学习·opencv·架构·开源