@@ -17,3 +17,46 @@ Here's a little working example program:
17
17
18
18
Enjoy,
19
19
Gray Watson
20
+
21
+ -----------------------------------------------------------------------------
22
+ Little Sample Program
23
+ http://256.com/sources/simplejmx/docs/example-simple
24
+ -----------------------------------------------------------------------------
25
+
26
+ // create a new JMX server listening on a specific port
27
+ JmxServer jmxServer = new JmxServer(JMX_PORT);
28
+ // NOTE: you could also do: new JmxServer(ManagementFactory.getPlatformMBeanServer());
29
+ // start our server
30
+ jmxServer.start();
31
+
32
+ // create the object we will be exposing with JMX
33
+ RuntimeCounter counter = new RuntimeCounter();
34
+ // register our object
35
+ jmxServer.register(counter);
36
+ ...
37
+ // shutdown our server
38
+ jmxServer.stop();
39
+ ...
40
+
41
+ @JmxResource(domainName = "j256")
42
+ public class RuntimeCounter {
43
+ private long startMillis = System.currentTimeMillis();
44
+
45
+ // we can annotate fields directly to be published, isReadible defaults to true
46
+ @JmxAttributeField(description = "Show runtime in seconds", isWritable = true)
47
+ private boolean showSeconds;
48
+
49
+ // we can annotate getter methods
50
+ @JmxAttributeMethod(description = "Run time in seconds or milliseconds")
51
+ public long getRunTime() {
52
+ long diffMillis = System.currentTimeMillis() - startMillis;
53
+ return diffMillis / (showSeconds ? 1 : 1000);
54
+ }
55
+
56
+ // this is an operation that shows up in the operations tab in jconsole.
57
+ @JmxOperation(description = "Reset our start time to the current millis")
58
+ public String resetStartTime() {
59
+ startMillis = System.currentTimeMillis();
60
+ return "Timer has been reset to current millis";
61
+ }
62
+ }
0 commit comments