6

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

属性:名称大小

操作:getName() getSize()

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

例如,如果您转到IBM 信息中心并导航到 Network Deployment -> Reference -> Programming interfaces -> Mbean interfaces -> ThreadPool。它们只有列出的属性,没有操作。

使用 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()
4

2 回答 2

12

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

我不太清楚您是在谈论以编程方式从当前 JVM 内部还是从客户端远程查找 MBean。有许多 JMX 客户端库。您可能想试试我的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);
}
于 2012-04-27T14:26:12.077 回答
3

这是一个用于 ActiveMQ 的简单 JMX 示例。将来只需替换 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"));
}
于 2013-03-26T09:54:25.693 回答