事件驱动架构

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

核心概念

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

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

相关推荐
hstar95278 小时前
三十四、面向对象底层逻辑-SpringMVC九大组件之FlashMapManager接口设计哲学
java·spring·设计模式·架构
颜颜颜yan_12 小时前
【HarmonyOS5】DevEco Studio 使用指南:代码阅读与编辑功能详解
架构·harmonyos
W说编程13 小时前
Linux与量子计算:面向未来的架构演进
linux·服务器·性能优化·架构·系统架构·量子计算
林鹿14 小时前
Dart: 串联多个数据流
后端·架构·dart
异常君14 小时前
Java 应用中构建 Elasticsearch 多层次缓存:提升查询效率的实战方案
java·elasticsearch·架构
wu~97015 小时前
计算机网络自定向下:第二章复习
服务器·网络·架构
快起来别睡了15 小时前
代理模式:送花风波
前端·javascript·架构
前端付豪15 小时前
汇丰银行技术架构揭秘:全球交易稳定背后的“微服务+容灾+零信任安全体系”
前端·后端·架构
brzhang16 小时前
Flutter 调用原生代码,看这篇就够了:从零教你搭起通信的桥
前端·后端·架构
互联网搬砖老肖16 小时前
Web 架构之 API 安全防护:防刷、防爬、防泄漏
前端·安全·架构