JMX理解与实例

JMX(Java Management Extensions) 是来管理网络,设备,应用程序等资源,它描述了一个可扩展的管理体系结构,并且提供了 JMX API 和一些预定义的 java 管理服务。


通过JMX可以轻松地为应用程序添加管理功能,即可以在尽可能少的改变原有系统的代码基础上实现对原系统的管理。





[b]JMX Technology Architecture[/b]

Level Description
Instrumentation Resources, such as applications, devices, or services, are instrumented using Java objects called Managed Beans (MBeans). MBeans expose their management interfaces, composed of attributes and operations, through a JMX agent for remote management and monitoring.
Agent The main component of a JMX agent is the MBean server. This is a core managed object server in which MBeans are registered. A JMX agent also includes a set of services for handling MBeans. JMX agents directly control resources and make them available to remote management agents.
Remote Management Protocol adaptors and standard connectors make a JMX agent accessible from remote management applications outside the agent’s Java Virtual Machine (JVM).




[b]Managing Resources Remotely[/b]

JMX API instrumentation can be accessed in many different ways, either through existing management protocols such as the Simple Network Management Protocol (SNMP), or through proprietary protocols. The MBean server relies on protocol adaptors and connectors to make a JMX agent accessible from management applications outside the agent’s Java Virtual Machine (JVM).



Each adaptor provides a view through a specific protocol of all MBeans registered in the MBean server. For example, an HTML adaptor could display an MBean in a Web browser.



Connectors provide a manager-side interface that handles the communication between manager and JMX agent. Each connector provides the same remote management interface though a different protocol. When a remote management application uses this interface, it can connect to a JMX agent transparently through the network, regardless of the protocol.



JMX technology provides a standard solution for exporting JMX API instrumentation to remote applications, based on Remote Method Invocation (RMI). In addition, the JMX Remote API defines an optional protocol based directly on TCP sockets, called the JMX Messaging Protocol (JMXMP). An implementation of the JMX Remote API does not have to support this optional protocol. The J2SE platform, version 5.0, does not include the optional protocol. See Appendix A, ”JMX Technology Versions”for further information.



The JMX Remote API specification describes how you can advertise and find JMX agents using existing discovery and lookup infrastructures. Examples of how to do this are provided and are described in the Java Management Extensions (JMX) Technology Tutorial. The specification does not define its own discovery and lookup service. The use of existing discovery and lookup services is optional: alternatively you can encode the addresses of your JMX API agents in the form of URLs, and communicate these URLs to the manager.



[b]获得 MBeanServer 的实例[/b]

MBeanServer是MBean的容器,可以通过多种方式获得MBeanServer的实例,如:

MBeanServer server = MBeanServerFactory.createMBeanServer();




MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();




其中,通过下面的方式获得的实例能在jconsole中使用,而上面的不能。



创建 MBean

为了能够管理 Web 应用的资源,首先要使资源能够被管理,按照 JMX 规范的要求,我们将资源封装成 MBean,实际上也就是为 Web 应用添加可管理性。

MBean就是遵守JMX规范的普通Class。





JMX中常用的概念:

[b]Manageable resource:[/b]

可被管理的资源可以是应用程序,设备或者存在的能够被java程序所访问或者包装的实体。通过JMX可以管理这些资源。应用程序能够暴露自己的组件,API或者附加的资源,使得JMX能够管理应用程序。可被管理的资源甚至可以是网络上的设备,例如打印机。可被管理的资源作为一个实体被JMX MBean所管理。



[b]MBean:[/b]

MBean(managed bean)是一个Java类,符合JXM specification所规定的命名和继承规范。实例化的MBeans是Java对象,其中所暴露出来的接口(management interface)能够操作和访问manageable resources。这些接口是由MBean的属性和操作组成。

Management application通过访问MBean来访问属性和调用操作。MBean分三种类型:Standard,Dynamic和Model MBean.每一种类型都是针对于特定的manageable resource来使用的。



JMX agent:

JMX agent是一个Java process,能够为管理MBean的集合提供服务,是MBean Server的容器。这些服务可以是建立MBean的之间的关系,动态加载类,监控服务,作为计时器。



[b]Management application[/b]

