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作为webapplication服务器。我需要启用JMX到WebSocketMessageBrokerStats可以工作吗?
实际上我使用Spring Framework 4.3.2在Websocket实现上有一个STOMP。 Websocket WebSocketMessageBrokerStats在控制台中每30分钟显示一次stats bean显示信息。
我使用此Object找到一些代码的独特位置是Websocket Chat,但我不明白在示例中如何使用它。
https://github.com/salmar/spring-websocket-chat
感谢。
答案 0 :(得分:1)
我解决了这个问题。
由于这是使用Spring Framework的问题,我使用了提供此框架的注释。
WebSocketMessageBrokerStats
注释创建的@EnableWebSocketMessageBroker
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();
}
}
@ManagedResource
注释的组件。
当我们部署应用程序时,将自动加载所有配置,因为我们将@EnableMBeanExport声明为一个@Configuration,然后@EnableMBeanExport类将是MBeans。全部:)
@Configuration
@EnableMBeanExport
public class WebSocketStatsJMXBeans {
@Bean
public WebSocketStatsJmxImpl jmxWebSocketStatsJmxImpl() {
return new WebSocketStatsJmxImpl();
}
}
在我的情况下,我使用Eclipse + Wilfly pluging + Wildfly 9.0。
答案 1 :(得分:0)
在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();
}
}