Search in sources :

Example 1 with MemoryMXBeanImpl

use of com.ibm.java.lang.management.internal.MemoryMXBeanImpl in project openj9 by eclipse.

the class TestManagementFactory method testExtMemoryMXBeanProxyNotificationEmitter.

/**
 * For IBM extensions on the MemoryMXBean
 */
@Test
public void testExtMemoryMXBeanProxyNotificationEmitter() {
    try {
        com.ibm.lang.management.MemoryMXBean proxy = ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), "java.lang:type=Memory", com.ibm.lang.management.MemoryMXBean.class);
        MemoryMXBean stdProxy = proxy;
        AssertJUnit.assertNotNull(stdProxy);
        // Verify that the proxy is acting as a NotificationEmitter
        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 {
            MemoryUsage mu = new MemoryUsage(1, 2, 3, 4);
            MemoryNotificationInfo info = new MemoryNotificationInfo("Bob", mu, 42);
            CompositeData cd = MemoryNotificationInfoUtil.toCompositeData(info);
            Notification notification = new Notification(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
            notification.setUserData(cd);
            ((MemoryMXBeanImpl) ManagementFactory.getMemoryMXBean()).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 (IllegalArgumentException e) {
        Assert.fail("Unexpected IllegalArgumentException : " + e.getMessage());
    } catch (NullPointerException e) {
        Assert.fail("Unexpected NullPointerException : " + e.getMessage());
    } catch (IOException e) {
        Assert.fail("Unexpected IOException : " + e.getMessage());
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) MemoryMXBeanImpl(com.ibm.java.lang.management.internal.MemoryMXBeanImpl) CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MemoryUsage(java.lang.management.MemoryUsage) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) MemoryNotificationInfo(java.lang.management.MemoryNotificationInfo) MemoryMXBean(java.lang.management.MemoryMXBean) NotificationEmitter(javax.management.NotificationEmitter) Test(org.testng.annotations.Test)

Example 2 with MemoryMXBeanImpl

use of com.ibm.java.lang.management.internal.MemoryMXBeanImpl in project openj9 by eclipse.

the class TestManagementFactory method testMemoryMXBeanProxyNotificationEmitter.

@Test
public void testMemoryMXBeanProxyNotificationEmitter() {
    try {
        MemoryMXBean proxy = ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), "java.lang:type=Memory", MemoryMXBean.class);
        AssertJUnit.assertNotNull(proxy);
        // Verify that the proxy is acting as a NotificationEmitter
        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<String>();
        arr.add("Rock on Tommy !!!");
        proxyEmitter.addNotificationListener(listener, null, arr);
        // Fire off a notification and ensure that the listener receives it.
        try {
            MemoryUsage mu = new MemoryUsage(1, 2, 3, 4);
            MemoryNotificationInfo info = new MemoryNotificationInfo("Tim", mu, 42);
            CompositeData cd = MemoryNotificationInfoUtil.toCompositeData(info);
            Notification notification = new Notification(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
            notification.setUserData(cd);
            ((MemoryMXBeanImpl) ManagementFactory.getMemoryMXBean()).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("Rock on Tommy !!!", arr2.get(0));
        } catch (MalformedObjectNameException e) {
            Assert.fail("Unexpected MalformedObjectNameException : " + e.getMessage());
            e.printStackTrace();
        }
    } catch (IllegalArgumentException e) {
        Assert.fail("Unexpected IllegalArgumentException : " + e.getMessage());
    } catch (NullPointerException e) {
        Assert.fail("Unexpected NullPointerException : " + e.getMessage());
    } catch (IOException e) {
        Assert.fail("Unexpected IOException : " + e.getMessage());
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) MemoryMXBeanImpl(com.ibm.java.lang.management.internal.MemoryMXBeanImpl) CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MemoryUsage(java.lang.management.MemoryUsage) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) MemoryNotificationInfo(java.lang.management.MemoryNotificationInfo) MemoryMXBean(java.lang.management.MemoryMXBean) NotificationEmitter(javax.management.NotificationEmitter) Test(org.testng.annotations.Test)

Example 3 with MemoryMXBeanImpl

use of com.ibm.java.lang.management.internal.MemoryMXBeanImpl in project openj9 by eclipse.

the class TestManagementFactory method testExtMemoryMXBeanProxyNotificationEmitterRemoveListeners.

/**
 * For IBM extensions on the MemoryMXBean
 */
