Search in sources :

Example 11 with MemoryNotificationInfo

use of java.lang.management.MemoryNotificationInfo 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 = TestUtil.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 12 with MemoryNotificationInfo

use of java.lang.management.MemoryNotificationInfo 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 = TestUtil.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)

Example 13 with MemoryNotificationInfo

use of java.lang.management.MemoryNotificationInfo in project openj9 by eclipse.

the class MyTestListener method testRemoveNotificationListenerNotificationListenerNotificationFilterObject.

// -----------------------------------------------------------------
// Notification implementation tests follow ....
// -----------------------------------------------------------------
/*
	 * Class under test for void
	 * removeNotificationListener(NotificationListener, NotificationFilter,
	 * Object)
	 */
@Test
public final void testRemoveNotificationListenerNotificationListenerNotificationFilterObject() {
    // Register a listener
    NotificationFilterSupport filter = new NotificationFilterSupport();
    filter.enableType(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED);
    MyTestListener listener = new MyTestListener();
    mb.addNotificationListener(listener, filter, null);
    // 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 = TestUtil.toCompositeData(info);
        Notification notification = new Notification(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
        notification.setUserData(cd);
        mb.sendNotification(notification);
        AssertJUnit.assertEquals(1, listener.getNotificationsReceivedCount());
        // Remove the listener
        mb.removeNotificationListener(listener, filter, null);
        // 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);
        mb.sendNotification(notification);
        AssertJUnit.assertEquals(0, listener.getNotificationsReceivedCount());
        // ListenerNotFoundException being thrown.
        try {
            mb.removeNotificationListener(listener, filter, null);
            Assert.fail("Should have thrown a ListenerNotFoundException!");
        } catch (ListenerNotFoundException e) {
        }
    } catch (MalformedObjectNameException e) {
        Assert.fail("Unexpected MalformedObjectNameException : " + e.getMessage());
        e.printStackTrace();
    } catch (ListenerNotFoundException e) {
        Assert.fail("Unexpected ListenerNotFoundException : " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : MemoryNotificationInfo(java.lang.management.MemoryNotificationInfo) MalformedObjectNameException(javax.management.MalformedObjectNameException) CompositeData(javax.management.openmbean.CompositeData) NotificationFilterSupport(javax.management.NotificationFilterSupport) ListenerNotFoundException(javax.management.ListenerNotFoundException) MemoryUsage(java.lang.management.MemoryUsage) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) Test(org.testng.annotations.Test)

Example 14 with MemoryNotificationInfo

use of java.lang.management.MemoryNotificationInfo in project openj9 by eclipse.

the class TestMemoryNotificationInfo method setUp.

@BeforeClass
protected void setUp() throws Exception {
    goodMU = new MemoryUsage(MU_INIT_VAL, MU_USED_VAL, MU_COMMITTED_VAL, MU_MAX_VAL);
    goodMNI = new MemoryNotificationInfo(NAME_VAL, goodMU, COUNT_VAL);
    logger.info("Starting TestMemoryNotificationInfo tests ...");
}
Also used : MemoryNotificationInfo(java.lang.management.MemoryNotificationInfo) MemoryUsage(java.lang.management.MemoryUsage) BeforeClass(org.testng.annotations.BeforeClass)

Example 15 with MemoryNotificationInfo

use of java.lang.management.MemoryNotificationInfo in project openj9 by eclipse.

the class TestMemoryNotificationInfo method testFrom.

@Test
public final void testFrom() {
    CompositeData cd = TestUtil.toCompositeData(goodMNI);
    AssertJUnit.assertNotNull(cd);
    MemoryNotificationInfo mni = MemoryNotificationInfo.from(cd);
    AssertJUnit.assertNotNull(mni);
    // Verify that the MemoryNotificationInfo contains what we
    // *think* it contains.
    AssertJUnit.assertEquals(NAME_VAL, mni.getPoolName());
    AssertJUnit.assertEquals(COUNT_VAL, mni.getCount());
    MemoryUsage mu = mni.getUsage();
    AssertJUnit.assertNotNull(mu);
    AssertJUnit.assertEquals(MU_INIT_VAL, mu.getInit());
    AssertJUnit.assertEquals(MU_COMMITTED_VAL, mu.getCommitted());
    AssertJUnit.assertEquals(MU_MAX_VAL, mu.getMax());
    AssertJUnit.assertEquals(MU_USED_VAL, mu.getUsed());
}
Also used : MemoryNotificationInfo(java.lang.management.MemoryNotificationInfo) CompositeData(javax.management.openmbean.CompositeData) MemoryUsage(java.lang.management.MemoryUsage) Test(org.testng.annotations.Test)

Aggregations

MemoryNotificationInfo (java.lang.management.MemoryNotificationInfo)16 MemoryUsage (java.lang.management.MemoryUsage)11 CompositeData (javax.management.openmbean.CompositeData)11 Notification (javax.management.Notification)10 Test (org.testng.annotations.Test)8 MalformedObjectNameException (javax.management.MalformedObjectNameException)7 ObjectName (javax.management.ObjectName)7 IOException (java.io.IOException)5 MemoryMXBeanImpl (com.ibm.java.lang.management.internal.MemoryMXBeanImpl)4 MemoryMXBean (java.lang.management.MemoryMXBean)4 ListenerNotFoundException (javax.management.ListenerNotFoundException)4 NotificationEmitter (javax.management.NotificationEmitter)4 ArrayList (java.util.ArrayList)3 MemoryUsageCompositeData (sun.management.MemoryUsageCompositeData)3 LowMemoryHandler (com.sun.btrace.shared.LowMemoryHandler)1 NotificationFilterSupport (javax.management.NotificationFilterSupport)1 NotificationListener (javax.management.NotificationListener)1 NotifyDescriptor (org.openide.NotifyDescriptor)1 BeforeClass (org.testng.annotations.BeforeClass)1