Simple Broker

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

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

如果使用任务调度程序配置,简单的代理支持 STOMP heartbeats。要配置调度程序,你可以声明自己的`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)

		// ...
	}
}