如何以编程方式检查JMX MBean操作和属性?

时间:2012-04-26 23:04:52

标签: java jmx websphere-7 mbeans

假设我们有一个具有以下属性和操作的MBean。

属性: 名称 大小

操作: 的getName() 的getSize()

有没有办法以编程方式检查属性和操作?我一直在使用IBM WebSphere MBeans,他们的文档也不是很好。

例如,如果您转到IBMs Infocenter并导航到网络部署 - >参考 - >编程接口 - > Mbean接口 - >线程池。它们只列出了属性而没有操作。

使用WebSphere wsadmin工具,我实际上可以检查以查看操作和属性。我想知道是否有办法对所有MBean这样做。

wsadmin>print Help.attributes(object)
Attribute                       Type                            Access
name                            java.lang.String                RO
maximumSize                     int                             RW
minimumSize                     int                             RW
inactivityTimeout               long                            RW
growable                        boolean                         RW
stats                           javax.management.j2ee.statistics.Stats  RO

wsadmin>print Help.operations(object)
Operation
java.lang.String getName()
int getMaximumPoolSize()
void setMaximumPoolSize(int)
int getMinimumPoolSize()
void setMinimumPoolSize(int)
long getKeepAliveTime()
void setKeepAliveTime(long)
boolean isGrowAsNeeded()
void setGrowAsNeeded(boolean)
javax.management.j2ee.statistics.Stats getStats()

2 个答案:

答案 0 :(得分:10)

我无法确定您是否正在讨论以编程方式从当前JVM内部或从客户端远程查找MBean。有许多JMX客户端库。我写的那个可以在这里找到:

  

http://256stuff.com/sources/simplejmx/

使用我的代码,您可以执行以下操作:

JmxClient client = new JmxClient(hostName, port);
Set<ObjectName> objectNames = getBeanNames() 
for (ObjectName name : objectNames) {
    MBeanAttributeInfo[] attributes = getAttributesInfo(name);
    MBeanOperationInfo[] operations = getOperationsInfo(name);
}

如果您询问当前的JVM,那么您应该能够以这种方式从内部bean获取bean信息:

MBeanServer server = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objectNames = server.queryNames(null, null);
for (ObjectName name : objectNames) {
    MBeanInfo info = server.getMBeanInfo(name);
}

答案 1 :(得分:3)

以下是使用简单JMX for ActiveMQ的示例。仅仅替换activeMQ值对将来的某些人有用:

String brokerName = "AMQBroker";
String username = "";
String password = "";
String hostname = "localhost";
int port = 1099;

Map<String, Object> env = new HashMap<String, Object>();
if (username != null || password != null) {
    String[] credentials = new String[] { username, password };
    env.put("jmx.remote.credentials", credentials);
}

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + hostname + ":" + port + "/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
MBeanServerConnection conn = jmxc.getMBeanServerConnection();

// here is example for Type=Broker, can be different like
// "org.apache.activemq:BrokerName=" + brokerName + ",Type=Connection,ConnectorName=openwire,Connection=*"
// "org.apache.activemq:BrokerName=" + brokerName + ",*,Type=NetworkBridge" or same for Queue, Topic, Subscription
ObjectName name = new ObjectName("org.apache.activemq:BrokerName=" + brokerName + ",Type=Broker");
Set<ObjectName> queryNames = conn.queryNames(name, null);
// here is set with one element, but can be more depending on ObjectName query
for (ObjectName objectName : queryNames) {
    System.out.println(objectName.getCanonicalName());
    // use attribute you can be interested in
    System.out.println(conn.getAttribute(objectName, "Slave"));
}