官方介紹:http://docs.oracle.com/javase/1.5.0/docs/guide/jmx/overview/JMXoverviewTOC.htmlhtml
JMX(Java Management Extensions) 是來管理網絡,設備,應用程序等資源,它描述了一個可擴展的管理體系結構,而且提供了 JMX API 和一些預約義的 java 管理服務。java
經過JMX能夠輕鬆地爲應用程序添加管理功能,便可以在儘量少的改變原有系統的代碼基礎上實現對原系統的管理。windows
JMX Technology Architecture瀏覽器
Level安全
Description網絡
Instrumentationoracle
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.app
Agentless
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.socket
Remote Management
Protocol adaptors and standard connectors make a JMX agent accessible from remote management applications outside the agent’s Java Virtual Machine (JVM).
Managing Resources Remotely
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.
得到 MBeanServer 的實例
MBeanServer是MBean的容器,能夠經過多種方式得到MBeanServer的實例,如:
MBeanServer server = MBeanServerFactory.createMBeanServer();
或
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
其中,經過下面的方式得到的實例能在jconsole中使用,而上面的不能。
建立 MBean
爲了可以管理 Web 應用的資源,首先要使資源可以被管理,按照 JMX 規範的要求,咱們將資源封裝成 MBean,實際上也就是爲 Web 應用添加可管理性。
MBean就是遵照JMX規範的普通Class。
JMX中經常使用的概念:
Manageable resource:
可被管理的資源能夠是應用程序,設備或者存在的可以被java程序所訪問或者包裝的實體。經過JMX能夠管理這些資源。應用程序可以暴露本身的組件,API或者附加的資源,使得JMX可以管理應用程序。可被管理的資源甚至能夠是網絡上的設備,例如打印機。可被管理的資源做爲一個實體被JMX MBean所管理。
MBean:
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的之間的關係,動態加載類,監控服務,做爲計時器。
Management application
一個management application能夠是任何的用戶程序,用於和任意多的JMX agent之間創建接口。對於一些設計好的符合JMX技術的management appliction,JMX agents可以創建和該management application的聯繫,JMX agents也可以創建和那些先前沒有考慮用JMX技術的management application創建聯繫。
傳輸和安全性
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接口
Java代碼 收藏代碼
public interface ControllerMBean {
//屬性
public void setName(String name);
public String getName();
//操做
/**
* 獲取當前信息
* @return
*/
public String status();
public void start();
public void stop();
}
2.而後實現這個接口
Java代碼 收藏代碼
public class Controller implements ControllerMBean {
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
private String name;
public String status() {
return "this is a Controller MBean,name is " + this.name;
}
public void start() {
// TODO Auto-generated method stub
}
public void stop() {
// TODO Auto-generated method stub
}
}
3.在被管理的程序中加入這個管理對象
Java代碼 收藏代碼
import java.lang.management.ManagementFactory;
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.swing.JDialog;
import jmx.Controller;
import jmx.ControllerMBean;
import com.sun.jdmk.comm.HtmlAdaptorServer;
public class Main {
/**
* @param args
* @throws NullPointerException
* @throws MalformedObjectNameException
* @throws NotCompliantMBeanException
* @throws MBeanRegistrationException
* @throws InstanceAlreadyExistsException
*/
public static void main(String[] args)
throws InstanceAlreadyExistsException, MBeanRegistrationException,
NotCompliantMBeanException, MalformedObjectNameException,
NullPointerException {
//得到MBeanServer實例
// MBeanServer mbs = MBeanServerFactory.createMBeanServer();//不能在jconsole中使用
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();//可在jconsole中使用
//建立MBean
ControllerMBean controller = new Controller();
//將MBean註冊到MBeanServer中
mbs.registerMBean(controller, new ObjectName("MyappMBean:name=controller"));
//建立適配器,用於可以經過瀏覽器訪問MBean
HtmlAdaptorServer adapter = new HtmlAdaptorServer();
adapter.setPort(9797);
mbs.registerMBean(adapter, new ObjectName(
"MyappMBean:name=htmladapter,port=9797"));
adapter.start();
//因爲是爲了演示保持程序處於運行狀態,建立一個圖形窗口
javax.swing.JDialog dialog = new JDialog();
dialog.setName("jmx test");
dialog.setVisible(true);
}
}
運行上面的程序
4.在windows下進入控制檯(win+r->cmd),而後輸入jconsole命令,稍等片刻打開jconsole的圖形界面,在「本地」中選擇剛纔運行的程序,而後進入MBean面板頁,便可看到MyappMBean一項,下面就是具體的MBean,可展開這些MBean對其操做。
因爲上面的程序啓用了html協議適配器,所以能夠在瀏覽器中執行如同jconsole的操做,在瀏覽器中輸入:http://localhost:9797便可
後面再研究connector有關身份認證的問題,這樣就能以安全的方式鏈接到MBeanServer上了。