Search in sources :

Example 1 with CounterMonitor

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

the class CounterMonitorInitThresholdTest method runTest.

public static void runTest() throws Exception {
    // Retrieve the platform MBean server
    //
    System.out.println("\nRetrieve the platform MBean server");
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    String domain = mbs.getDefaultDomain();
    // Create and register TestMBeans
    //
    ObjectName name1 = new ObjectName(domain + ":type=" + Test.class.getName() + ",name=1");
    mbs.createMBean(Test.class.getName(), name1);
    TestMBean mbean1 = (TestMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, name1, TestMBean.class, false);
    ObjectName name2 = new ObjectName(domain + ":type=" + Test.class.getName() + ",name=2");
    mbs.createMBean(Test.class.getName(), name2);
    TestMBean mbean2 = (TestMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, name2, TestMBean.class, false);
    // Create and register CounterMonitorMBean
    //
    ObjectName cmn = new ObjectName(domain + ":type=" + CounterMonitor.class.getName());
    CounterMonitor m = new CounterMonitor();
    mbs.registerMBean(m, cmn);
    CounterMonitorMBean cm = (CounterMonitorMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, cmn, CounterMonitorMBean.class, true);
    ((NotificationEmitter) cm).addNotificationListener(new Listener(), null, null);
    cm.setObservedAttribute("Counter");
    cm.setGranularityPeriod(100);
    cm.setInitThreshold(3);
    cm.setNotify(true);
    // Add observed object name1
    //
    System.out.println("\nObservedObject \"" + name1 + "\" registered before starting the monitor");
    cm.addObservedObject(name1);
    // Start the monitor
    //
    System.out.println("\nStart monitoring...");
    cm.start();
    // Play with counter for name1
    //
    System.out.println("\nTest ObservedObject \"" + name1 + "\"");
    for (int i = 0; i < 4; i++) {
        mbean1.setCounter(i);
        System.out.println("\nCounter = " + mbean1.getCounter());
        Thread.sleep(300);
        Number thresholdValue = cm.getThreshold(name1);
        System.out.println("Threshold = " + thresholdValue);
        if (thresholdValue.intValue() != 3) {
            System.out.println("Wrong threshold! Current value = " + thresholdValue + " Expected value = 3");
            System.out.println("\nStop monitoring...");
            cm.stop();
            throw new IllegalArgumentException("wrong threshold");
        }
        Thread.sleep(300);
    }
    // Add observed object name2
    //
    System.out.println("\nObservedObject \"" + name2 + "\" registered after starting the monitor");
    cm.addObservedObject(name2);
    // Play with counter for name2
    //
    System.out.println("\nTest ObservedObject \"" + name2 + "\"");
    for (int i = 0; i < 4; i++) {
        mbean2.setCounter(i);
        System.out.println("\nCounter = " + mbean2.getCounter());
        Thread.sleep(300);
        Number thresholdValue = cm.getThreshold(name2);
        System.out.println("Threshold = " + thresholdValue);
        if (thresholdValue.intValue() != 3) {
            System.out.println("Wrong threshold! Current value = " + thresholdValue + " Expected value = 3");
            System.out.println("\nStop monitoring...");
            cm.stop();
            throw new IllegalArgumentException("wrong threshold");
        }
        Thread.sleep(300);
    }
    // Stop the monitor
    //
    System.out.println("\nStop monitoring...");
    cm.stop();
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) NotificationListener(javax.management.NotificationListener) CounterMonitorMBean(javax.management.monitor.CounterMonitorMBean) CounterMonitor(javax.management.monitor.CounterMonitor) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 2 with CounterMonitor

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

the class CounterMonitorThresholdTest method runTest.

public static void runTest(int offset, int[] counter, int[] derivedGauge, int[] threshold) throws Exception {
    // Retrieve the platform MBean server
    //
    System.out.println("\nRetrieve the platform MBean server");
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    String domain = mbs.getDefaultDomain();
    // Create and register TestMBean
    //
    ObjectName name = new ObjectName(domain + ":type=" + Test.class.getName() + ",offset=" + offset);
    mbs.createMBean(Test.class.getName(), name);
    TestMBean mbean = (TestMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, name, TestMBean.class, false);
    // Create and register CounterMonitorMBean
    //
    ObjectName cmn = new ObjectName(domain + ":type=" + CounterMonitor.class.getName() + ",offset=" + offset);
    CounterMonitor m = new CounterMonitor();
    mbs.registerMBean(m, cmn);
    CounterMonitorMBean cm = (CounterMonitorMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, cmn, CounterMonitorMBean.class, true);
    ((NotificationEmitter) cm).addNotificationListener(new Listener(), null, null);
    cm.addObservedObject(name);
    cm.setObservedAttribute("Counter");
    cm.setGranularityPeriod(100);
    cm.setInitThreshold(1);
    cm.setOffset(offset);
    cm.setModulus(5);
    cm.setNotify(true);
    // Start the monitor
    //
    System.out.println("\nStart monitoring...");
    cm.start();
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // see 8025207
        do {
            Thread.sleep(150);
            derivedGaugeValue = (Integer) cm.getDerivedGauge(name);
        } while (derivedGaugeValue.intValue() != derivedGauge[i]);
        Number thresholdValue = cm.getThreshold(name);
        System.out.println("Threshold = " + thresholdValue);
        if (thresholdValue.intValue() != threshold[i]) {
            System.out.println("Wrong threshold! Current value = " + thresholdValue + " Expected value = " + threshold[i]);
            System.out.println("\nStop monitoring...");
            cm.stop();
            throw new IllegalArgumentException("wrong threshold");
        }
    }
    // Stop the monitor
    //
    System.out.println("\nStop monitoring...");
    cm.stop();
}
Also used : NotificationListener(javax.management.NotificationListener) ObjectName(javax.management.ObjectName) NotificationEmitter(javax.management.NotificationEmitter) CounterMonitorMBean(javax.management.monitor.CounterMonitorMBean) CounterMonitor(javax.management.monitor.CounterMonitor) MBeanServer(javax.management.MBeanServer)

