简单代理

内置的简单消息代理处理来自客户端的订阅请求,将它们存储在内存中,并将消息广播到连接的具有匹配目标的客户端。代理支持路径样式的目标,包括对 Ant 样式目标模式的订阅。

应用程序还可以使用点分隔(而不是斜杠分隔)的目标。请参阅 点作为分隔符

如果配置了任务调度器,则简单代理支持 STOMP 心跳。要配置调度器,您可以声明自己的TaskScheduler bean 并通过MessageBrokerRegistry 设置它。或者,您可以使用内置 WebSocket 配置中自动声明的那个,但是,您需要@Lazy 来避免内置 WebSocket 配置和您的WebSocketMessageBrokerConfigurer 之间的循环。例如

  • Java

  • Kotlin

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {

	private TaskScheduler messageBrokerTaskScheduler;

	@Autowired
	public void setMessageBrokerTaskScheduler(@Lazy TaskScheduler taskScheduler) {
		this.messageBrokerTaskScheduler = taskScheduler;
	}

	@Override
	public void configureMessageBroker(MessageBrokerRegistry registry) {
		registry.enableSimpleBroker("/queue/", "/topic/")
				.setHeartbeatValue(new long[] {10000, 20000})
				.setTaskScheduler(this.messageBrokerTaskScheduler);

		// ...
	}
}
@Configuration
@EnableWebSocketMessageBroker
class WebSocketConfiguration : WebSocketMessageBrokerConfigurer {

	private lateinit var messageBrokerTaskScheduler: TaskScheduler

	@Autowired
	fun setMessageBrokerTaskScheduler(@Lazy taskScheduler: TaskScheduler) {
		this.messageBrokerTaskScheduler = taskScheduler
	}

	override fun configureMessageBroker(registry: MessageBrokerRegistry) {
		registry.enableSimpleBroker("/queue/", "/topic/")
			.setHeartbeatValue(longArrayOf(10000, 20000))
			.setTaskScheduler(messageBrokerTaskScheduler)

		// ...
	}
}