Search in sources :

Example 1 with ProcessingCapacityNotificationInfo

use of com.ibm.lang.management.ProcessingCapacityNotificationInfo in project openj9 by eclipse.

the class TestManagementFactory method testExtOperatingSystemMXBeanProxyDoesRemoveListeners.

/**
 * For IBM extensions on the OperatingSystemMXBean
 */
@Test
public void testExtOperatingSystemMXBeanProxyDoesRemoveListeners() {
    try {
        com.ibm.lang.management.OperatingSystemMXBean proxy = ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), "java.lang:type=OperatingSystem", getOperatingSystemInterface());
        OperatingSystemMXBean stdProxy = proxy;
        AssertJUnit.assertNotNull(stdProxy);
        AssertJUnit.assertFalse(proxy instanceof com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl);
        AssertJUnit.assertTrue(proxy instanceof NotificationEmitter);
        NotificationEmitter proxyEmitter = (NotificationEmitter) proxy;
        MyTestListener listener = new MyTestListener();
        proxyEmitter.addNotificationListener(listener, null, null);
        // Fire off a notification and ensure that the listener receives it.
        try {
            ProcessingCapacityNotificationInfo info = new ProcessingCapacityNotificationInfo(240);
            CompositeData cd = TestUtil.toCompositeData(info);
            Notification notification = new Notification(ProcessingCapacityNotificationInfo.PROCESSING_CAPACITY_CHANGE, new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME), 12);
            notification.setUserData(cd);
            ((ExtendedOperatingSystemMXBeanImpl) (ManagementFactory.getOperatingSystemMXBean())).sendNotification(notification);
            AssertJUnit.assertEquals(1, listener.getNotificationsReceivedCount());
            // Verify the user data of the notification.
            Notification n = listener.getNotification();
            AssertJUnit.assertNotNull(n);
            verifyProcessingCapacityNotificationUserData(n.getUserData());
            // Remove the listener
            proxyEmitter.removeNotificationListener(listener);
            // Fire off a notification and ensure that the listener does
            // *not* receive it.
            listener.resetNotificationsReceivedCount();
            notification = new Notification(ProcessingCapacityNotificationInfo.PROCESSING_CAPACITY_CHANGE, new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME), 13);
            notification.setUserData(cd);
            ((ExtendedOperatingSystemMXBeanImpl) (ManagementFactory.getOperatingSystemMXBean())).sendNotification(notification);
            AssertJUnit.assertEquals(0, listener.getNotificationsReceivedCount());
            // ListenerNotFoundException being thrown.
            try {
                proxyEmitter.removeNotificationListener(listener);
                Assert.fail("Should have thrown a ListenerNotFoundException!");
            } catch (ListenerNotFoundException e) {
                logger.debug("ListenerNotFoundException occurred, as expected: " + e.getMessage());
            }
        } catch (MalformedObjectNameException e) {
            Assert.fail("Unexpected MalformedObjectNameException : " + e.getMessage());
            e.printStackTrace();
        }
    } catch (ListenerNotFoundException e) {
        Assert.fail("Unexpected ListenerNotFoundException : " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        Assert.fail("Unexpected IOException : " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) CompositeData(javax.management.openmbean.CompositeData) IOException(java.io.IOException) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) NotificationEmitter(javax.management.NotificationEmitter) ExtendedOperatingSystemMXBeanImpl(com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl) ProcessingCapacityNotificationInfo(com.ibm.lang.management.ProcessingCapacityNotificationInfo) ListenerNotFoundException(javax.management.ListenerNotFoundException) ExtendedOperatingSystemMXBeanImpl(com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) Test(org.testng.annotations.Test)

Example 2 with ProcessingCapacityNotificationInfo

use of com.ibm.lang.management.ProcessingCapacityNotificationInfo in project openj9 by eclipse.

the class TestManagementFactory method testExtOperatingSystemMXBeanProxyEmitsNotifications.

/**
 * For IBM extension on the OperatingSystemMXBean
 */
@Test
public void testExtOperatingSystemMXBeanProxyEmitsNotifications() {
    try {
        com.ibm.lang.management.OperatingSystemMXBean proxy = ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), "java.lang:type=OperatingSystem", getOperatingSystemInterface());
        OperatingSystemMXBean stdProxy = proxy;
        AssertJUnit.assertNotNull(stdProxy);
        AssertJUnit.assertFalse(proxy instanceof com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl);
        AssertJUnit.assertTrue(proxy instanceof NotificationEmitter);
        NotificationEmitter proxyEmitter = (NotificationEmitter) proxy;
        // Add a listener with a handback object.
        MyTestListener listener = new MyTestListener();
        ArrayList<String> arr = new ArrayList<>();
        arr.add("Save your money for the children.");
        proxyEmitter.addNotificationListener(listener, null, arr);
        // Fire off a notification and ensure that the listener receives it.
        try {
            ProcessingCapacityNotificationInfo info = new ProcessingCapacityNotificationInfo(24);
            CompositeData cd = TestUtil.toCompositeData(info);
            Notification notification = new Notification(ProcessingCapacityNotificationInfo.PROCESSING_CAPACITY_CHANGE, new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME), 12);
            notification.setUserData(cd);
            ((ExtendedOperatingSystemMXBeanImpl) (ManagementFactory.getOperatingSystemMXBean())).sendNotification(notification);
            AssertJUnit.assertEquals(1, listener.getNotificationsReceivedCount());
            // Verify that the handback is as expected.
            AssertJUnit.assertNotNull(listener.getHandback());
            AssertJUnit.assertSame(arr, listener.getHandback());
            ArrayList<?> arr2 = (ArrayList<?>) listener.getHandback();
            AssertJUnit.assertEquals(1, arr2.size());
            AssertJUnit.assertEquals("Save your money for the children.", arr2.get(0));
        } catch (MalformedObjectNameException e) {
            Assert.fail("Unexpected MalformedObjectNameException : " + e.getMessage());
            e.printStackTrace();
        }
    } catch (IOException e) {
        Assert.fail("Unexpected IOException : " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) NotificationEmitter(javax.management.NotificationEmitter) ExtendedOperatingSystemMXBeanImpl(com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl) ProcessingCapacityNotificationInfo(com.ibm.lang.management.ProcessingCapacityNotificationInfo) ExtendedOperatingSystemMXBeanImpl(com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) Test(org.testng.annotations.Test)

Example 3 with ProcessingCapacityNotificationInfo

use of com.ibm.lang.management.ProcessingCapacityNotificationInfo in project openj9 by eclipse.

the class OperatingSystemNotificationThread method dispatchNotificationHelper.

private void dispatchNotificationHelper(int type, long data, long sequenceNumber) {
    if (type == 1) {
        // #CPUs changed
        AvailableProcessorsNotificationInfo info = new AvailableProcessorsNotificationInfo((int) data);
        // $NON-NLS-1$
        Notification n = new Notification(AvailableProcessorsNotificationInfo.AVAILABLE_PROCESSORS_CHANGE, "java.lang:type=OperatingSystem", sequenceNumber);
        n.setUserData(AvailableProcessorsNotificationInfoUtil.toCompositeData(info));
        osBean.sendNotification(n);
    } else if (type == 2) {
        // processing capacity changed
        ProcessingCapacityNotificationInfo info = new ProcessingCapacityNotificationInfo((int) data);
        // $NON-NLS-1$
        Notification n = new Notification(ProcessingCapacityNotificationInfo.PROCESSING_CAPACITY_CHANGE, "java.lang:type=OperatingSystem", sequenceNumber);
        n.setUserData(ProcessingCapacityNotificationInfoUtil.toCompositeData(info));
        osBean.sendNotification(n);
    } else if (type == 3) {
        // total physical memory changed
        TotalPhysicalMemoryNotificationInfo info = new TotalPhysicalMemoryNotificationInfo(data);
        // $NON-NLS-1$
        Notification n = new Notification(TotalPhysicalMemoryNotificationInfo.TOTAL_PHYSICAL_MEMORY_CHANGE, "java.lang:type=OperatingSystem", sequenceNumber);
        n.setUserData(TotalPhysicalMemoryNotificationInfoUtil.toCompositeData(info));
        osBean.sendNotification(n);
    }
}
Also used : TotalPhysicalMemoryNotificationInfo(com.ibm.lang.management.TotalPhysicalMemoryNotificationInfo) ProcessingCapacityNotificationInfo(com.ibm.lang.management.ProcessingCapacityNotificationInfo) AvailableProcessorsNotificationInfo(com.ibm.lang.management.AvailableProcessorsNotificationInfo) Notification(javax.management.Notification)

Example 4 with ProcessingCapacityNotificationInfo

use of com.ibm.lang.management.ProcessingCapacityNotificationInfo in project openj9 by eclipse.

the class TestOperatingSystemMXBean method testProcessingCapacityNotications.

@Test
public void testProcessingCapacityNotications() {
    // Register a listener
    NotificationFilterSupport filter = new NotificationFilterSupport();
    filter.enableType(ProcessingCapacityNotificationInfo.PROCESSING_CAPACITY_CHANGE);
    MyTestListener listener = new MyTestListener();
    this.osb.addNotificationListener(listener, filter, null);
    // Fire off a notification and ensure that the listener receives it.
    try {
        ProcessingCapacityNotificationInfo info = new ProcessingCapacityNotificationInfo(50);
        CompositeData cd = TestUtil.toCompositeData(info);
        Notification notification = new Notification(ProcessingCapacityNotificationInfo.PROCESSING_CAPACITY_CHANGE, new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME), 42);
        notification.setUserData(cd);
        this.osb.sendNotification(notification);
        AssertJUnit.assertEquals(1, listener.getNotificationsReceivedCount());
        // Remove the listener
        osb.removeNotificationListener(listener, filter, null);
        // Fire off a notification and ensure that the listener does
        // *not* receive it.
        listener.resetNotificationsReceivedCount();
        notification = new Notification(ProcessingCapacityNotificationInfo.PROCESSING_CAPACITY_CHANGE, new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME), 43);
        notification.setUserData(cd);
        osb.sendNotification(notification);
        AssertJUnit.assertEquals(0, listener.getNotificationsReceivedCount());
        // ListenerNotFoundException being thrown.
        try {
            osb.removeNotificationListener(listener, filter, null);
            Assert.fail("Should have thrown a ListenerNotFoundException!");
        } catch (Exception e) {
            AssertJUnit.assertTrue(e instanceof ListenerNotFoundException);
        }
    } catch (MalformedObjectNameException e) {
        e.printStackTrace();
        Assert.fail("Unexpected MalformedObjectNameException : " + e.getMessage());
    } catch (ListenerNotFoundException e) {
        e.printStackTrace();
        Assert.fail("Unexpected ListenerNotFoundException : " + e.getMessage());
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) CompositeData(javax.management.openmbean.CompositeData) ProcessingCapacityNotificationInfo(com.ibm.lang.management.ProcessingCapacityNotificationInfo) NotificationFilterSupport(javax.management.NotificationFilterSupport) ListenerNotFoundException(javax.management.ListenerNotFoundException) Notification(javax.management.Notification) AttributeNotFoundException(javax.management.AttributeNotFoundException) IntrospectionException(javax.management.IntrospectionException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanException(javax.management.MBeanException) ListenerNotFoundException(javax.management.ListenerNotFoundException) ObjectName(javax.management.ObjectName) Test(org.testng.annotations.Test)

Example 5 with ProcessingCapacityNotificationInfo

use of com.ibm.lang.management.ProcessingCapacityNotificationInfo in project openj9 by eclipse.

the class TestOperatingSystemMXBean method testAddListenerForProcessingCapacityNotifications.

@Test
public final void testAddListenerForProcessingCapacityNotifications() {
    // Add a listener with a handback object.
    MyTestListener listener = new MyTestListener();
    ArrayList<String> arr = new ArrayList<String>();
    arr.add("Socrates eats hemlock");
    osb.addNotificationListener(listener, null, arr);
    // Fire off a notification and ensure that the listener receives it.
    try {
        ProcessingCapacityNotificationInfo info = new ProcessingCapacityNotificationInfo(50);
        CompositeData cd = TestUtil.toCompositeData(info);
        Notification notification = new Notification(ProcessingCapacityNotificationInfo.PROCESSING_CAPACITY_CHANGE, new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME), 42);
        notification.setUserData(cd);
        this.osb.sendNotification(notification);
        AssertJUnit.assertEquals(1, listener.getNotificationsReceivedCount());
        // Verify that the handback is as expected.
        AssertJUnit.assertNotNull(listener.getHandback());
        AssertJUnit.assertSame(arr, listener.getHandback());
        ArrayList arr2 = (ArrayList) listener.getHandback();
        AssertJUnit.assertTrue(arr2.size() == 1);
        AssertJUnit.assertEquals("Socrates eats hemlock", arr2.get(0));
    } catch (MalformedObjectNameException e) {
        Assert.fail("Unexpected MalformedObjectNameException : " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) ProcessingCapacityNotificationInfo(com.ibm.lang.management.ProcessingCapacityNotificationInfo) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) Test(org.testng.annotations.Test)

Aggregations

ProcessingCapacityNotificationInfo (com.ibm.lang.management.ProcessingCapacityNotificationInfo)5 Notification (javax.management.Notification)5 MalformedObjectNameException (javax.management.MalformedObjectNameException)4 ObjectName (javax.management.ObjectName)4 CompositeData (javax.management.openmbean.CompositeData)4 Test (org.testng.annotations.Test)4 ExtendedOperatingSystemMXBeanImpl (com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl)2 IOException (java.io.IOException)2 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)2 ArrayList (java.util.ArrayList)2 ListenerNotFoundException (javax.management.ListenerNotFoundException)2 NotificationEmitter (javax.management.NotificationEmitter)2 AvailableProcessorsNotificationInfo (com.ibm.lang.management.AvailableProcessorsNotificationInfo)1 TotalPhysicalMemoryNotificationInfo (com.ibm.lang.management.TotalPhysicalMemoryNotificationInfo)1 AttributeNotFoundException (javax.management.AttributeNotFoundException)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 IntrospectionException (javax.management.IntrospectionException)1 MBeanException (javax.management.MBeanException)1 NotificationFilterSupport (javax.management.NotificationFilterSupport)1 ReflectionException (javax.management.ReflectionException)1