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

相关推荐
大只鹅2 小时前
WebSocket类明明注入了Bean,为什么报错为null
java·websocket
掉头发的王富贵10 天前
如何用WebSocket完成实时消息,实时进度条
websocket·网络协议·http
2501_9159214310 天前
后台发热、掉电严重?iOS 应用性能问题实战分析全过程
websocket·网络协议·tcp/ip·http·网络安全·https·udp
小鸡脚来咯10 天前
WebSocket快速入门
网络·websocket·网络协议
即可皕10 天前
WebSocket长连接在小程序中的实践:消息推送与断线重连机制设计
websocket·网络协议·小程序
00后程序员张10 天前
用Fiddler抓包工具优化API联调流程:与Postman、Wireshark协作实践分享
websocket·网络协议·tcp/ip·http·网络安全·https·udp
TinpeaV11 天前
Ruoyi(若依)整合websocket实现信息推送功能(消息铃铛)
网络·websocket·网络协议
海奥华211 天前
WebSocket 协议详解
网络·websocket·网络协议
2501_9159184111 天前
还原线上 WebView 异常:手机端APP远程调试
websocket·网络协议·tcp/ip·http·网络安全·https·udp
chengbo_eva11 天前
Eventsource vs Websocket vs Socket.io
网络·websocket·网络协议