Skip to content

Commit 2fff699

Browse files
author
Gray Watson
committedAug 18, 2014
Merge branch 'master' of github.com:j256/simplejmx
2 parents 9e36d68 + 1f1d519 commit 2fff699

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
 

‎README.txt

+43
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,46 @@ Here's a little working example program:
1717

1818
Enjoy,
1919
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

Comments
 (0)
Please sign in to comment.