PageRenderTime 23ms CodeModel.GetById 16ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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