// Get the Platform MBean Server MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
// Construct the ObjectName for the MBean we will register ObjectName name = new ObjectName("com.example.mbeans:type=Hello");
// Create the Hello World MBean Hello mbean = new Hello();
// Register the Hello World MBean mbs.registerMBean(mbean, name);
// operations
public void sayHello(); public int add(int x, int y);
// attributes
// a read-only attribute called Name of type String public String getName();
// a read-write attribute called CacheSize of type int public int getCacheSize(); public void setCacheSize(int size);
public void sayHello() { System.out.println("hello, world"); }
public int add(int x, int y) { return x + y; }
/* Name 属性の getter. 通常プライベートな実データフィールドを返す。 アプリケーション実行に伴い変化する値は読み込みのみ可とする。 */ public String getName() { return this.name; }
/* キャッシュサイズに関するゲッタ。変更可能な属性 */ public int getCacheSize() { return this.cacheSize; }
/* キャッシュサイズに関する雪駄。スレッド環境を考慮し同期とする */ public synchronized void setCacheSize(int size) { this.cacheSize = size;
System.out.println("Cache size now " + this.cacheSize); }