Use spring's jmx annotation to get Pojo objects out to JMX

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.