Example 3 with CounterMonitor

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

the class ThreadPoolAccTest method main.

public static void main(String[] args) throws Exception {
    ObjectName[] mbeanNames = new ObjectName[6];
    ObservedObject[] monitored = new ObservedObject[6];
    ObjectName[] monitorNames = new ObjectName[6];
    Monitor[] monitor = new Monitor[6];
    String[] principals = { "role1", "role2" };
    String[] attributes = { "Integer", "Double", "String" };
    try {
        echo(">>> CREATE MBeanServer");
        MBeanServer server = MBeanServerFactory.newMBeanServer();
        for (int i = 0; i < 6; i++) {
            mbeanNames[i] = new ObjectName(":type=ObservedObject,instance=" + i);
            monitored[i] = new ObservedObject();
            echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
            server.registerMBean(monitored[i], mbeanNames[i]);
            switch(i) {
                case 0:
                case 3:
                    monitorNames[i] = new ObjectName(":type=CounterMonitor,instance=" + i);
                    monitor[i] = new CounterMonitor();
                    break;
                case 1:
                case 4:
                    monitorNames[i] = new ObjectName(":type=GaugeMonitor,instance=" + i);
                    monitor[i] = new GaugeMonitor();
                    break;
                case 2:
                case 5:
                    monitorNames[i] = new ObjectName(":type=StringMonitor,instance=" + i);
                    monitor[i] = new StringMonitor();
                    break;
            }
            echo(">>> CREATE Monitor = " + monitorNames[i].toString());
            server.registerMBean(monitor[i], monitorNames[i]);
            monitor[i].addObservedObject(mbeanNames[i]);
            monitor[i].setObservedAttribute(attributes[i % 3]);
            monitor[i].setGranularityPeriod(500);
            final Monitor m = monitor[i];
            Subject subject = new Subject();
            echo(">>> RUN Principal = " + principals[i / 3]);
            subject.getPrincipals().add(new JMXPrincipal(principals[i / 3]));
            PrivilegedAction<Void> action = new PrivilegedAction<Void>() {

                public Void run() {
                    m.start();
                    return null;
                }
            };
            Subject.doAs(subject, action);
        }
        while (!testPrincipals(monitored, monitorNames, monitor, principals)) ;
    } finally {
        for (int i = 0; i < 6; i++) if (monitor[i] != null)
            monitor[i].stop();
    }
}
Also used : GaugeMonitor(javax.management.monitor.GaugeMonitor) JMXPrincipal(javax.management.remote.JMXPrincipal) Subject(javax.security.auth.Subject) ObjectName(javax.management.ObjectName) StringMonitor(javax.management.monitor.StringMonitor) StringMonitor(javax.management.monitor.StringMonitor) CounterMonitor(javax.management.monitor.CounterMonitor) Monitor(javax.management.monitor.Monitor) GaugeMonitor(javax.management.monitor.GaugeMonitor) PrivilegedAction(java.security.PrivilegedAction) CounterMonitor(javax.management.monitor.CounterMonitor) MBeanServer(javax.management.MBeanServer)

Example 4 with CounterMonitor

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

the class ReflectionExceptionTest method counterMonitorNotification.

/**
     * Update the counter and check for notifications
     */
