-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathBasicExample.java
113 lines (95 loc) · 3.69 KB
/
BasicExample.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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;
/**
* Example program that was written to show off the basic features of SimpleJmx. It starts up a JMX server and registers
* an object with the server. The object exposes field and method information as JMX attributes and operations using
* annotations.
*
* <p>
* <b>NOTE:</b> For more details, see the SimpleJMX website: http://256stuff.com/sources/simplejmx/
* </p>
*
* @author graywatson
*/
public class BasicExample {
private static final int JMX_PORT = 8000;
public static void main(String[] args) throws Exception {
new BasicExample().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);
/*
* NOTE: you could also do:
*
* JmxServer jmxServer = new JmxServer(ManagementFactory.getPlatformMBeanServer());
*/
try {
// start our server
jmxServer.start();
// register our object
jmxServer.register(counter);
// we can register other objects here
// jmxServer.register(someOtherObject);
// 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);
Thread.sleep(1000000000);
} finally {
// unregister is not necessary if we are stopping the server
jmxServer.unregister(counter);
// close our server
jmxServer.close();
}
}
/**
* Here is our little bean that we are exposing via JMX. It can be in another class. It's just an inner class here
* for convenience. We could also specify folderNames array here to locate the inside of a folder for jconsole.
*/
@JmxResource(domainName = "j256.simplejmx", description = "Counter that shows how long we have been running")
public static class RuntimeCounter {
// start our timer
private long startMillis = System.currentTimeMillis();
// we can annotate fields directly to be published in JMX, isReadible defaults to true
@JmxAttributeField(description = "Show runtime in seconds", isWritable = true)
private boolean showSeconds;
// we can annotate getter methods
@JmxAttributeMethod(description = "Run time in seconds or milliseconds")
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;
}
}
/*
* NOTE: there is no setRunTime(...) so it won't be writable.
*/
// this is an operation that shows up in the operations tab in jconsole.
@JmxOperation(description = "Reset our start time to the current millis")
public String resetStartTime() {
startMillis = System.currentTimeMillis();
return "Timer has been reset to current millis";
}
// this is an operation that shows up in the operations tab in jconsole.
@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;
}
}
}