Search in sources :

Example 11 with MonitorNotification

use of javax.management.monitor.MonitorNotification in project jdk8u_jdk by JetBrains.

the class Monitor method monitor.

/**
     * This method is called by the monitor each time
     * the granularity period has been exceeded.
     * @param o The observed object.
     */
private void monitor(ObservedObject o, int index, int[] an) {
    String attribute;
    String notifType = null;
    String msg = null;
    Object derGauge = null;
    Object trigger = null;
    ObjectName object;
    Comparable<?> value = null;
    MonitorNotification alarm = null;
    if (!isActive())
        return;
    //
    synchronized (this) {
        object = o.getObservedObject();
        attribute = getObservedAttribute();
        if (object == null || attribute == null) {
            return;
        }
    }
    // Check that the observed object is registered in the
    // MBean server and that the observed attribute
    // belongs to the observed object.
    //
    Object attributeValue = null;
    try {
        attributeValue = getAttribute(server, object, attribute);
        if (attributeValue == null)
            if (isAlreadyNotified(o, OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED))
                return;
            else {
                notifType = OBSERVED_ATTRIBUTE_TYPE_ERROR;
                setAlreadyNotified(o, index, OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED, an);
                msg = "The observed attribute value is null.";
                MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", msg);
            }
    } catch (NullPointerException np_ex) {
        if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
            return;
        else {
            notifType = RUNTIME_ERROR;
            setAlreadyNotified(o, index, RUNTIME_ERROR_NOTIFIED, an);
            msg = "The monitor must be registered in the MBean " + "server or an MBeanServerConnection must be " + "explicitly supplied.";
            MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", msg);
            MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", np_ex.toString());
        }
    } catch (InstanceNotFoundException inf_ex) {
        if (isAlreadyNotified(o, OBSERVED_OBJECT_ERROR_NOTIFIED))
            return;
        else {
            notifType = OBSERVED_OBJECT_ERROR;
            setAlreadyNotified(o, index, OBSERVED_OBJECT_ERROR_NOTIFIED, an);
            msg = "The observed object must be accessible in " + "the MBeanServerConnection.";
            MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", msg);
            MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", inf_ex.toString());
        }
    } catch (AttributeNotFoundException anf_ex) {
        if (isAlreadyNotified(o, OBSERVED_ATTRIBUTE_ERROR_NOTIFIED))
            return;
        else {
            notifType = OBSERVED_ATTRIBUTE_ERROR;
            setAlreadyNotified(o, index, OBSERVED_ATTRIBUTE_ERROR_NOTIFIED, an);
            msg = "The observed attribute must be accessible in " + "the observed object.";
            MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", msg);
            MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", anf_ex.toString());
        }
    } catch (MBeanException mb_ex) {
        if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
            return;
        else {
            notifType = RUNTIME_ERROR;
            setAlreadyNotified(o, index, RUNTIME_ERROR_NOTIFIED, an);
            msg = mb_ex.getMessage() == null ? "" : mb_ex.getMessage();
            MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", msg);
            MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", mb_ex.toString());
        }
    } catch (ReflectionException ref_ex) {
        if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED)) {
            return;
        } else {
            notifType = RUNTIME_ERROR;
            setAlreadyNotified(o, index, RUNTIME_ERROR_NOTIFIED, an);
            msg = ref_ex.getMessage() == null ? "" : ref_ex.getMessage();
            MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", msg);
            MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", ref_ex.toString());
        }
    } catch (IOException io_ex) {
        if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
            return;
        else {
            notifType = RUNTIME_ERROR;
            setAlreadyNotified(o, index, RUNTIME_ERROR_NOTIFIED, an);
            msg = io_ex.getMessage() == null ? "" : io_ex.getMessage();
            MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", msg);
            MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", io_ex.toString());
        }
    } catch (RuntimeException rt_ex) {
        if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
            return;
        else {
            notifType = RUNTIME_ERROR;
            setAlreadyNotified(o, index, RUNTIME_ERROR_NOTIFIED, an);
            msg = rt_ex.getMessage() == null ? "" : rt_ex.getMessage();
            MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", msg);
            MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", rt_ex.toString());
        }
    }
    synchronized (this) {
        //
        if (!isActive())
            return;
        //
        if (!attribute.equals(getObservedAttribute()))
            return;
        //
        if (msg == null) {
            try {
                value = getComparableFromAttribute(object, attribute, attributeValue);
            } catch (ClassCastException e) {
                if (isAlreadyNotified(o, OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED))
                    return;
                else {
                    notifType = OBSERVED_ATTRIBUTE_TYPE_ERROR;
                    setAlreadyNotified(o, index, OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED, an);
                    msg = "The observed attribute value does not " + "implement the Comparable interface.";
                    MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", msg);
                    MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", e.toString());
                }
            } catch (AttributeNotFoundException e) {
                if (isAlreadyNotified(o, OBSERVED_ATTRIBUTE_ERROR_NOTIFIED))
                    return;
                else {
                    notifType = OBSERVED_ATTRIBUTE_ERROR;
                    setAlreadyNotified(o, index, OBSERVED_ATTRIBUTE_ERROR_NOTIFIED, an);
                    msg = "The observed attribute must be accessible in " + "the observed object.";
                    MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", msg);
                    MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", e.toString());
                }
            } catch (RuntimeException e) {
                if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
                    return;
                else {
                    notifType = RUNTIME_ERROR;
                    setAlreadyNotified(o, index, RUNTIME_ERROR_NOTIFIED, an);
                    msg = e.getMessage() == null ? "" : e.getMessage();
                    MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", msg);
                    MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", e.toString());
                }
            }
        }
        //
        if (msg == null) {
            if (!isComparableTypeValid(object, attribute, value)) {
                if (isAlreadyNotified(o, OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED))
                    return;
                else {
                    notifType = OBSERVED_ATTRIBUTE_TYPE_ERROR;
                    setAlreadyNotified(o, index, OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED, an);
                    msg = "The observed attribute type is not valid.";
                    MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", msg);
                }
            }
        }
        //
        if (msg == null) {
            if (!isThresholdTypeValid(object, attribute, value)) {
                if (isAlreadyNotified(o, THRESHOLD_ERROR_NOTIFIED))
                    return;
                else {
                    notifType = THRESHOLD_ERROR;
                    setAlreadyNotified(o, index, THRESHOLD_ERROR_NOTIFIED, an);
                    msg = "The threshold type is not valid.";
                    MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", msg);
                }
            }
        }
        //
        if (msg == null) {
            msg = buildErrorNotification(object, attribute, value);
            if (msg != null) {
                if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
                    return;
                else {
                    notifType = RUNTIME_ERROR;
                    setAlreadyNotified(o, index, RUNTIME_ERROR_NOTIFIED, an);
                    MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(), "monitor", msg);
                }
            }
        }
        //
        if (msg == null) {
            // Clear all already notified flags.
            //
            resetAllAlreadyNotified(o, index, an);
            // Get derived gauge from comparable value.
            //
            derGauge = getDerivedGaugeFromComparable(object, attribute, value);
            o.setDerivedGauge(derGauge);
            o.setDerivedGaugeTimeStamp(System.currentTimeMillis());
            // Check if an alarm must be fired.
            //
            alarm = buildAlarmNotification(object, attribute, (Comparable<?>) derGauge);
        }
    }
    //
    if (msg != null)
        sendNotification(notifType, System.currentTimeMillis(), msg, derGauge, trigger, object, true);
    //
    if (alarm != null && alarm.getType() != null)
        sendNotification(alarm.getType(), System.currentTimeMillis(), alarm.getMessage(), derGauge, alarm.getTrigger(), object, false);
}
Also used : ReflectionException(javax.management.ReflectionException) MonitorNotification(javax.management.monitor.MonitorNotification) AttributeNotFoundException(javax.management.AttributeNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) IOException(java.io.IOException) ObjectName(javax.management.ObjectName) MBeanException(javax.management.MBeanException)