一个management application可以是任何的用户程序,用于和任意多的JMX agent之间建立接口。对于一些设计好的符合JMX技术的management appliction,JMX agents能够建立和该management application的联系,JMX agents也能够建立和那些先前没有考虑用JMX技术的management application建立联系。



[b]传输和安全性[/b]

JMX 指定了在 MBeanServer 和 JMX 客户之间通信所使用的协议,协议可以在各种传输机制上运行。可以使用针对本地连接的内置传输,及通过 RMI、socket 或 SSL 的远程传输(可以通过 JMX Connector API 创建新的传输)。认证是由传输执行的;本地传输允许用相同的用户 ID 连接到运行在本地系统上的 JVM;远程传输可以用口令或证书进行认证。本地传输在 Java 6 下默认就是启用的。要在 Java 5.0 下启用它,需要在 JVM 启动时定义系统属性 com.sun.management.jmxremote。“Monitoring and Management using JMX” 这份文档(请参阅参考资料)描述了启用和配置传输的配置步骤。

The MBean server relies on protocol adaptors and connectors to make a JMX agent accessible from management applications outside the agent’s Java Virtual Machine (JVM).





下面给出一个JMX开发示例程序:



1.MBean接口

package study.test.jmx;

public interface HelloWorldMBean {
public String getName();
public void setName(String name);
public void printHello();
public void printHello(String whoName);
}

2. 被管理的类
被管理的类需要实现相应的MBean接口,通过MBean接口中的方法来被管理。

package study.test.jmx;

public class HelloWorld implements HelloWorldMBean {

private String name;

public String getName() {
return name;
}

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

public void printHello() {
System.out.println("Hello World, " + name);
}

public void printHello(String whoName) {
System.out.println("Hello , " + whoName);
}

}

3. 创建一个Agent类
Agent其实实现的是类似于Server的功能,他负责把JMX服务绑定到相应的URL,并将我们上面创建的被管理的类绑定到其中,使得外部可以访问。

package study.test.jmx;

import java.io.IOException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

import com.sun.jdmk.comm.HtmlAdaptorServer;

public class HelloWorldAgent {

public static void main(String[] args) throws MalformedObjectNameException,
NullPointerException, InstanceAlreadyExistsException,
MBeanRegistrationException, NotCompliantMBeanException, IOException {

int rmiPort = 1099;
String jmxServerName = "TestJMXServer";

// jdkfolder/bin/rmiregistry.exe 9999
Registry registry = LocateRegistry.createRegistry(rmiPort);

MBeanServer mbs = MBeanServerFactory.createMBeanServer(jmxServerName);
//MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

HtmlAdaptorServer adapter = new HtmlAdaptorServer();
ObjectName adapterName;
adapterName = new ObjectName(jmxServerName + ":name=" + "htmladapter");
adapter.setPort(8082);
adapter.start();
mbs.registerMBean(adapter, adapterName);

ObjectName objName = new ObjectName(jmxServerName + ":name=" + "HelloWorld");
mbs.registerMBean(new HelloWorld(), objName);

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + rmiPort + "/" + jmxServerName);
System.out.println("JMXServiceURL: " + url.toString());
JMXConnectorServer jmxConnServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
jmxConnServer.start();

}
}

说明:
1. Agent实现中的35行到40行为MBeanServer添加了一个htmladapter,这样我们就可以通过网页的方式来进行管理。
比如说上面我们实行的Agent,我们就可以通过http://localhost:8082来对程序进行管理。这里的8082就是htmladapter中设置的端口。

Client 端调用

package study.test.jmx;

import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class HelloClient {

public static void main(String[] args) {

try {
JMXServiceURL url = new JMXServiceURL(
"service:jmx:rmi:///jndi/rmi://localhost:1099/TestJMXServer");
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

ObjectName mbeanName = new ObjectName("TestJMXServer:name=HelloWorld");
HelloWorldMBean mbeanProxy = JMX.newMBeanProxy(mbsc, mbeanName,
HelloWorldMBean.class, true);

mbeanProxy.setName("Jack");
mbeanProxy.printHello();

} catch (Exception e) {
e.printStackTrace();
}

}
}


转载自 <a href="http://blog.ticeng.com/238.html" title="JMX理解与实例" rel="bookmark">JMX理解与实例 | Tints小站</a>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值