配置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

相关推荐
【赫兹威客】浩哥14 小时前
基于SpringBoot+Vue3的健身运动管理系统|WebSocket实时消息 健身房毕设项目
spring boot·websocket·课程设计
Amir_zy2 天前
TCP/IP 与 WebSocket 测试实战:从底层原理到场景落地
websocket·网络协议·tcp/ip
名字还没想好☜2 天前
Next.js Route Handler 做 SSE 服务端推送:实时进度条、自动重连与什么时候别用 WebSocket
开发语言·javascript·websocket·react·sse·next.js
胸中有沟壑3 天前
SpringBoot使用WebSocket
spring boot·后端·websocket
海鸥两三4 天前
【websocket合集2】websocket.js源码解析和页面调用示例
javascript·websocket·网络协议
小小龙学IT4 天前
Boost.Beast 深度实战:基于 Asio 的开源 C++ HTTP/WebSocket 协议库
c++·websocket·http
tech讯息5 天前
企业 AI Agent 对接外部服务如何安全集成?—— 多租户业务场景优先选用 WebSocket 方案
人工智能·websocket·安全
RuoyiOffice6 天前
Springboot+WebSocket×场景×渠道:企业统一消息中心编排实战
spring boot·websocket·短信·消息中心·spring boot 3·站内信·场景编排
烟雨江南7856 天前
国产化语音识别如何落地?——灵声智库国产 CPU/OS、GPU/NPU、流式转写与离线私有化部署实践
人工智能·websocket·语音识别·ai客服·企业agent