-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
PublishAllExample.java
90 lines (72 loc) · 2.62 KB
/
PublishAllExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.j256.simplejmx.example;
import com.j256.simplejmx.common.JmxResourceInfo;
import com.j256.simplejmx.server.JmxServer;
import com.j256.simplejmx.server.PublishAllBeanWrapper;
/**
* Example program that was written to show off how you can publish any object using the {@link PublishAllBeanWrapper}
* which publishes all public fields and methods. You may also want to look at the {@link RandomObjectExample} which
* publishes objects to JMX programmatically.
*
* <p>
* <b>NOTE:</b> For more details, see the SimpleJMX website: http://256stuff.com/sources/simplejmx/
* </p>
*
* @author graywatson
*/
public class PublishAllExample {
private static final int JMX_PORT = 8000;
public static void main(String[] args) throws Exception {
new PublishAllExample().doMain(args);
}
private void doMain(String[] args) throws Exception {
// create the object we will be exposing with JMX
RuntimeCounter counter = new RuntimeCounter();
// create a new JMX server listening on a specific port
JmxServer jmxServer = new JmxServer(JMX_PORT);
try {
// start our server
jmxServer.start();
// register our object using the PublishAllBeanWrapper to expose the public fields and methods
jmxServer.register(new PublishAllBeanWrapper(counter, new JmxResourceInfo("com.j256", null,
"runtime counter")));
// we just sleep forever to let the jmx server do its stuff
System.out.println("Sleeping for a while to let the server do its stuff");
System.out.println("JMX server on port " + JMX_PORT);
Thread.sleep(1000000000);
} finally {
// unregister is not necessary if we are stopping the server
jmxServer.unregister(counter);
// stop our server
jmxServer.close();
}
}
/**
* Here is our little bean that we are exposing via JMX by using the {@link PublishAllBeanWrapper}. It can be in
* another class. It's just an inner class here for convenience.
*/
public static class RuntimeCounter {
private long startMillis = System.currentTimeMillis();
public boolean showSeconds;
public long getRunTime() {
// show how long we are running
long diffMillis = System.currentTimeMillis() - startMillis;
if (showSeconds) {
// as seconds
return diffMillis / 1000;
} else {
// or as milliseconds
return diffMillis;
}
}
// no set method so it won't be writable
public String resetStartTime() {
startMillis = System.currentTimeMillis();
return "Timer has been reset to current millis";
}
public String addToStartTime(long offset) {
long old = startMillis;
startMillis += offset;
return "Timer value changed from " + old + " to " + startMillis;
}
}
}