配置websocket消息代理类AbstractBrokerRegistration

在Spring Framework的Spring Messaging和Spring WebSocket模块中,StompBrokerRelayRegistration、SimpleBrokerRegistration和AbstractBrokerRegistration是用于配置WebSocket消息代理(broker)的关键类。这些类通常在配置WebSocket消息代理时通过Java配置或XML配置被使用。

  • AbstractBrokerRegistration:这是一个抽象类,为配置消息代理提供了基础结构和共通的方法。通常不会被直接使用,而是由其子类StompBrokerRelayRegistration和SimpleBrokerRegistration继承。
  • StompBrokerRelayRegistration:这个类用于配置STOMP消息代理的中继(relay)。当应用需要将消息转发到外部的STOMP消息代理(如:RabbitMQ、ActiveMQ等)时,会用到此类。允许你配置代理的URL、登录凭证、心跳等信息。
  • SimpleBrokerRegistration:这个类用于配置一个内置的、简单的消息代理。这个代理对于原型开发和测试都很有用,因为它不需要外部依赖。你可以配置前缀等参数来区分不同应用或不同上下文中的消息。
一、SimpleBrokerRegistration

SimpleBrokerRegistration是AbstractBrokerRegistration的子类,用于配置SimpleBrokerMessageHandler的注册类;SimpleBrokerMessageHandler是一个"simple"的消息代理处理程序,可以识别SimpMessageType中定义的消息类型,在SubscriptionRegistry的帮助下跟踪订阅,并向订阅者发送消息。

如:org.springframework.messaging.simp.config.SimpleBrokerRegistration#getMessageHandler中定义如何创建"simple"消息代理:

java 复制代码
	@Override
	protected SimpleBrokerMessageHandler getMessageHandler(SubscribableChannel brokerChannel) {
    // 此处新建一个消息代理处理程序,其内部会new DefaultSubscriptionRegistry()对象,其内部就是默认消息注册表信息,包括订阅者数量,默认:1024
		SimpleBrokerMessageHandler handler = new SimpleBrokerMessageHandler(getClientInboundChannel(),
				getClientOutboundChannel(), brokerChannel, getDestinationPrefixes());
		if (this.taskScheduler != null) {
			handler.setTaskScheduler(this.taskScheduler);
		}
		if (this.heartbeat != null) {
			handler.setHeartbeatValue(this.heartbeat);
		}
		handler.setSelectorHeaderName(this.selectorHeaderName);
		return handler;
	}

如:org.springframework.messaging.simp.config.MessageBrokerRegistry#getSimpleBroker获取消息代理处理程序,并设置相关属性:

java 复制代码
	@Nullable
	protected SimpleBrokerMessageHandler getSimpleBroker(SubscribableChannel brokerChannel) {
		if (this.simpleBrokerRegistration == null && this.brokerRelayRegistration == null) {
			enableSimpleBroker();
		}
		if (this.simpleBrokerRegistration != null) {
      // 此处获取消息代理处理程序
			SimpleBrokerMessageHandler handler = this.simpleBrokerRegistration.getMessageHandler(brokerChannel);
			handler.setPathMatcher(this.pathMatcher);
      //订阅者缓存大小限制,默认:1024
			handler.setCacheLimit(this.cacheLimit);
			handler.setPreservePublishOrder(this.preservePublishOrder);
			return handler;
		}
		return null;
	}

开源SDK:https://github.com/mingyang66/spring-parent

相关推荐
曲幽16 小时前
FastAPI实战:WebSocket vs Socket.IO,这回真给我整明白了!
python·websocket·nginx·socket·fastapi·web·async·socketio
Densen201420 小时前
发布blazor应用到Linux, 使用nginx作为WebSocket代理
linux·websocket·nginx
花月C20 小时前
基于WebSocket的 “聊天” 业务设计与实战指南
java·网络·后端·websocket·网络协议
tyung21 小时前
用 zhenyi-base 做一个带网页的群聊 Demo
websocket·go
带娃的IT创业者2 天前
工具状态失踪之谜:EventBus事件漏接与asyncio.Lock并发陷阱双线诊断
qt·websocket·并发控制·eventbus·事件驱动架构·pwa·asyncio.lock
特立独行的猫a2 天前
ESP32小智AI的WebSocket 调试工具实现,小智AI后台交互过程揭秘(一、开篇介绍 )
人工智能·websocket·网络协议·esp32·小智ai
特立独行的猫a2 天前
ESP32小智AI的WebSocket 调试工具的实现,小智AI后台交互过程揭秘(二、技术原理与实现过程详解 )
人工智能·websocket·网络协议·esp32·调试工具·小智ai
带娃的IT创业者2 天前
WeClaw 日志分析实战:如何从海量日志中快速定位根因?
运维·python·websocket·jenkins·fastapi·架构设计·实时通信
mftang2 天前
WebSocket 通信协议详细解析
网络·websocket·网络协议
riyue6663 天前
封装 WebSocket 工具类
网络·vue.js·websocket·网络协议·v