@Test
public void testExtMemoryMXBeanProxyNotificationEmitterRemoveListeners() {
    try {
        com.ibm.lang.management.MemoryMXBean proxy = ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), "java.lang:type=Memory", com.ibm.lang.management.MemoryMXBean.class);
        MemoryMXBean stdProxy = proxy;
        AssertJUnit.assertNotNull(stdProxy);
        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.
        MemoryUsage mu = new MemoryUsage(1, 2, 3, 4);
        MemoryNotificationInfo info = new MemoryNotificationInfo("Marcel", mu, 42);
        CompositeData cd = MemoryNotificationInfoUtil.toCompositeData(info);
        Notification notification = new Notification(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
        notification.setUserData(cd);
        ((MemoryMXBeanImpl) ManagementFactory.getMemoryMXBean()).sendNotification(notification);
        AssertJUnit.assertEquals(1, listener.getNotificationsReceivedCount());
        // Verify that the handback is as expected.
        AssertJUnit.assertNull(listener.getHandback());
        // Verify the user data of the notification.
        Notification n = listener.getNotification();
        AssertJUnit.assertNotNull(n);
        verifyMemoryNotificationUserData(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(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 43);
        notification.setUserData(cd);
        ((MemoryMXBeanImpl) ManagementFactory.getMemoryMXBean()).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) MemoryMXBeanImpl(com.ibm.java.lang.management.internal.MemoryMXBeanImpl) CompositeData(javax.management.openmbean.CompositeData) IOException(java.io.IOException) MemoryUsage(java.lang.management.MemoryUsage) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) MemoryNotificationInfo(java.lang.management.MemoryNotificationInfo) MemoryMXBean(java.lang.management.MemoryMXBean) NotificationEmitter(javax.management.NotificationEmitter) ListenerNotFoundException(javax.management.ListenerNotFoundException) Test(org.testng.annotations.Test)

Example 4 with MemoryMXBeanImpl

use of com.ibm.java.lang.management.internal.MemoryMXBeanImpl in project openj9 by eclipse.

the class TestManagementFactory method testMemoryMXBeanProxyNotificationEmitterRemoveListeners.

@Test
public void testMemoryMXBeanProxyNotificationEmitterRemoveListeners() {
    try {
        MemoryMXBean proxy = ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), "java.lang:type=Memory", MemoryMXBean.class);
        AssertJUnit.assertNotNull(proxy);
        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.
        MemoryUsage mu = new MemoryUsage(1, 2, 3, 4);
        MemoryNotificationInfo info = new MemoryNotificationInfo("Marcel", mu, 42);
        CompositeData cd = MemoryNotificationInfoUtil.toCompositeData(info);
        Notification notification = new Notification(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
        notification.setUserData(cd);
        ((MemoryMXBeanImpl) ManagementFactory.getMemoryMXBean()).sendNotification(notification);
        AssertJUnit.assertEquals(1, listener.getNotificationsReceivedCount());
        // Verify that the handback is as expected.
        AssertJUnit.assertNull(listener.getHandback());
        // Verify the user data of the notification.
        Notification n = listener.getNotification();
        AssertJUnit.assertNotNull(n);
        verifyMemoryNotificationUserData(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(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 43);
        notification.setUserData(cd);
        ((MemoryMXBeanImpl) ManagementFactory.getMemoryMXBean()).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 : MemoryNotificationInfo(java.lang.management.MemoryNotificationInfo) MalformedObjectNameException(javax.management.MalformedObjectNameException) MemoryMXBean(java.lang.management.MemoryMXBean) NotificationEmitter(javax.management.NotificationEmitter) MemoryMXBeanImpl(com.ibm.java.lang.management.internal.MemoryMXBeanImpl) CompositeData(javax.management.openmbean.CompositeData) ListenerNotFoundException(javax.management.ListenerNotFoundException) IOException(java.io.IOException) MemoryUsage(java.lang.management.MemoryUsage) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) Test(org.testng.annotations.Test)

Aggregations

MemoryMXBeanImpl (com.ibm.java.lang.management.internal.MemoryMXBeanImpl)4 IOException (java.io.IOException)4 MemoryMXBean (java.lang.management.MemoryMXBean)4 MemoryNotificationInfo (java.lang.management.MemoryNotificationInfo)4 MemoryUsage (java.lang.management.MemoryUsage)4 MalformedObjectNameException (javax.management.MalformedObjectNameException)4 Notification (javax.management.Notification)4 NotificationEmitter (javax.management.NotificationEmitter)4 ObjectName (javax.management.ObjectName)4 CompositeData (javax.management.openmbean.CompositeData)4 Test (org.testng.annotations.Test)4 ArrayList (java.util.ArrayList)2 ListenerNotFoundException (javax.management.ListenerNotFoundException)2