/src/com/googlecode/jmxtrans/example/TreeWalker3.java

http://jmxtrans.googlecode.com/ · Java · 93 lines · 65 code · 18 blank · 10 comment · 6 complexity · 40aaa42774bcf005af5816b06c5fbaff MD5 · raw file

  1. package com.googlecode.jmxtrans.example;
  2. import java.io.IOException;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7. import java.util.Set;
  8. import javax.management.AttributeNotFoundException;
  9. import javax.management.MBeanAttributeInfo;
  10. import javax.management.MBeanInfo;
  11. import javax.management.MBeanServerConnection;
  12. import javax.management.ObjectName;
  13. import javax.management.remote.JMXConnector;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import com.googlecode.jmxtrans.model.Query;
  17. import com.googlecode.jmxtrans.model.Result;
  18. import com.googlecode.jmxtrans.model.Server;
  19. import com.googlecode.jmxtrans.util.JmxUtils;
  20. /**
  21. * Walks a JMX tree and prints out all of the unique typenames and their attributes.
  22. *
  23. * This is a good test of the core engine of JmxTrans to ensure that it covers all cases.
  24. *
  25. * @author jon
  26. */
  27. public class TreeWalker3 {
  28. private static final Logger log = LoggerFactory.getLogger(TreeWalker3.class);
  29. /** */
  30. public static void main(String[] args) throws Exception {
  31. Server server = new Server("w2", "1105");
  32. JMXConnector conn = null;
  33. try {
  34. conn = JmxUtils.getServerConnection(server);
  35. MBeanServerConnection mbeanServer = conn.getMBeanServerConnection();
  36. TreeWalker3 tw = new TreeWalker3();
  37. tw.walkTree(mbeanServer);
  38. } catch (IOException e) {
  39. log.error("Problem processing queries for server: " + server.getHost() + ":" + server.getPort(), e);
  40. } finally {
  41. if (conn != null) {
  42. conn.close();
  43. }
  44. }
  45. }
  46. /** */
  47. public void walkTree(MBeanServerConnection connection) throws Exception {
  48. // key here is null, null returns everything!
  49. Set<ObjectName> mbeans = connection.queryNames(null, null);
  50. Map<String, String> output = new HashMap<String, String>();
  51. for (ObjectName name : mbeans) {
  52. MBeanInfo info = connection.getMBeanInfo(name);
  53. MBeanAttributeInfo[] attrs = info.getAttributes();
  54. Query query = new Query();
  55. query.setObj(name.getCanonicalName());
  56. for (MBeanAttributeInfo attrInfo : attrs) {
  57. query.addAttr(attrInfo.getName());
  58. }
  59. try {
  60. JmxUtils.processQuery(connection, query);
  61. } catch (AttributeNotFoundException anfe) {
  62. log.error("Error", anfe);
  63. }
  64. List<Result> results = query.getResults();
  65. for (Result result : results) {
  66. output.put(result.getTypeName(), query.getAttr().toString());
  67. }
  68. }
  69. for (Entry<String, String> entry : output.entrySet()) {
  70. log.debug(entry.getKey());
  71. log.debug(entry.getValue());
  72. log.debug("-----------------------------------------");
  73. }
  74. }
  75. }