1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.neo4j.server.rrd;
21
22 import org.neo4j.jmx.Kernel;
23 import org.neo4j.jmx.Primitives;
24 import org.neo4j.kernel.AbstractGraphDatabase;
25
26 import javax.management.AttributeNotFoundException;
27 import javax.management.InstanceNotFoundException;
28 import javax.management.MBeanException;
29 import javax.management.MBeanServer;
30 import javax.management.MalformedObjectNameException;
31 import javax.management.ObjectName;
32 import javax.management.ReflectionException;
33 import java.lang.management.ManagementFactory;
34
35 public abstract class JmxSampleableBase implements Sampleable
36 {
37 private MBeanServer mbeanServer;
38 private ObjectName objectName;
39
40 public JmxSampleableBase( AbstractGraphDatabase graphDb ) throws MalformedObjectNameException
41 {
42 mbeanServer = ManagementFactory.getPlatformMBeanServer();
43 ObjectName neoQuery = graphDb.getManagementBean( Kernel.class ).getMBeanQuery();
44 String instance = neoQuery.getKeyProperty( "instance" );
45 String baseName = neoQuery.getDomain() + ":instance=" + instance + ",name=";
46 objectName = new ObjectName( baseName + Primitives.NAME );
47 }
48
49 public abstract String getName();
50
51 public long getValue()
52 {
53 try
54 {
55 return (Long)mbeanServer.getAttribute( objectName, getJmxAttributeName() );
56 } catch ( MBeanException e )
57 {
58 throw new RuntimeException( e );
59 } catch ( AttributeNotFoundException e )
60 {
61 throw new RuntimeException( e );
62 } catch ( InstanceNotFoundException e )
63 {
64 throw new RuntimeException( e );
65 } catch ( ReflectionException e )
66 {
67 throw new RuntimeException( e );
68 }
69 }
70
71 protected abstract String getJmxAttributeName();
72 }