Example 12 with MonitorNotification

use of javax.management.monitor.MonitorNotification in project wildfly-camel by wildfly-extras.

the class JMXIntegrationTest method testMonitorMBeanAttribute.

@Test
public void testMonitorMBeanAttribute() throws Exception {
    CamelContext context = contextRegistry.getCamelContext("jmx-context-1");
    Assert.assertNotNull("Camel context jmx-context-1 was null", context);
    final String routeName = context.getRoutes().get(0).getId();
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("jmx:platform?format=raw&objectDomain=org.apache.camel&key.context=jmx-context-1&key.type=routes&key.name=\"" + routeName + "\"" + "&monitorType=counter&observedAttribute=ExchangesTotal&granularityPeriod=500").to("direct:end");
        }
    });
    camelctx.start();
    try {
        ConsumerTemplate consumer = camelctx.createConsumerTemplate();
        MonitorNotification notifcation = consumer.receiveBody("direct:end", MonitorNotification.class);
        Assert.assertEquals("ExchangesTotal", notifcation.getObservedAttribute());
    } finally {
        camelctx.stop();
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ConsumerTemplate(org.apache.camel.ConsumerTemplate) MonitorNotification(javax.management.monitor.MonitorNotification) RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Aggregations

MonitorNotification (javax.management.monitor.MonitorNotification)12 AttributeNotFoundException (javax.management.AttributeNotFoundException)2 CamelContext (org.apache.camel.CamelContext)2 ConsumerTemplate (org.apache.camel.ConsumerTemplate)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)2 Test (org.junit.Test)2 IntrospectionException (java.beans.IntrospectionException)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1 AttributeChangeNotification (javax.management.AttributeChangeNotification)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 MBeanException (javax.management.MBeanException)1 MBeanServerNotification (javax.management.MBeanServerNotification)1 ObjectName (javax.management.ObjectName)1 ReflectionException (javax.management.ReflectionException)1 OpenDataException (javax.management.openmbean.OpenDataException)1