Spring Boot 项目中使用事件发布和监听来实现消息推送和处理

对于在 Spring Boot 项目内部不同函数间或不同线程间的消息推送和处理机制,您可以使用 Spring 内置的事件发布/订阅机制。这种机制允许不同组件之间异步通信,而不需要它们彼此直接依赖。

以下是一个示例,展示了如何在 Spring Boot 项目中使用事件发布和监听来实现消息推送和处理。

1. 定义事件类

首先,定义一个事件类,该类继承自 ApplicationEvent:

java 复制代码
package com.example.demo.events;

import org.springframework.context.ApplicationEvent;

public class CustomEvent extends ApplicationEvent {
    private final String message;

    public CustomEvent(Object source, String message) {
        super(source);
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}

2. 创建事件发布者

接下来,创建一个事件发布者类,使用 ApplicationEventPublisher 发布事件:

java 复制代码
package com.example.demo.publishers;

import com.example.demo.events.CustomEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

@Component
public class EventPublisher {
    private final ApplicationEventPublisher publisher;

    public EventPublisher(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }

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

3. 创建事件监听器

然后,创建一个事件监听器类,使用 @EventListener 注解来监听事件:

java 复制代码
package com.example.demo.listeners;

import com.example.demo.events.CustomEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class EventListenerExample {

    @EventListener
    public void handleCustomEvent(CustomEvent event) {
        System.out.println("Received event - " + event.getMessage());
    }
}

4. 使用事件发布者

最后,在任何需要的地方使用事件发布者来发布事件:

java 复制代码
package com.example.demo;

import com.example.demo.publishers.EventPublisher;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {
            EventPublisher publisher = ctx.getBean(EventPublisher.class);
            publisher.publishEvent("Hello, Event Driven World!");
        };
    }
}

5. 运行应用

启动 Spring Boot 应用,您会看到事件监听器捕获到发布的事件并处理它。

这个示例展示了如何在 Spring Boot 应用中使用事件发布和监听机制来实现内部的消息推送和处理。不同的组件可以通过发布和监听事件进行异步通信,而不需要直接依赖彼此,从而实现解耦和更灵活的设计。

后续建议:
a. 添加更多的事件类型和对应的监听器来处理不同类型的事件。
b. 结合异步处理,使用 @Async 注解使事件处理器方法异步执行,提高性能。

相关推荐
GISer_Jing12 分钟前
如何学习Agent
后端·langchain
JWASX1 小时前
【Go】map 源码解析 - group
后端·golang·map·源码分析
文丑颜不良啊1 小时前
LeetCode2141~2160题目解析
java·leetcode
LabVIEW开发2 小时前
LabVIEW 连接板:28 个端子怎么排,优先级怎么定
java·算法·labview
天天被压力2 小时前
【A股分钟级高频数据完全手册 #07】北交所历史分钟K线接口:京市Pro版分时批量
java·人工智能·python
天天被压力2 小时前
【A股分钟级高频数据完全手册 #08】股票日、分时数据接口对比:普通分时与Pro版差异实测
java·人工智能·python
量化分析码农2 小时前
【Python量化波动率实战 01】波动率是怎么算的:年化、滚动与区间估计一次讲清
后端
特立独行的猫A2 小时前
AI 时代,我为什么推荐用 cj-tauri 开发桌面应用
java·后端·python
金銀銅鐵3 小时前
从一个类名到一张类图:用反射自动生成 PlantUML
java·后端·uml
小郎君6583 小时前
注解式脱敏:用 Jackson 的 ContextualSerializer 做字段级策略(一)
spring boot·spring cloud