Spring 如何使用 MBeanExporter 通过 JMX 公开 WebSocketMessageBrokerStats

How can Spring WebSocketMessageBrokerStats be exposed via JMX using MBeanExporter

Spring 文档说,当您使用 @EnableWebSocketMessageBroker 注释时,会创建一个 WebSocketMessageBrokerStats 类型的 bean。这个 bean 可以通过 Spring 的 MBeanExporter 导出到 JMX 以便在运行时查看,例如通过 JDK 的 jconsole(或 VisualVM)。我不知道如何创建 Mbean。

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-stomp-stats

我发现 @EnableMBeanExport 相当于使用 <context:mbean-export>

在另一个 Whosebug 中 link 我发现我需要做一些类似于下一个的事情:

@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 作为网络应用程序服务器。我需要为 WebSocketMessageBrokerStats 启用 JMX 才能工作?

实际上我有一个 STOMP over Websocket 实现,使用 Spring Framework 4.3.2。 Websocket WebSocketMessageBrokerStats 在控制台中每 30 分钟向我显示一次统计 bean 显示信息。

我发现一些使用此对象的代码的独特之处是 Websocket 聊天,但我不明白如何在示例中使用它。

https://github.com/salmar/spring-websocket-chat

谢谢。

我解决了问题。

由于这是使用 Spring 框架的问题,我使用了提供此框架的注释。

  1. 创建一个 class ,我们需要在其中注入 WebSocketMessageBrokerStats bean,@EnableWebSocketMessageBroker 注释创建以监视 websockets 连接。我们需要用 @ManagedResource.
  2. 注释这个 class

代码:

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();
    }
}
  1. 创建一个@Configuration,我们将在其中创建一个Spring Bean,我们将在其中实例化WebSocketStatsJmxImpl class。我们必须记住添加 @EnableMBeanExport 在某个地方向 JMX 代理注册所有用 @ManagedResource 注释的组件。 当我们部署应用程序时,将自动加载所有配置,并且由于我们将 @EnableMBeanExport 声明为一个 @Configuration 然后 @EnableMBeanExport classes 将是 MBean。

就这些了:)

@Configuration
@EnableMBeanExport
public class WebSocketStatsJMXBeans {
    @Bean
    public WebSocketStatsJmxImpl jmxWebSocketStatsJmxImpl() {
        return new WebSocketStatsJmxImpl();
    }
}

在我的例子中,我使用 Eclipse + Wilfly 插件 + Wildfly 9.0。

  1. 打开 Java VisualVM 并查找 "org.jboss.modules.main",转到 MBean 选项卡并在本例中查找 Examples Mbean。

就是这样。这项工作。

在 Spring 5 和 Spring Boot 2 中可以做得更简单。

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

Java JMX class 导出:

@Component
@ManagedResource(objectName = "Examples:type=JMX,name=Resource")                
public class YourClassToExport {

    private WebSocketMessageBrokerStats webSocketMessageBrokerStats;

    public WebSocketMessageBrokerStats getWebSocketMessageBrokerStats() {
        return webSocketMessageBrokerStats;
    }

    @Autowired
    public void setWebSocketMessageBrokerStats(WebSocketMessageBrokerStats webSocketMessageBrokerStats) {
        this.webSocketMessageBrokerStats = webSocketMessageBrokerStats; 
    }

    // attributes of an MBean

    @ManagedAttribute(description="Get stats about WebSocket sessions.")                            
    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();
    }

}