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();
}
}
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());
}
}
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());
}
}
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");
}
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();
}
Aggregations