-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
WebServerExample.java
93 lines (78 loc) · 2.98 KB
/
WebServerExample.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
91
92
93
package com.j256.simplejmx.example;
import com.j256.simplejmx.common.JmxAttributeField;
import com.j256.simplejmx.common.JmxAttributeMethod;
import com.j256.simplejmx.common.JmxOperation;
import com.j256.simplejmx.common.JmxResource;
import com.j256.simplejmx.server.JmxServer;
import com.j256.simplejmx.web.JmxWebServer;
/**
* Example program which uses the {@link JmxWebServer} which publishes the beans over JMX _and_ over a simple web
* interface.
*
* <p>
* <b>NOTE:</b> For more details, see the SimpleJMX website: http://256stuff.com/sources/simplejmx/
* </p>
*
* @author graywatson
*/
public class WebServerExample {
private static final int JMX_PORT = 8000;
private static final int WEB_PORT = 8080;
public static void main(String[] args) throws Exception {
new WebServerExample().doMain(args);
}
private void doMain(String[] args) throws Exception {
// create a new JMX server listening on a specific port
JmxServer jmxServer = new JmxServer(JMX_PORT);
jmxServer.start();
RuntimeCounter counter = new RuntimeCounter();
jmxServer.register(counter);
// create a web server publisher listening on a specific port
JmxWebServer jmxWebServer = new JmxWebServer(WEB_PORT);
jmxWebServer.start();
try {
// do your other code here...
// 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);
System.out.println("Web server on port " + WEB_PORT);
Thread.sleep(1000000000);
} finally {
// stop our server
jmxWebServer.close();
jmxServer.close();
}
}
/**
* Here is our little bean that we are exposing via JMX. For more documentation about how it works, see
* {@link BasicExample}.
*/
@JmxResource(domainName = "j256.simplejmx", description = "Counter that shows how long we have been running")
public static class RuntimeCounter {
private long startMillis = System.currentTimeMillis();
@JmxAttributeField(isWritable = true, description = "Show the time in seconds if true else milliseconds")
private boolean showSeconds;
@JmxAttributeMethod(description = "The time we have been running in seconds or milliseconds")
public long getRunTime() {
long diffMillis = System.currentTimeMillis() - startMillis;
if (showSeconds) {
return diffMillis / 1000;
} else {
return diffMillis;
}
}
@JmxOperation(description = "Reset the start time to the current time millis")
public String resetStartTime() {
startMillis = System.currentTimeMillis();
return "Timer has been reset to current millis";
}
@JmxOperation(description = "Add a positive or negative offset to the start time milliseconds",
parameterNames = { "offset in millis" },
parameterDescriptions = { "offset milliseconds value to add to start time millis" })
public String addToStartTime(long offset) {
long old = startMillis;
startMillis += offset;
return "Timer value changed from " + old + " to " + startMillis;
}
}
}