JMXServiceURL url = new JMXServiceURL(jmxUrl);
JMXConnector connection = JMXConnectorFactory.connect(url);
MBeanServer mBeanServer = connection.getMBeanServerConnection();
// And there you are:
Set
Hashtable
topicStatTable.put(BEAN_FIELD.InFlightCount, new ArrayList
topicStatTable.put(BEAN_FIELD.QueueSize, new ArrayList
Pattern compiledPattern = Pattern.compile("org.apache.activemq:BrokerName=" + brokerName
+ ",Type=Topic,Destination=(.*)");
String separator = " <==> ";
for (ObjectInstance mbeanInstance : mbeanInstances) {
Matcher matcher = compiledPattern.matcher(mbeanInstance.getObjectName().toString());
if (matcher.matches()) {
StringBuilder buffer = new StringBuilder();
buffer.append(matcher.group(1));
buffer.append(separator);
MBeanInfo mbeanInfo = mBeanServer.getMBeanInfo(mbeanInstance.getObjectName());
for (MBeanAttributeInfo attrInfo : mbeanInfo.getAttributes()) {
buffer.append(attrInfo.getName() + "="
+ mBeanServer.getAttribute(mbeanInstance.getObjectName(), attrInfo.getName()));
buffer.append(separator);
}
System.out.println(buffer.toString());
}
}
For using the bean, you can look here:
Below is the full example, as given to me by Ophir Radnitz (ex-alphaCSP)
package com.cpu;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectInstance;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import com.cpu.JmxScanner.BEAN_FIELD;
import com.cpu.JmxScanner.TopicStat;
import com.sampullara.cli.Args;
import com.sampullara.cli.Argument;
public class JmxBeanSnapshot {
public static void main(String[] args) throws Exception {
TestCommand tc = new TestCommand();
Args.usage(tc);
Args.parse(tc, args);
String brokerName = tc.brokerName;
String jmxHost = tc.jmxHost;
int jmxPort = tc.jmxPort;
String jmxUrl = "service:jmx:rmi://" + jmxHost + ":" + jmxPort + "/jndi/rmi://" + jmxHost + ":" + jmxPort
+ "/jmxrmi";
System.out.println("jmxHost : " + jmxHost);
System.out.println("jmxPort : " + jmxPort);
System.out.println("brokerNAme : " + brokerName);
System.out.println("jmxUrl = " + jmxUrl);
JmxBeanSnapshot jmxBeanSnapshot = new JmxBeanSnapshot(jmxHost, jmxPort, brokerName);
jmxBeanSnapshot.analyzeTopic();
}
private void analyzeTopic() throws Exception {
Set mbeanInstances = mBeanServer.queryMBeans(null, null);
Hashtable> topicStatTable = new Hashtable>();
topicStatTable.put(BEAN_FIELD.InFlightCount, new ArrayList());
topicStatTable.put(BEAN_FIELD.QueueSize, new ArrayList());
Pattern compiledPattern = Pattern.compile("org.apache.activemq:BrokerName=" + brokerName
+ ",Type=Topic,Destination=(.*)");
String separator = " <==> ";
for (ObjectInstance mbeanInstance : mbeanInstances) {
Matcher matcher = compiledPattern.matcher(mbeanInstance.getObjectName().toString());
if (matcher.matches()) {
StringBuilder buffer = new StringBuilder();
buffer.append(matcher.group(1));
buffer.append(separator);
MBeanInfo mbeanInfo = mBeanServer.getMBeanInfo(mbeanInstance.getObjectName());
for (MBeanAttributeInfo attrInfo : mbeanInfo.getAttributes()) {
buffer.append(attrInfo.getName() + "="
+ mBeanServer.getAttribute(mbeanInstance.getObjectName(), attrInfo.getName()));
buffer.append(separator);
}
System.out.println(buffer.toString());
}
}
}
private MBeanServerConnection mBeanServer;
private String brokerName;
public JmxBeanSnapshot(String jmxHost, int jmxPort, String brokerName) throws Exception {
String jmxUrl = "service:jmx:rmi://" + jmxHost + ":" + jmxPort + "/jndi/rmi://" + jmxHost + ":" + jmxPort
+ "/jmxrmi";
this.brokerName = brokerName;
JMXServiceURL url = new JMXServiceURL(jmxUrl);
JMXConnector connection = JMXConnectorFactory.connect(url);
mBeanServer = connection.getMBeanServerConnection();
}
public static class TestCommand {
@Argument(value = "jmxHost", description = "JMX server host", required = false)
private String jmxHost = "michael";
@Argument(value = "jmxPort", description = "JMX Server Port", required = false)
private Integer jmxPort = 10099;
@Argument(value = "brokerName", description = "Name of the broker")
private String brokerName = "mich_production";
@Argument(value = "action", description = "Action to be taken: queueSize, connection (optional)")
private String action = "queueSize";
}
}
No comments:
Post a Comment