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

相关推荐
我也爱吃馄饨1 小时前
项目复盘:websocket不受跨域限制的原理
网络·websocket·网络协议
网络抓包与爬虫3 小时前
android应用崩溃了,通过崩溃手机连接电脑查找日志方法
websocket·网络协议·tcp/ip·http·网络安全·https·udp
iOS技术狂热者5 小时前
Flutter Mac上使用VSCode支持Flutter开发
websocket·网络协议·tcp/ip·http·网络安全·https·udp
冲上云霄的Jayden9 小时前
Paddlex服务化代理处理跨域、替换Response中Json key
json·fastapi·代理·跨域·uvicorn·paddlex·服务化
鹅肝手握高V五色10 小时前
android studio 运行flutter项目
websocket·网络协议·tcp/ip·http·网络安全·https·udp
flying jiang18 小时前
HttpServletRequest
java·网络·websocket·http
Jiaberrr18 小时前
Vue3 实战:基于 mxGraph 与 WebSocket 的动态流程图构建
前端·javascript·vue.js·websocket·流程图
EdmundXjs1 天前
浅谈WebSocket-FLV
websocket·网络协议
金丝猴也是猿1 天前
手机抓取崩溃的log日志(安卓/ios)
websocket·网络协议·tcp/ip·http·网络安全·https·udp
网络安全天地1 天前
Flutter环境搭建
websocket·网络协议·tcp/ip·http·网络安全·https·udp