/* * JBoss, the OpenSource EJB server * * Distributable under LGPL license. * See terms of license at gnu.org. */ package javax.management.monitor; import java.io.Serializable; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import javax.management.InstanceNotFoundException; import javax.management.MBeanAttributeInfo; import javax.management.MBeanInfo; import javax.management.MBeanRegistration; import javax.management.MBeanServer; import javax.management.NotificationBroadcasterSupport; import javax.management.ObjectName; import org.jboss.mx.util.RunnableScheduler; import org.jboss.mx.util.SchedulableRunnable; /** * The monitor service. * *
Revisions: *
20020319 Adrian Brock: *
* * Retrieves the monitored attribute and passes it to each service.
* * Peforms the common error processing. * REVIEW: Use the internal interface???? */ void runMonitor() { // Monitor for uncaught errors try { MBeanInfo mbeanInfo = null; try { mbeanInfo = server.getMBeanInfo(observedObject); } catch (InstanceNotFoundException e) { sendObjectErrorNotification("The observed object is not registered."); return; } // Get the attribute information MBeanAttributeInfo[] mbeanAttributeInfo = mbeanInfo.getAttributes(); MBeanAttributeInfo attributeInfo = null; for (int i = 0; i < mbeanAttributeInfo.length; i++) { if (mbeanAttributeInfo[i].getName().equals(observedAttribute)) { attributeInfo = mbeanAttributeInfo[i]; break; } } // The attribute must exist if (attributeInfo == null) { sendAttributeErrorNotification( "The observed attribute does not exist"); return; } // The attribute must exist if (!attributeInfo.isReadable()) { sendAttributeErrorNotification("Attribute not readable."); return; } // Get the value Object value = null; try { value = server.getAttribute(observedObject, observedAttribute); } catch (InstanceNotFoundException e) { sendObjectErrorNotification("The observed object is not registered."); return; } // Check for null value if (value == null) { sendAttributeTypeErrorNotification("Attribute is null"); return; } // Now pass the value to the respective monitor. monitor(attributeInfo, value); } // Notify an unexcepted error catch (Exception e) { sendRuntimeErrorNotification("General error: " + e.toString()); } } /** * Perform the monitor specific processing. * * @param attributeInfo the MBean attribute information. * @param value the value to monitor. */ abstract void monitor(MBeanAttributeInfo attributeInfo, Object value) throws Exception; /** * Sends the notification * * @param type the notification type. * @param timestamp the time of the notification. * @param message the human readable message to send. * @param attribute the attribute name. * @param gauge the derived gauge. * @param trigger the trigger value. */ void sendNotification(String type, long timestamp, String message, String attribute, Object gauge, Object trigger) { long seq = 0; synchronized (this) { seq = ++sequenceNumber; } if (timestamp == 0) timestamp = System.currentTimeMillis(); sendNotification(new MonitorNotification(type, objectName, seq, timestamp, message, gauge, attribute, observedObject, trigger)); } /** * Send a runtime error notification. * * @param message the human readable message to send. */ void sendRuntimeErrorNotification(String message) { if ((alreadyNotified & RUNTIME_ERROR_NOTIFIED) == 0) sendNotification(MonitorNotification.RUNTIME_ERROR, 0, message, observedAttribute, null, null); alreadyNotified |= RUNTIME_ERROR_NOTIFIED; } /** * Send an object error notification. * * @param message the human readable message to send. */ void sendObjectErrorNotification(String message) { if ((alreadyNotified & OBSERVED_OBJECT_ERROR_NOTIFIED) == 0) sendNotification(MonitorNotification.OBSERVED_OBJECT_ERROR, 0, message, observedAttribute, null, null); alreadyNotified |= OBSERVED_OBJECT_ERROR_NOTIFIED; } /** * Send an attribute error notification. * * @param message the human readable message to send. */ void sendAttributeErrorNotification(String message) { if ((alreadyNotified & OBSERVED_ATTRIBUTE_ERROR_NOTIFIED) == 0) sendNotification(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, 0, message, observedAttribute, null, null); alreadyNotified |= OBSERVED_ATTRIBUTE_ERROR_NOTIFIED; } /** * Send an attribute type error notification. * * @param message the human readable message to send. */ void sendAttributeTypeErrorNotification(String message) { if ((alreadyNotified & OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED) == 0) sendNotification(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, 0, message, observedAttribute, null, null); alreadyNotified |= OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED; } // Protected ----------------------------------------------------- // Private ------------------------------------------------------- // Inner classes ------------------------------------------------- /** * A monitor runnable. */ private class MonitorRunnable extends SchedulableRunnable { // Attributes ---------------------------------------------------- // The monitoring to perform private Monitor monitor; // Constructors -------------------------------------------------- /** * Create a monitor runnable to periodically perform monitoring. * * @param monitor the monitoring to perform. */ public MonitorRunnable(Monitor monitor) { this.monitor = monitor; setScheduler(scheduler); } // Public -------------------------------------------------------- // SchedulableRunnable overrides --------------------------------- /** * Run the montior */ public void doRun() { // Perform the monitoring monitor.runMonitor(); // Reschedule setNextRun(System.currentTimeMillis() + monitor.granularityPeriod); } } }