public int counterMonitorNotification() throws Exception {
    CounterMonitor counterMonitor = new CounterMonitor();
    try {
        // Create a new CounterMonitor MBean and add it to the MBeanServer.
        //
        echo(">>> CREATE a new CounterMonitor MBean");
        ObjectName counterMonitorName = new ObjectName(domain + ":type=" + CounterMonitor.class.getName());
        server.registerMBean(counterMonitor, counterMonitorName);
        echo(">>> ADD a listener to the CounterMonitor");
        counterMonitor.addNotificationListener(this, null, null);
        //
        // MANAGEMENT OF A STANDARD MBEAN
        //
        echo(">>> SET the attributes of the CounterMonitor:");
        counterMonitor.addObservedObject(obsObjName);
        echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
        counterMonitor.setObservedAttribute("IntegerAttribute");
        echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
        counterMonitor.setNotify(false);
        echo("\tATTRIBUTE \"NotifyFlag\"        = false");
        Integer threshold = 2;
        counterMonitor.setInitThreshold(threshold);
        echo("\tATTRIBUTE \"Threshold\"         = " + threshold);
        int granularityperiod = 500;
        counterMonitor.setGranularityPeriod(granularityperiod);
        echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
        echo(">>> START the CounterMonitor");
        counterMonitor.start();
        // Check if notification was received
        //
        doWait();
        if (messageReceived) {
            echo("\tOK: CounterMonitor got RUNTIME_ERROR notification!");
        } else {
            echo("\tKO: CounterMonitor did not get " + "RUNTIME_ERROR notification!");
            return 1;
        }
    } finally {
        messageReceived = false;
        if (counterMonitor != null)
            counterMonitor.stop();
    }
    return 0;
}
Also used : CounterMonitor(javax.management.monitor.CounterMonitor) ObjectName(javax.management.ObjectName)

Example 5 with CounterMonitor

use of javax.management.monitor.CounterMonitor in project camel by apache.

the class JMXMonitorConsumer method addNotificationListener.

@Override
protected void addNotificationListener() throws Exception {
    JMXEndpoint ep = (JMXEndpoint) getEndpoint();
    // create the monitor bean
    Monitor bean = null;
    if (ep.getMonitorType().equals("counter")) {
        CounterMonitor counter = new CounterMonitor();
        Number initThreshold = convertNumberToAttributeType(ep.getInitThreshold(), ep.getJMXObjectName(), ep.getObservedAttribute());
        Number offset = convertNumberToAttributeType(ep.getOffset(), ep.getJMXObjectName(), ep.getObservedAttribute());
        Number modulus = convertNumberToAttributeType(ep.getModulus(), ep.getJMXObjectName(), ep.getObservedAttribute());
        counter.setInitThreshold(initThreshold);
        counter.setOffset(offset);
        counter.setModulus(modulus);
        counter.setDifferenceMode(ep.isDifferenceMode());
        counter.setNotify(true);
        bean = counter;
    } else if (ep.getMonitorType().equals("gauge")) {
        GaugeMonitor gm = new GaugeMonitor();
        gm.setNotifyHigh(ep.isNotifyHigh());
        gm.setNotifyLow(ep.isNotifyLow());
        gm.setDifferenceMode(ep.isDifferenceMode());
        Number highValue = convertNumberToAttributeType(ep.getThresholdHigh(), ep.getJMXObjectName(), ep.getObservedAttribute());
        Number lowValue = convertNumberToAttributeType(ep.getThresholdLow(), ep.getJMXObjectName(), ep.getObservedAttribute());
        gm.setThresholds(highValue, lowValue);
        bean = gm;
    } else if (ep.getMonitorType().equals("string")) {
        StringMonitor sm = new StringMonitor();
        sm.setNotifyDiffer(ep.isNotifyDiffer());
        sm.setNotifyMatch(ep.isNotifyMatch());
        sm.setStringToCompare(ep.getStringToCompare());
        bean = sm;
    }
    bean.addObservedObject(ep.getJMXObjectName());
    bean.setObservedAttribute(ep.getObservedAttribute());
    bean.setGranularityPeriod(ep.getGranularityPeriod());
    // register the bean
    mMonitorObjectName = new ObjectName(ep.getObjectDomain(), "name", "camel-jmx-monitor-" + UUID.randomUUID());
    ManagementFactory.getPlatformMBeanServer().registerMBean(bean, mMonitorObjectName);
    // add ourselves as a listener to it
    NotificationFilter nf = ep.getNotificationFilter();
    getServerConnection().addNotificationListener(mMonitorObjectName, this, nf, bean);
    bean.start();
}
Also used : StringMonitor(javax.management.monitor.StringMonitor) StringMonitor(javax.management.monitor.StringMonitor) CounterMonitor(javax.management.monitor.CounterMonitor) Monitor(javax.management.monitor.Monitor) GaugeMonitor(javax.management.monitor.GaugeMonitor) GaugeMonitor(javax.management.monitor.GaugeMonitor) CounterMonitor(javax.management.monitor.CounterMonitor) NotificationFilter(javax.management.NotificationFilter) ObjectName(javax.management.ObjectName)

Aggregations

ObjectName (javax.management.ObjectName)10 CounterMonitor (javax.management.monitor.CounterMonitor)10 MBeanServer (javax.management.MBeanServer)7 GaugeMonitor (javax.management.monitor.GaugeMonitor)5 Monitor (javax.management.monitor.Monitor)4 StringMonitor (javax.management.monitor.StringMonitor)4 NotificationEmitter (javax.management.NotificationEmitter)2 NotificationListener (javax.management.NotificationListener)2 CounterMonitorMBean (javax.management.monitor.CounterMonitorMBean)2 PrivilegedAction (java.security.PrivilegedAction)1 NotificationFilter (javax.management.NotificationFilter)1 JMXPrincipal (javax.management.remote.JMXPrincipal)1 Subject (javax.security.auth.Subject)1