2016-08-12 85 views
1

Spring文檔說,當您使用@EnableWebSocketMessageBroker註釋時,將創建一個WebSocketMessageBrokerStats類型的bean。這個bean可以通過Spring的MBeanExporter導出到JMX,以便在運行時查看,例如通過JDK的jconsole(或VisualVM)。我不知道如何創建Mbean。如何使用MBeanExporter通過JMX公開Spring WebSocketMessageBrokerStats

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

我發現@EnableMBeanExport等同用<context:mbean-export>

在另一個計算器鏈接我發現,我需要做一些類似於下一個:

@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的Websocket實現了一個STOMP。 WebSocket WebSocketMessageBrokerStats每隔30分鐘在控制檯中向我顯示統計信息bean顯示信息。

我發現一些代碼使用這個對象的獨特地方是一個WebSocket聊天,但我不明白如何在示例中使用它。

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

謝謝。

回答

1

我解決了這個問題。

由於這是使用Spring框架的問題,我使用了提供此框架的註釋。

  1. 創建一個類,我們需要注入WebSocketMessageBrokerStats豆的@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(); 
    } 
} 
  • 創建@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。

    enter image description here

    這就是所有。這項工作。

    相關問題