15 JMX MBeanの通知
この例では、MBeanの通知を実装します。この項に含まれるソース・コードを使用して、適切な設定手順で指定したexamples/
ディレクトリに対応するファイルが作成され、次の内容が含まれます。
examples/Notification/README
# ==============================================================================
#
# JMX Tutorial Introductory Example : Instrumenting Your Own Applications.
# Using Notifications.
#
# This example is the same as the previous essential example with the
# only difference that the Hello World MBean has been modified to send
# notifications.
#
# The Hello World MBean implements the NotificationBroadcaster interface
# by extending the NotificationBroadcasterSupport class and emits
# AttributeChangeNotifications every time the CacheSize attribute
# is changed.
#
# ==============================================================================
#
# In order to compile and run the example, make a copy of this README file, and
# then simply cut and paste all the commands as needed into a terminal window.
#
# This README makes the assumption that you are running under Java SE 6 on Unix,
# you are familiar with the JMX technology, and with the bourne shell or korn
# shell syntax.
#
# All the commands below are defined using Unix korn shell syntax.
#
# If you are not running Unix and korn shell you are expected to be able to
# adapt these commands to your favorite OS and shell environment.
#
# Compile Java classes
#
# The Java classes used in this example are contained in the com.example.mbeans
# Java package.
#
# * Main.java: gets the Platform MBean Server, and creates
# and registers the Hello World MBean on it.
#
# * Hello.java: implements the Hello World standard MBean.
# This MBean emits notifications every time
# the CacheSize attribute is changed.
#
# * HelloMBean.java: the management interface exposed by
# the Hello World standard MBean.
#
javac com/example/mbeans/*.java
# Start the Main application
#
java com.example.mbeans.Main
# Start jconsole on a different shell window on the same machine
#
# JConsole is located in $(J2SE_HOME)/bin/jconsole
#
jconsole
# ==============================================================================
examples/Notification/com/example/mbeans/Main.java
/* Main.java - main class for Hello World example. Create the
HelloWorld MBean, register it, then wait forever (or until the
program is interrupted). */
package com.example.mbeans;
import java.lang.management.*;
import javax.management.*;
public class Main {
/* For simplicity, we declare "throws Exception". Real programs
will usually want finer-grained exception handling. */
public static void main(String[] args) throws Exception {
// 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);
// Wait forever
System.out.println("Waiting forever...");
Thread.sleep(Long.MAX_VALUE);
}
}
examples/Notification/com/example/mbeans/Hello.java
/* Hello.java - MBean implementation for the Hello World MBean.
This class must implement all the Java methods declared in the
HelloMBean interface, with the appropriate behavior for each one. */
package com.example.mbeans;
import javax.management.*;
public class Hello
extends NotificationBroadcasterSupport implements HelloMBean {
public void sayHello() {
System.out.println("hello, world");
}
public int add(int x, int y) {
return x + y;
}
/* Getter for the Name attribute. The pattern shown here is
frequent: the getter returns a private field representing the
attribute value. In our case, the attribute value never
changes, but for other attributes it might change as the
application runs. Consider an attribute representing
statistics such as uptime or memory usage, for example. Being
read-only just means that it can't be changed through the
management interface. */
public String getName() {
return this.name;
}
/* Getter for the CacheSize attribute. The pattern shown here is
frequent: the getter returns a private field representing the
attribute value, and the setter changes that field. */
public int getCacheSize() {
return this.cacheSize;
}
/* Setter for the CacheSize attribute. To avoid problems with
stale values in multithreaded situations, it is a good idea
for setters to be synchronized. */
public synchronized void setCacheSize(int size) {
int oldSize = this.cacheSize;
this.cacheSize = size;
/* In a real application, changing the attribute would
typically have effects beyond just modifying the cacheSize
field. For example, resizing the cache might mean
discarding entries or allocating new ones. The logic for
these effects would be here. */
System.out.println("Cache size now " + this.cacheSize);
/* Construct a notification that describes the change. The
"source" of a notification is the ObjectName of the MBean
that emitted it. But an MBean can put a reference to
itself ("this") in the source, and the MBean server will
replace this with the ObjectName before sending the
notification on to its clients.
For good measure, we maintain a sequence number for each
notification emitted by this MBean.
The oldValue and newValue parameters to the constructor are
of type Object, so we are relying on Tiger's autoboxing
here. */
Notification n =
new AttributeChangeNotification(this,
sequenceNumber++,
System.currentTimeMillis(),
"CacheSize changed",
"CacheSize",
"int",
oldSize,
this.cacheSize);
/* Now send the notification using the sendNotification method
inherited from the parent class
NotificationBroadcasterSupport. */
sendNotification(n);
}
@Override
public MBeanNotificationInfo[] getNotificationInfo() {
String[] types = new String[] {
AttributeChangeNotification.ATTRIBUTE_CHANGE
};
String name = AttributeChangeNotification.class.getName();
String description = "An attribute of this MBean has changed";
MBeanNotificationInfo info =
new MBeanNotificationInfo(types, name, description);
return new MBeanNotificationInfo[] {info};
}
private final String name = "Reginald";
private int cacheSize = DEFAULT_CACHE_SIZE;
private static final int DEFAULT_CACHE_SIZE = 200;
private long sequenceNumber = 1;
}
examples/Notification/com/example/mbeans/HelloMBean.java
/* HelloMBean.java - MBean interface describing the management
operations and attributes for the Hello World MBean. In this case
there are two operations, "sayHello" and "add", and two attributes,
"Name" and "CacheSize". */
package com.example.mbeans;
public interface HelloMBean {
// 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);
}