Spring JMX之二:远程访问MBean
虽然最初的JMX规范提及了通过MBean进行应用的远程管理,但是它并没有定义实际的远程 访问协议或API。因此,会由JMX供应商定义自己的JMX远程访问解决方案,但这通常又是专 有的。
为了满足以标准方式进行远程访问JMX的需求,JCP(Java Community Process)制订了JSR-160: Java管理扩展远程访问API规范(Java Management Extensions Remote API Specification)。该规范 定义了JMX远程访问的标准,该标准至少需要绑定RMI和可选的JMX消息协议(JMX Messaging Protocol ,JMXMP)。
在本小节中,我们将看到Spring如何远程访问MBean。我们首先从配置Spring把 SpittleController导出为远程MBean开始,然后我们再了解如何使用Spring远程操纵 MBean。
spring通过annotation暴露MBean
在javaconfig类中开启注解
@EnableMBeanExport
涉及到三个重要的annotation:@ManagedResource @ManagedAttribute 和 @ManagedOperation。

说明:
- @ManagedResource @ManagedAttribute 和 @ManagedOperation 还有许多参数,具体使用请参考spring官方手册。( spring手册[2.5.3] ---- 20.3.4. 源代码级的元数据类型)
- @ManagedOperationParameters 是对@ManagedOperation的补充。具体看代码样例中的add1方法上的注解,然后再看图2(查看已暴露MBean的方法)的add1方法和add2的区别。添加参数说明的add1方法会显示出参数名,而add2方法则是默认的参数名p1/p2。
- 没有添加@ManagedOperation和@ManagedAttribute的方法,在图2中就没有看到,说明添加了注解的方法暴露MBean是可用的。
- @ManagedOperation和@ManagedAttribute的区别,请查看2.1的详解。
1、暴露远程MBean
使MBean成为远程对象的最简单方式是配置Spring的ConnectorServerFactoryBean:
@Bean
public ConnectorServerFactoryBean connectorServerFactoryBean() {
return new ConnectorServerFactoryBean();
}
ConnectorServerFactoryBean会创建和启动JSR-160 JMXConnectorServer。默认 情况下,服务器使用JMXMP协议并监听端口9875——因此,它将绑 定“service:jmx:jmxmp://localhost:9875”。但是我们导出MBean的可选方案并不局限于JMXMP。
根据不同JMX的实现,我们有多种远程访问协议可供选择,包括远程方法调用(Remote Method Invocation,RMI)、SOAP、Hessian/Burlap和IIOP(Internet InterORB Protocol)。为MBean绑定不同 的远程访问协议,我们仅需要设置ConnectorServerFactoryBean的serviceUrl属性。 例如,如果我们想使用RMI远程访问MBean,我们可以像下面示例这样配置:
@Bean
public ConnectorServerFactoryBean connectorServerFactoryBean() {
ConnectorServerFactoryBean csfb = new ConnectorServerFactoryBean();
csfb.setServiceUrl("service:jmx:rmi://localhost/jndi/rmi://localhost:1099/spitter");
return csfb;
}
在这里,我们将ConnectorServerFactoryBean绑定到了一个RMI注册表,该注册表监听 本机的1099端口。这意味着我们需要一个RMI注册表运行时,并监听该端口。我们可以回顾下第15章,RmiServiceExporter可以为我们自动启动一个RMI注册表。但是,我们在本示例 中不使用RmiServiceExporter,而是通过在Spring中声明RmiRegistryFactoryBean 来启动一个RMI注册表,如下面的@Bean方法所示:
@Bean
public RmiRegistryFactoryBean rmiRegistryFB() {
RmiRegistryFactoryBean rmiRegistryFB = new RmiRegistryFactoryBean();
rmiRegistryFB.setPort(1099);
return rmiRegistryFB;
}
没错!现在我们的MBean可以通过RMI进行远程访问了。
完整代码如下:
package com.dxz.mvcdemo1.web.jmx4; import static org.springframework.web.bind.annotation.RequestMethod.GET; import org.springframework.context.annotation.Bean;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.jmx.support.ConnectorServerFactoryBean;
import org.springframework.remoting.rmi.RmiRegistryFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/biz4")
@ManagedResource(objectName="spitter:name=SpittleController4") //将SpittleController导出为MBean
public class SpittleCntroller4 { // 默认每个页面的大小
public static final int DEFAULT_SPITTLES_PER_PAGE = 25; // 每页的大小
private int spittlesPerPage = DEFAULT_SPITTLES_PER_PAGE; //@ManagedOperation@ManagedOperation注解替换@ManagedAttribute注解来标注存取器方法
@ManagedAttribute //将spittlesPerPage暴露为托管属性
public int getSpittlesPerPage() {
return spittlesPerPage;
} //@ManagedOperation@ManagedOperation注解替换@ManagedAttribute注解来标注存取器方法
@ManagedAttribute //将spittlesPerPage暴露为托管属性
public void setSpittlesPerPage(int spittlesPerPage) {
this.spittlesPerPage = spittlesPerPage;
} @RequestMapping(value = "/test4", method = GET)
public String test() {
String result = spittlesPerPage + " - test()";
System.out.println(result);
return "home";
} //启动一个RMI注册表,并监听1099端口
@Bean
public RmiRegistryFactoryBean rmiRegistryFB() {
RmiRegistryFactoryBean rmiRegistryFB = new RmiRegistryFactoryBean();
rmiRegistryFB.setPort(1099);
return rmiRegistryFB;
} //将ConnectorServerFactoryBean绑定到了上面的RMI注册表,该注册表监听本机的1099端口
@Bean
public ConnectorServerFactoryBean connectorServerFactoryBean() {
ConnectorServerFactoryBean csfb = new ConnectorServerFactoryBean();
csfb.setServiceUrl("service:jmx:rmi://localhost/jndi/rmi://localhost:1099/spitter");
return csfb;
} }
启动测试:
注意:日志中RMI注册表运行了,并监听1099端口

