Since JDK5.0 introduced annotations (Annotation), Java development has been simplified a lot, allowing developers to almost get rid of complex
Trouble with configuration files. This article describes the set of annotations that spring provides equivalent to the Commons Attribut attribute class and the implementation class annotationsjmxattributesource of a policy interface jmxattributesource that allows Mbeaninfoassembler to read these notes. This article shows you how to quickly and easily get Pojo objects out to JMX using the spring jmx annotation.
Inside the appearance of the function of the class name, described below, first look at an example:
Write a Pojo object first
1 Import java.util.ArrayList;
2 Import java.util.List;
3
4 Import Org.springframework.jmx.export.annotation.ManagedAttribute;
5 Import org.springframework.jmx.export.annotation.ManagedOperation;
6 Import Org.springframework.jmx.export.annotation.ManagedOperationParameter;
7 Import Org.springframework.jmx.export.annotation.ManagedOperationParameters;
8 Import Org.springframework.jmx.export.annotation.ManagedResource;
9
10//instance marked as a resource managed by JMX
One @ManagedResource (objectname= "Bean:name=testjmxbean", description= "My Managed Bean", log=true,
Logfile= "Jmx.log", currencytimelimit=15, persistpolicy= "OnUpdate", persistperiod=200,
Persistlocation= "foo", Persistname= "bar"
public class Annotationtestbean {
15
private String name;
-Private int age;
18
Private list<string> values;
20
21//To mark the getter or setter as JMX properties
@ManagedAttribute (description= "The Age Attribute", currencytimelimit=1)
public int getage () {
return to age;
25}
26
setage void (int age) {
This.age = age;
29}
30
@ManagedAttribute (description= "The Values", currencytimelimit=1)
Public list<string> getValues () {
The values = new arraylist<string> (2);
Values.add ("Hello");
Values.add ("World");
return values;
37}
38
@ManagedAttribute (description= "The Name Attribute",
Currencytimelimit=20,
The defaultvalue= "Bar",
Persistpolicy= "OnUpdate")
The public void SetName (String name) {
THIS.name = name;
System.out.println ("set:" + name);
46}
47
@ManagedAttribute (defaultvalue= "foo", persistperiod=300)
The public String GetName () {
System.out.println ("Get:" + name);
Wu return name;
52}
53
54//To mark the method as a JMX operation
@ManagedOperation (description= "Add two Numbers")
@ManagedOperationParameters ({
@ManagedOperationParameter (name = "X", Description = "the number"),
@ManagedOperationParameter (name = "Y", Description = "The second Number")})
public int Add (int x, int y) {
return x + y;
61}
62
Dontexposeme public void () {
throw new RuntimeException ();
65}
66}
67