javax.management.modelmbean

提供了 ModelMBean類的定義。Model MBean 是充當管理接口和底層託管資源之間橋樑的 MBean。管理接口和託管資源都指定為 Java 對象。不同管理接口和託管資源可以多次重用相同的 Model MBean 實現,它可以提供諸如持久性和快取這樣常見的功能。

軟體包名稱

javax.management.modelmbean

主要描述

Model MBean 實現 ModelMBean 接口。它是一個 DynamicMBean 接口,該接口的 getMBeanInfo 方法返回實現 ModelMBeanInfo 的對象。
每個 MBean 都有一個 MBeanInfo,它包含關於 MBean 本身、其屬性、操作、構造方法和通知的信息。Model MBean 用 Descriptor 來擴充此 MBeanInfo,這裡 Descriptor 通過 (key,value) 對的形式對其他信息進行編碼。通常,Descriptor 是 DescriptorSupport 的實例。
RequiredModelMBean 類提供了標準的 Model MBean 實現。
以下顯示了一個 Model MBean 的示例,它使我們可以通過 MBean 伺服器管理 HashMap 的 get 方法。其他方法不能通過 MBean 伺服器進行管理。這裡 HashMap 沒有特殊的地方。任何公共類的公共方法都可以通過相同方式進行管理。
import java.lang.reflect.Method;
import java.util.HashMap;
import javax.management.*;
import javax.management.modelmbean.*;
// ...
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
// The MBean Server
HashMap map = new HashMap();
// The resource that will be managed
// Construct the management interface for the Model MBean
Method getMethod = HashMap.class.getMethod("get", new Class[] {Object.class});
ModelMBeanOperationInfo getInfo =
new ModelMBeanOperationInfo("Get value for key", getMethod);
ModelMBeanInfo MMBi =
new ModelMBeanInfoSupport(HashMap.class.getName(),
"Map of keys and values",
null,// no attributes
null,// no constructors
new ModelMBeanOperationInfo[] ,
null); // no notifications
// Make the Model MBean and link it to the resource
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(map, "ObjectReference");
// Register the Model MBean in the MBean Server
ObjectName mapName = new ObjectName(":type=Map,name=whatever");
mbs.registerMBean(mmb, mapName);
// Resource can evolve independently of the MBean
map.put("key", "value");
// Can access the "get" method through the MBean Server
mbs.invoke(mapName, "get", new Object[] {"key"}, new String[] {Object.class.getName()});
// returns "value"
包規範
JMX API, version 1.2 規範
從以下版本開始:
1.5

相關詞條

相關搜尋

熱門詞條

聯絡我們