Simple Broker
内置的简单消息代理会处理来自客户端的订阅请求,将其存储在内存中,并将消息广播到具有匹配目的地的已连接客户端。代理支持类似于路径的目标,包括对 Ant 风格目标模式的订阅。
The built-in simple message broker handles subscription requests from clients, stores them in memory, and broadcasts messages to connected clients that have matching destinations. The broker supports path-like destinations, including subscriptions to Ant-style destination patterns.
应用程序还可以使用点分隔(而不是斜杠分隔)的目标。请参阅 Dots as Separators。 |
Applications can also use dot-separated (rather than slash-separated) destinations. See Dots as Separators. |
如果使用任务调度程序配置,简单的代理支持 STOMP heartbeats。要配置调度程序,你可以声明自己的`TaskScheduler`bean 并通过`MessageBrokerRegistry`设置它。或者,你可以使用内置 WebSocket 配置中自动声明的那个,但是,你需要`@Lazy`来避免内置 WebSocket 配置和你自己的`WebSocketMessageBrokerConfigurer`之间的循环。例如:
If configured with a task scheduler, the simple broker supports
STOMP heartbeats.
To configure a scheduler, you can declare your own TaskScheduler
bean and set it through
the MessageBrokerRegistry
. Alternatively, you can use the one that is automatically
declared in the built-in WebSocket configuration, however, you’ll need @Lazy
to avoid
a cycle between the built-in WebSocket configuration and your
WebSocketMessageBrokerConfigurer
. For example:
-
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)
// ...
}
}