Spring MX4J JMX基于注解的环境集成

时间:2022-06-01 20:37:05

Spring MX4J JMX集成

最近在看jmx,网上的资料很多,但是能够拿来即用的不多,这是我在本地经过多次测试的一个可行方案(基于注解):

maven依赖

<dependency>
<groupId>mx4j</groupId>
<artifactId>mx4j</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>mx4j</groupId>
<artifactId>mx4j-impl</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mx4j</groupId>
<artifactId>mx4j-jmx</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>mx4j</groupId>
<artifactId>mx4j-remote</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>mx4j</groupId>
<artifactId>mx4j-rimpl</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mx4j</groupId>
<artifactId>mx4j-rjmx</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mx4j</groupId>
<artifactId>mx4j-tools</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 这个当使用HtmlAdaptorServer时放开
<dependency>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
<version>1.2.1</version>
</dependency>
-->

暴露的接口

package com.jzh.springboot.util.jmx;

import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedResource;

/**
* Created by jzh on 2017/4/5.
*/

@ManagedResource
public class MyMBean {

private String name;

@ManagedAttribute
public String getName() {
return name;
}

@ManagedAttribute
public void setName(String name) {
this.name = name;
}
}

注:spring通过annotation暴露MBean
涉及到三个重要的annotation:@ManagedResource @ManagedAttribute 和 @ManagedOperation

@ManagedResource:将类的所有实例标识为JMX受控资源
@ManagedAttribute:将getter或者setter标识为部分JMX属性
@ManagedOperation:将方法标识为JMX操作

其他:
@ManagedOperationParameter和@ManagedOperationParameters:定义操作参数说明

@ManagedOperationParameters({    
@ManagedOperationParameter(name = "x", description = "The first number"),
@ManagedOperationParameter(name = "y", description = "The second number")})
public int add_1(int x, int y) {
return x + y;
}

spring-jmx.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


<bean id="monitorMBeanManager" class="com.jzh.springboot.util.jmx.MyMBean"/>

<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean" />

<bean id="jmxAttributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>

<bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
<property name="attributeSource">
<ref bean="jmxAttributeSource" />
</property>
</bean>

<bean id="xsltProcessor" class="mx4j.tools.adaptor.http.XSLTProcessor"/>

<bean id="httpAdaptor" class="mx4j.tools.adaptor.http.HttpAdaptor">
<property name="processor">
<ref bean="xsltProcessor" />
</property>
<property name="port">
<value>9999</value>
</property>
</bean>

<!--<bean id="adaptorServer" class="com.sun.jdmk.comm.HtmlAdaptorServer">
<property name="port">
<value>9998</value>
</property>
</bean>-->


<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="assembler" ref="assembler" />
<property name="beans">
<map>
<entry key="bean:name=monitorMBeanManager" value-ref="monitorMBeanManager" />
<entry key="mx4j:name=HttpAdaptor" value-ref="httpAdaptor" />
</map>
</property>
<property name="server" ref="mbeanServer"/>
</bean>

</beans>

测试:启动项目
可以配置一个当spring项目初始化成功之后执行的某个方法,在该方法中执行httpAdaptor.start();该语句

为了简单说明,我在项目中配置了一个url访问来执行这个方法

@RequestMapping("/hello")
public String hello() {
try {
httpAdaptor.start();
} catch (IOException e) {
e.printStackTrace();
}
return "hello";
}

先调用http://localhost:8080/hello,然后再执行http://localhost:9999/就会看到我们想看到的结果了

附:可以在spring容器加载之后执行start方法

import mx4j.tools.adaptor.http.HttpAdaptor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
* Created by Administrator on 2017/4/10.
*/

@Component
public class InstantiationTracingBeanPostProcessor implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
HttpAdaptor httpAdaptor = (HttpAdaptor) applicationContext.getBean("httpAdaptor");
if(!httpAdaptor.isActive()) {
try {
httpAdaptor.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}