1 package org.jgroups.demos; 2 3 import org.jgroups.annotations.MBean; 4 import org.jgroups.annotations.ManagedAttribute; 5 import org.jgroups.annotations.ManagedOperation; 6 import org.jgroups.jmx.JmxConfigurator; 7 import org.jgroups.util.Util; 8 9 import javax.management.MBeanServer; 10 11 /** 12 * Shows how annotations can be used to expose attributes and operations 13 * @author Bela Ban 14 */ 15 @MBean 16 public class JmxDemo { 17 @ManagedAttribute 18 private int age; // exposed as read-only 'age' 19 20 @ManagedAttribute 21 private static final String last_name="Ban"; // read-only (because final) 'last_name' 22 23 @ManagedAttribute 24 private static final String first_name="Bela"; // read-only (final) 'first_name' 25 26 @ManagedAttribute(description="social security number") // read-only 27 private static final long id=322649L; 28 foo()29 public void foo() { // must be exposed because we have @MBean on the class 30 System.out.println("foo(" + number + "): age=" + age + ", name=" + first_name + " " + last_name); 31 } 32 33 @ManagedAttribute 34 private int number=10; // writeable because we have the (non-annnotated) setter below !! 35 36 @ManagedAttribute setNumber(int num)37 public void setNumber(int num) {number=num;} 38 39 @ManagedAttribute(name="NumberAsString") getNumberAsString()40 public String getNumberAsString() { 41 return String.valueOf(number); 42 } 43 44 @ManagedAttribute getMyFoo()45 public static int getMyFoo() {return 22;} // exposed as 'myFoo' *not* 'getMyFoo()' !! 46 47 @ManagedAttribute(writable=true) 48 private static int my_other_number_is_here=999; // exposed as writable myOtherNumberIsHere 49 50 @ManagedAttribute 51 private int other_number=20; // exposed as 'otherNumber' ? 52 53 @ManagedAttribute setOtherNumber(int num)54 public void setOtherNumber(int num) {other_number=num;} 55 56 @ManagedAttribute foobar()57 public void foobar() {} // doesn't start with setXXX() or getXXX(), ignored 58 59 @ManagedAttribute isFlag()60 public static boolean isFlag() {return true;} // exposed as Flag, *not* 'isFlag()' !! 61 62 @ManagedAttribute(description="my number attribute") 63 private long my_number=322649L; 64 65 @ManagedAttribute setMyNumber(long new_number)66 public void setMyNumber(long new_number) { 67 my_number=new_number; 68 } 69 70 private int accountNumber=10; 71 72 @ManagedAttribute setAccountNumber(int num)73 public void setAccountNumber(int num) {accountNumber=num;} // exposes accountNumber as writable 74 75 @ManagedAttribute getAccountNumber()76 public int getAccountNumber() {return accountNumber;} 77 78 int max_age=100; 79 80 @ManagedAttribute setMaxAge(int age)81 public void setMaxAge(int age) {max_age=age;} 82 83 @ManagedAttribute getMaxAge()84 public int getMaxAge() {return max_age;} 85 86 87 88 @ManagedOperation sayName()89 public String sayName() { 90 return "I'm " + first_name + " " + last_name; 91 } 92 add(int a, int b)93 public int add(int a, int b) {return a+b;} // exposed because @MBean is on the class 94 95 main(String[] args)96 public static void main(String[] args) { 97 JmxDemo demo=new JmxDemo(); 98 99 MBeanServer server=Util.getMBeanServer(); 100 if(server != null) { 101 try { 102 JmxConfigurator.register(demo, server, "demo:name=DemoObject"); 103 while(true) { 104 Util.sleep(10000); 105 } 106 } 107 catch(Exception e) { 108 e.printStackTrace(); 109 } 110 } 111 } 112 } 113