【问题标题】:How can Spring WebSocketMessageBrokerStats be exposed via JMX using MBeanExporter如何使用 MBeanExporter 通过 JMX 公开 Spring WebSocketMessageBrokerStats
【发布时间】:2016-08-12 21:07:36
【问题描述】:

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>

在另一个 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 作为 web 应用程序服务器。我需要启用 JMX 到 WebSocketMessageBrokerStats 才能工作?

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

我发现一些使用此对象的代码的唯一地方是 Websocket Chat,但我不明白在示例中如何使用它。

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

谢谢。

【问题讨论】:

    标签: spring jmx stomp spring-websocket


    【解决方案1】:

    我解决了这个问题。

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

    1. 创建一个类,我们需要在其中注入WebSocketMessageBrokerStats bean,@EnableWebSocketMessageBroker 注释创建来监控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();
        }
    }
    
    1. 创建一个@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 pluging + Wildfly 9.0。

    1. 打开 Java VisualVM 并查找“org.jboss.modules.main”,转到 MBeans 选项卡并在本例中查找示例 Mbean。

    仅此而已。这项工作。

    【讨论】:

      【解决方案2】:

      在 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 类:

      @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();
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2016-02-18
        • 2013-06-20
        • 1970-01-01
        • 2020-10-06
        • 2011-08-21
        • 2017-09-26
        • 2014-03-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多