下面先通过java自带的工具连接及测试如下,选择“远程进程”并将代码中的url=“service:jmx:rmi://localhost/jndi/rmi://localhost:1099/spitter”填入输入框中,进行连接:

连接成功后的结果如下:

但是如果没有人通过RMI访问MBean 的话,那就不值得这么做。所以现在让我们把关注点转向JMX远程访问的客户端,看看如何在 Spring中装配一个远程MBean到JMX客户端中。
2 访问远程MBean
要想访问远程MBean服务器,我们需要在Spring上下文中配置MbeanServerConnectionFactoryBean。下面的bean声明装配了一个MbeanServerConnectionFactoryBean,该bean用于访问我们在上一节中所创建的基于RMI的远程服务器。
@Bean
public MBeanServerConnectionFactoryBean connectionFactoryBean() throws MalformedURLException {
MBeanServerConnectionFactoryBean mbscfb = new MBeanServerConnectionFactoryBean();
mbscfb.setServiceUrl("service:jmx:rmi://localhost/jndi/rmi://localhost:1099/spitter");
return mbscfb;
}
顾名思义,MBeanServerConnectionFactoryBean是一个可用于创建MbeanServerConnection的工厂bean。由MBeanServerConnectionFactoryBean所生成的 MBeanServerConnection实际上是作为远程MBean服务器的本地代理。它能够以 MBeanServerConnection的形式注入到其他bean的属性中:
@Bean
public JmxClient jmxClient(MBeanServerConnection connection) {
JmxClient jmxClient = new JmxClient();
jmxClient.setMbeanServerConnection(connection);
return jmxClient;
}
JmxClient.java
package com.dxz.mvcdemo2.web; import javax.management.MBeanServerConnection; import org.springframework.stereotype.Component; @Component
public class JmxClient { MBeanServerConnection mbeanServerConnection; public JmxClient() { } public MBeanServerConnection getMbeanServerConnection() {
return mbeanServerConnection;
}
public void setMbeanServerConnection(MBeanServerConnection mbeanServerConnection) {
this.mbeanServerConnection = mbeanServerConnection;
}
测试类:
package com.dxz.mvcdemo2.web; import static org.springframework.web.bind.annotation.RequestMethod.GET; import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Set; import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jmx.support.MBeanServerConnectionFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/biz4")
public class ClientController { @Autowired
JmxClient jmxClient; @RequestMapping(value = "/client4", method = GET)
public String test() throws IOException, MalformedObjectNameException, AttributeNotFoundException,
InstanceNotFoundException, MBeanException, ReflectionException {
MBeanServerConnection connection = jmxClient.mbeanServerConnection;
int mbenaCount = connection.getMBeanCount();
System.out.println("There are " + mbenaCount + " MBeans"); ObjectName managerObjName = new ObjectName("spitter:name=SpittleController4");
Set<ObjectName> s = connection.queryNames(managerObjName, null);
for (ObjectName obj : s) {
System.out.println(obj);
} Object cronExpression = connection.getAttribute(new ObjectName("spitter:name=SpittleController4"),
"SpittlesPerPage");
System.out.println(cronExpression);
return "home";
}
@Bean
public MBeanServerConnectionFactoryBean connectionFactoryBean() throws MalformedURLException {
MBeanServerConnectionFactoryBean mbscfb = new MBeanServerConnectionFactoryBean();
mbscfb.setServiceUrl("service:jmx:rmi://localhost/jndi/rmi://localhost:1099/spitter");
return mbscfb;
} @Bean
public JmxClient jmxClient(MBeanServerConnection connection) {
JmxClient jmxClient = new JmxClient();
jmxClient.setMbeanServerConnection(connection);
return jmxClient;
}
}
结果:

3、代理MBean
Spring JMX之二:远程访问MBean的更多相关文章
- spring源码分析之spring jmx
JMX架构定义: https://docs.oracle.com/javase/8/docs/technotes/guides/jmx/overview/architecture.html Archi ...
- 开源框架是如何通过JMX来做监控的(一) - JMX简介和Standard MBean
相关文章目录: 开源框架是如何通过JMX来做监控的(一) - JMX简介和Standard MBean 开源框架是如何通过JMX来做监控的(二) - Druid连接池的监控 相信很多做Java开发的同 ...
- Spring JMX之一:使用JMX管理Spring Bean
spring中关于jmx包括几个概念: MBeanExporter: 从字面上很容易理解, 用来将一些spring的bean作为MBean暴露给MBEanServer.MBeanServerFacto ...
- spring boot / cloud (二) 规范响应格式以及统一异常处理
spring boot / cloud (二) 规范响应格式以及统一异常处理 前言 为什么规范响应格式? 我认为,采用预先约定好的数据格式,将返回数据(无论是正常的还是异常的)规范起来,有助于提高团队 ...
- Spring Data(二)查询
Spring Data(二)查询 接着上一篇,我们继续讲解Spring Data查询的策略. 查询的生成 查询的构建机制对于Spring Data的基础是非常有用的.构建的机制将截断前缀find-By ...
- spring boot / cloud (二十) 相同服务,发布不同版本,支撑并行的业务需求
spring boot / cloud (二十) 相同服务,发布不同版本,支撑并行的业务需求 有半年多没有更新了,按照常规剧本,应该会说项目很忙,工作很忙,没空更新,吧啦吧啦,相关的话吧, 但是细想想 ...
- Spring IOC(二)容器初始化
本系列目录: Spring IOC(一)概览 Spring IOC(二)容器初始化 Spring IOC(三)依赖注入 Spring IOC(四)总结 目录 一.ApplicationContext接 ...
- 深入理解Spring AOP之二代理对象生成
深入理解Spring AOP之二代理对象生成 spring代理对象 上一篇博客中讲到了Spring的一些基本概念和初步讲了实现方法,当中提到了动态代理技术,包含JDK动态代理技术和Cglib动态代理 ...
- spring AOP 之二:@AspectJ注解的3种配置
@AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...
随机推荐
- java中读取配置文件
若是Javaweb项目,项目运行于tomcat或其他容器时,可以使用下面方式来获取文件的输入流 1.当属性文件放在src下面时 InputStream is = Thread.currentThrea ...
- TASKER 定制你的手机让它在办公室时屏幕 30 分钟才灭
TASKER 定制你的手机让它在办公室时屏幕 30 分钟才灭 因为到的办公室,手机一直是充电的,不想屏幕太快关关掉,所以使用 TASKER 做了一个条件. 当 WIFI 连接到公司 WIFI 且充电中 ...
- oracle 之 安装10.2.0.1 且 升级到 10.2.0.4
一. centos 6.5 安装 oracle 10.2.0.1 1.安装操作系统,选择桌面环境 2.配置本地yum源 , 可参考 http://blog.csdn.net/zhang12345645 ...
- Visual Studio Community 2013 中文语言包-离线安装版
vs_langpack.exe /layout 命令运行或者批处理运行. 转自:http://www.tuicool.com/articles/uMzqAnA 现成安装包下载地址:链接: http:/ ...
- JSTL-c:forEach标签详解
c:forEach基本格式: <c:forEach var="每个变量名字" items="要迭代的list" varStatus="每个对象的 ...
- 【HDU】4352 XHXJ's LIS(数位dp+状压)
题目 传送门:QWQ 分析 数位dp 状压一下现在的$ O(nlogn) $的$ LIS $的二分数组 数据小,所以更新时直接暴力不用二分了. 代码 #include <bits/stdc++. ...
- 符合RESTful规范的API
统一使用的utils,serializers: class BaseResponse: def __init__(self): self.code = 1000 self.data = None se ...
- 简单的自动化测试模型(python+selenium)
刚接触自动化测试,由于没有编程语言的基础,是搞不懂代码里面的函数.封装.包以及其他概念,只是了解字符串.数组.元组及字典这种最基本的名词,更不懂自动化测试框架了. ...
- http协议再复习(二)
HTTP和HTTPS HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的方法. HTTPS(Hypertext Transfer ...
- oracle忘记sys及system密码
一.忘记除SYS.SYSTEM用户之外的用户的登录密码. 用SYS (或SYSTEM)用户登录. CONN SYS/PASS_WORD AS SYSDBA; 使用如下语句修改用户的密码. ALTER ...