Search in sources :

Example 6 with MemoryNotificationInfo

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

the class MyTestListener method testAddNotificationListener.

@Test
public final void testAddNotificationListener() {
    // Add a listener with a handback object.
    MyTestListener listener = new MyTestListener();
    ArrayList<String> arr = new ArrayList<String>();
    arr.add("Hegemony or survival ?");
    mb.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 = 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());
        // 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("Hegemony or survival ?", arr2.get(0));
    } catch (MalformedObjectNameException e) {
        Assert.fail("Unexpected MalformedObjectNameException : " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : MemoryNotificationInfo(java.lang.management.MemoryNotificationInfo) MalformedObjectNameException(javax.management.MalformedObjectNameException) CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) MemoryUsage(java.lang.management.MemoryUsage) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) Test(org.testng.annotations.Test)

Example 7 with MemoryNotificationInfo

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

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

use of java.lang.management.MemoryNotificationInfo in project jdk8u_jdk by JetBrains.

the class MemoryNotifInfoCompositeData method badTypeCompositeData.

public static void badTypeCompositeData() throws Exception {
    CompositeType ct = new CompositeType("MyCompositeType", "CompositeType for MemoryNotificationInfo", validItemNames, validItemNames, badItemTypes);
    final Object[] values1 = { "Dummy", "Foo", new Long(100), "Bad memory usage object", "Dummy" };
    CompositeData cd = new CompositeDataSupport(ct, validItemNames, values1);
    try {
        MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);
    } catch (IllegalArgumentException e) {
        System.out.println("Expected exception: " + e.getMessage());
        return;
    }
    throw new RuntimeException("IllegalArgumentException not thrown");
}
Also used : MemoryNotificationInfo(java.lang.management.MemoryNotificationInfo) MemoryUsageCompositeData(sun.management.MemoryUsageCompositeData)

Example 10 with MemoryNotificationInfo

use of java.lang.management.MemoryNotificationInfo in project gephi by gephi.

the class MemoryStarvationManager method handleNotification.

@Override
public void handleNotification(Notification n, Object o) {
    if (messageDelivered) {
        return;
    }
    CompositeData cd = (CompositeData) n.getUserData();
    MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);
    suspendThreads();
    messageDelivered = true;
    // Dialog
    if (canIncreaseMemory()) {
        String messageBundle = NbBundle.getMessage(MemoryStarvationManager.class, "OutOfMemoryError.canIncreaseMemory.message", getMb(Runtime.getRuntime().maxMemory()) + " mb", getMb(getMaximumXmx()) + " mb");
        String titleBundle = NbBundle.getMessage(MemoryStarvationManager.class, "OutOfMemoryError.title");
        String increaseAndRestart = NbBundle.getMessage(MemoryStarvationManager.class, "OutOfMemoryError.canIncreaseMemory.button");
        String cancelBundle = NbBundle.getMessage(MemoryStarvationManager.class, "OutOfMemoryError.cancel");
        NotifyDescriptor msg = new NotifyDescriptor(messageBundle, titleBundle, NotifyDescriptor.YES_NO_CANCEL_OPTION, NotifyDescriptor.ERROR_MESSAGE, new Object[] { increaseAndRestart, cancelBundle }, increaseAndRestart);
        if (DialogDisplayer.getDefault().notify(msg) != increaseAndRestart) {
            resumeThreads();
            return;
        }
        String xmx = getMb(getMaximumXmx()) + "m";
        try {
            updateConfiguration(xmx);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    } else {
        String messageBundle = NbBundle.getMessage(MemoryStarvationManager.class, "OutOfMemoryError.canIncreaseMemory.message", getMb(Runtime.getRuntime().maxMemory()) + " mb");
        String titleBundle = NbBundle.getMessage(MemoryStarvationManager.class, "OutOfMemoryError.title");
        String saveAndRestart = NbBundle.getMessage(MemoryStarvationManager.class, "OutOfMemoryError.canIncreaseMemory.button");
        String cancelBundle = NbBundle.getMessage(MemoryStarvationManager.class, "OutOfMemoryError.cancel");
        NotifyDescriptor msg = new NotifyDescriptor(messageBundle, titleBundle, NotifyDescriptor.YES_NO_CANCEL_OPTION, NotifyDescriptor.ERROR_MESSAGE, new Object[] { saveAndRestart, cancelBundle }, saveAndRestart);
        if (DialogDisplayer.getDefault().notify(msg) != saveAndRestart) {
            resumeThreads();
            return;
        }
    }
    interruptThreads();
    freeSomeMemory();
    saveProject();
    restart();
}
Also used : NotifyDescriptor(org.openide.NotifyDescriptor) MemoryNotificationInfo(java.lang.management.MemoryNotificationInfo) CompositeData(javax.management.openmbean.CompositeData) IOException(java.io.IOException)

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