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

标签 spring jmx stomp spring-websocket

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

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

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

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

谢谢。

最佳答案

我解决了问题。

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

  1. 创建一个类,我们需要在其中注入(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。
  • enter image description here

    仅此而已。这项工作。

    关于spring - 如何使用 MBeanExporter 通过 JMX 公开 Spring WebSocketMessageBrokerStats,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38926479/

    相关文章:

    java - 如何使用 Spring WebSocket 向 STOMP 客户端发送错误消息?

    轨道 cometd 服务器和 stomp 的 PHP 代码

    spring - 订阅不工作 Spring SockJs Stomp

    java - 在独立 tomcat 上使用 Spring Boot 开发我的 REST 应用程序时获取 404

    java - 刷新 apache Camel 路由

    java - ThreadMXBean.getThreadCpuTime() 是否包括在所有状态下花费的时间,或仅包括 RUNNABLE?

    jmx - 如何将 Prometheus 与 Presto JMX 连接

    tomcat - 以编程方式访问 Tomcat 中的内置 MBean

    mysql - DataSourceInitializer 不适用于 Spring boot 1.2

    java - 捕获所有异常并返回带有消息列表的异常