Spring 文档说,当您使用 @EnableWebSocketMessageBroker 注释时,会创建一个 WebSocketMessageBrokerStats 类型的 bean。这个bean可以通过Spring的MBeanExporter导出到JMX以便在运行时查看,例如通过JDK的jconsole(或VisualVM)。我不知道如何创建 Mbean。
我发现@EnableMBeanExport
相当于使用<context:mbean-export>
在另一个 stackoverflow 链接中,我发现我需要做一些类似于下一个的事情:
@Configuration
@EnableMBeanExport
public class SpringConfiguration {
@Bean
protected CountingHttpInterceptor countingHttpInterceptor() {
return new CountingHttpInterceptor();
}
}
Exporting Spring @Bean objects using JMX
然后我认为我需要声明一个 bean 如下:
@Configuration
@EnableMBeanExport
public class SpringConfiguration {
@Bean
protected WebSocketMessageBrokerStats webSocketMessageBrokerStats() {
return new WebSocketMessageBrokerStats();
}
}
但这不起作用。
我发现我也需要在 JVM 中启用 JMX。 我使用 WildFly 9.0 作为网络应用程序服务器。我需要启用 JMX 才能使 WebSocketMessageBrokerStats 可以工作吗?
实际上,我有一个使用 Spring Framework 4.3.2 的 STOMP over Websocket 实现。 Websocket WebSocketMessageBrokerStats 向我显示统计 bean 在控制台中每 30 分钟显示一次信息。
我发现一些使用此对象的代码的独特之处是 Websocket Chat,但我不明白如何在示例中使用它。
https://github.com/salmar/spring-websocket-chat
谢谢。
最佳答案
我解决了问题。
由于这是使用 Spring 框架的问题,因此我使用了提供此框架的注释。
- 创建一个类,我们需要在其中注入(inject)
@EnableWebSocketMessageBroker
注释创建的WebSocketMessageBrokerStats
bean,以监视 Websockets 连接。我们需要用@ManagedResource
注释这个类。
代码:
package mx.config.ws;
@ManagedResource(objectName = "Examples:type=JMX,name=Resource")
public class WebSocketStatsJmxImpl implements WebSocketStatsJmx {
public WebSocketStatsJmxImpl() {
super();
System.out.println("WebSocketStatsJmxImpl::Constructor");
}
WebSocketMessageBrokerStats webSocketMessageBrokerStats;
public WebSocketMessageBrokerStats getWebSocketMessageBrokerStats() {
return webSocketMessageBrokerStats;
}
@Autowired
public void setWebSocketMessageBrokerStats(WebSocketMessageBrokerStats webSocketMessageBrokerStats) {
this.webSocketMessageBrokerStats = webSocketMessageBrokerStats;
}
@ManagedAttribute(description="Get stats about WebSocket sessions.") // defines an attribute of an MBean
public String getWebSocketSessionStatsInfo(){
return webSocketMessageBrokerStats.getWebSocketSessionStatsInfo();
}
@ManagedAttribute(description="Get stats about STOMP-related WebSocket message processing.")
public String getStompSubProtocolStatsInfo(){
return webSocketMessageBrokerStats.getStompSubProtocolStatsInfo();
}
@ManagedAttribute(description="Get stats about STOMP broker relay (when using a full-featured STOMP broker).")
public String getStompBrokerRelayStatsInfo(){
return webSocketMessageBrokerStats.getStompBrokerRelayStatsInfo();
}
@ManagedAttribute(description="Get stats about the executor processing incoming messages from WebSocket clients.")
public String getClientInboundExecutorStatsInfo(){
return webSocketMessageBrokerStats.getClientInboundExecutorStatsInfo();
}
@ManagedAttribute(description="Get stats about the executor processing outgoing messages to WebSocket clients.")
public String getClientOutboundExecutorStatsInfo(){
return webSocketMessageBrokerStats.getClientOutboundExecutorStatsInfo();
}
@ManagedAttribute(description="Get stats about the SockJS task scheduler.")
public String getSockJsTaskSchedulerStatsInfo(){
return webSocketMessageBrokerStats.getSockJsTaskSchedulerStatsInfo();
}
}
- 创建一个 @Configuration,我们将在其中创建一个 Spring Bean,并在其中实例化 WebSocketStatsJmxImpl 类。我们必须记住添加
@EnableMBeanExport 在某个地方向 JMX 代理注册所有用
@ManagedResource
注释的组件。 当我们部署应用程序时,将自动加载所有配置,并且由于我们已将 @EnableMBeanExport 声明到一个 @Configuration 中,因此 @EnableMBeanExport 类将是 MBean。
就这些了:)
@Configuration
@EnableMBeanExport
public class WebSocketStatsJMXBeans {
@Bean
public WebSocketStatsJmxImpl jmxWebSocketStatsJmxImpl() {
return new WebSocketStatsJmxImpl();
}
}
就我而言,我使用 Eclipse + Wilfly 插件 + Wildfly 9.0。
- 打开 Java VisualVM 并查找“org.jboss.modules.main”,转到 MBeans 选项卡并在本例中查找示例 Mbean。
仅此而已。这项工作。
关于spring - 如何使用 MBeanExporter 通过 JMX 公开 Spring WebSocketMessageBrokerStats,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38926479/