Java Code Examples for java.lang.management.MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED

The following are Jave code examples for showing how to use MEMORY_THRESHOLD_EXCEEDED of the java.lang.management.MemoryNotificationInfo class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: javify   File: MemoryMXBeanImpl.java   View Source Code Vote up 5 votes
public MBeanNotificationInfo[] getNotificationInfo()
{
  return new MBeanNotificationInfo[]
    {
      new MBeanNotificationInfo(new String[]
        {
          MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED,
          MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED
        },
                                Notification.class.getName(),
                                "Memory Usage Notifications")
    };
}
 
Example 2
Project: jvm-stm   File: MemoryMXBeanImpl.java   View Source Code Vote up 5 votes
public MBeanNotificationInfo[] getNotificationInfo()
 {
   return new MBeanNotificationInfo[]
     {
new MBeanNotificationInfo(new String[]
  {
    MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED,
    MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED
  },
			  Notification.class.getName(), 
			  "Memory Usage Notifications")
     };
 }
 
Example 3
Project: cn1   File: ManagementUtils.java   View Source Code Vote up 5 votes
/**
 * Creates the metadata for the {@link java.lang.management.MemoryMXBean}.
 * For this type of platform bean the metadata covers :
 * <ul>
 * <li>4 attributes
 * <li>0 constructors
 * <li>1 operations
 * <li>1 notification
 * </ul>
 * 
 * @param map
 */
private static void addMemoryBeanInfo(HashMap<String, MBeanInfo> map) {
    // Attributes
    MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[4];
    attributes[0] = new MBeanAttributeInfo("HeapMemoryUsage",
            CompositeData.class.getName(), "HeapMemoryUsage", true, false,
            false);
    attributes[1] = new MBeanAttributeInfo("NonHeapMemoryUsage",
            CompositeData.class.getName(), "NonHeapMemoryUsage", true,
            false, false);
    attributes[2] = new MBeanAttributeInfo(
            "ObjectPendingFinalizationCount", Integer.TYPE.getName(),
            "ObjectPendingFinalizationCount", true, false, false);
    attributes[3] = new MBeanAttributeInfo("Verbose", Boolean.TYPE
            .getName(), "Verbose", true, true, true);

    // Operations
    MBeanOperationInfo[] operations = new MBeanOperationInfo[1];
    MBeanParameterInfo[] params = new MBeanParameterInfo[0];
    operations[0] = new MBeanOperationInfo("gc", "gc", params, Void.TYPE
            .getName(), MBeanOperationInfo.ACTION_INFO);

    // Notifications
    MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[1];
    String[] notifTypes = new String[2];
    notifTypes[0] = MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED;
    notifTypes[1] = MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED;
    notifications[0] = new MBeanNotificationInfo(notifTypes,
            javax.management.Notification.class.getName(),
            "Memory Notification");

    MBeanInfo mbeanInfo = new MBeanInfo(MemoryMXBeanImpl.class.getName(),
            MemoryMXBeanImpl.class.getName(), attributes, null, operations,
            notifications);
    map.put(MemoryMXBean.class.getName(), mbeanInfo);
}
 
Example 4
Project: cn1   File: MemoryMXBeanImplTest.java   View Source Code Vote up 5 votes
public final void testAddNotificationListener() throws Exception {
    // Add a listener with a handback object.
    SimpleTestListener listener = new SimpleTestListener();
    ArrayList<String> arr = new ArrayList<String>();
    arr.add("Hegemony or survival ?");
    notifierBean.addNotificationListener(listener, null, arr);

    // Fire off a notification and ensure that the listener receives it.
    MemoryUsage mu = new MemoryUsage(1, 2, 3, 4);
    MemoryNotificationInfo info = new MemoryNotificationInfo("Lloyd", mu,
            42);
    CompositeData cd = ManagementUtils
            .toMemoryNotificationInfoCompositeData(info);
    Notification notification = new Notification(
            MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
            new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
    notification.setUserData(cd);
    notifierBean.sendNotification(notification);
    assertEquals(1, listener.getNotificationsReceivedCount());

    // Verify that the handback is as expected.
    assertNotNull(listener.getHandback());
    assertSame(arr, listener.getHandback());
    ArrayList arr2 = (ArrayList) listener.getHandback();
    assertTrue(arr2.size() == 1);
    assertEquals("Hegemony or survival ?", arr2.get(0));
}
 
Example 5
Project: JamVM-PH   File: MemoryMXBeanImpl.java   View Source Code Vote up 5 votes
public MBeanNotificationInfo[] getNotificationInfo()
 {
   return new MBeanNotificationInfo[]
     {
new MBeanNotificationInfo(new String[]
  {
    MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED,
    MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED
  },
			  Notification.class.getName(), 
			  "Memory Usage Notifications")
     };
 }
 
Example 6
Project: classpath   File: MemoryMXBeanImpl.java   View Source Code Vote up 5 votes
public MBeanNotificationInfo[] getNotificationInfo()
{
  return new MBeanNotificationInfo[]
    {
      new MBeanNotificationInfo(new String[]
        {
          MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED,
          MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED
        },
                                Notification.class.getName(),
                                "Memory Usage Notifications")
    };
}
 
Example 7
Project: freeVM   File: ManagementUtils.java   View Source Code Vote up 5 votes
/**
 * Creates the metadata for the {@link java.lang.management.MemoryMXBean}.
 * For this type of platform bean the metadata covers :
 * <ul>
 * <li>4 attributes
 * <li>0 constructors
 * <li>1 operations
 * <li>1 notification
 * </ul>
 * 
 * @param map
 */
private static void addMemoryBeanInfo(HashMap<String, MBeanInfo> map) {
    // Attributes
    MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[4];
    attributes[0] = new MBeanAttributeInfo("HeapMemoryUsage",
            CompositeData.class.getName(), "HeapMemoryUsage", true, false,
            false);
    attributes[1] = new MBeanAttributeInfo("NonHeapMemoryUsage",
            CompositeData.class.getName(), "NonHeapMemoryUsage", true,
            false, false);
    attributes[2] = new MBeanAttributeInfo(
            "ObjectPendingFinalizationCount", Integer.TYPE.getName(),
            "ObjectPendingFinalizationCount", true, false, false);
    attributes[3] = new MBeanAttributeInfo("Verbose", Boolean.TYPE
            .getName(), "Verbose", true, true, true);

    // Operations
    MBeanOperationInfo[] operations = new MBeanOperationInfo[1];
    MBeanParameterInfo[] params = new MBeanParameterInfo[0];
    operations[0] = new MBeanOperationInfo("gc", "gc", params, Void.TYPE
            .getName(), MBeanOperationInfo.ACTION_INFO);

    // Notifications
    MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[1];
    String[] notifTypes = new String[2];
    notifTypes[0] = MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED;
    notifTypes[1] = MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED;
    notifications[0] = new MBeanNotificationInfo(notifTypes,
            javax.management.Notification.class.getName(),
            "Memory Notification");

    MBeanInfo mbeanInfo = new MBeanInfo(MemoryMXBeanImpl.class.getName(),
            MemoryMXBeanImpl.class.getName(), attributes, null, operations,
            notifications);
    map.put(MemoryMXBean.class.getName(), mbeanInfo);
}
 
Example 8
Project: freeVM   File: MemoryMXBeanImplTest.java   View Source Code Vote up 5 votes
public final void testAddNotificationListener() throws Exception {
    // Add a listener with a handback object.
    SimpleTestListener listener = new SimpleTestListener();
    ArrayList<String> arr = new ArrayList<String>();
    arr.add("Hegemony or survival ?");
    notifierBean.addNotificationListener(listener, null, arr);

    // Fire off a notification and ensure that the listener receives it.
    MemoryUsage mu = new MemoryUsage(1, 2, 3, 4);
    MemoryNotificationInfo info = new MemoryNotificationInfo("Lloyd", mu,
            42);
    CompositeData cd = ManagementUtils
            .toMemoryNotificationInfoCompositeData(info);
    Notification notification = new Notification(
            MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
            new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
    notification.setUserData(cd);
    notifierBean.sendNotification(notification);
    assertEquals(1, listener.getNotificationsReceivedCount());

    // Verify that the handback is as expected.
    assertNotNull(listener.getHandback());
    assertSame(arr, listener.getHandback());
    ArrayList arr2 = (ArrayList) listener.getHandback();
    assertTrue(arr2.size() == 1);
    assertEquals("Hegemony or survival ?", arr2.get(0));
}
 
Example 9
Project: freeVM   File: ManagementUtils.java   View Source Code Vote up 5 votes
/**
 * Creates the metadata for the {@link java.lang.management.MemoryMXBean}.
 * For this type of platform bean the metadata covers :
 * <ul>
 * <li>4 attributes
 * <li>0 constructors
 * <li>1 operations
 * <li>1 notification
 * </ul>
 * 
 * @param map
 */
private static void addMemoryBeanInfo(HashMap<String, MBeanInfo> map) {
    // Attributes
    MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[4];
    attributes[0] = new MBeanAttributeInfo("HeapMemoryUsage",
            CompositeData.class.getName(), "HeapMemoryUsage", true, false,
            false);
    attributes[1] = new MBeanAttributeInfo("NonHeapMemoryUsage",
            CompositeData.class.getName(), "NonHeapMemoryUsage", true,
            false, false);
    attributes[2] = new MBeanAttributeInfo(
            "ObjectPendingFinalizationCount", Integer.TYPE.getName(),
            "ObjectPendingFinalizationCount", true, false, false);
    attributes[3] = new MBeanAttributeInfo("Verbose", Boolean.TYPE
            .getName(), "Verbose", true, true, true);

    // Operations
    MBeanOperationInfo[] operations = new MBeanOperationInfo[1];
    MBeanParameterInfo[] params = new MBeanParameterInfo[0];
    operations[0] = new MBeanOperationInfo("gc", "gc", params, Void.TYPE
            .getName(), MBeanOperationInfo.ACTION_INFO);

    // Notifications
    MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[1];
    String[] notifTypes = new String[2];
    notifTypes[0] = MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED;
    notifTypes[1] = MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED;
    notifications[0] = new MBeanNotificationInfo(notifTypes,
            javax.management.Notification.class.getName(),
            "Memory Notification");

    MBeanInfo mbeanInfo = new MBeanInfo(MemoryMXBeanImpl.class.getName(),
            MemoryMXBeanImpl.class.getName(), attributes, null, operations,
            notifications);
    map.put(MemoryMXBean.class.getName(), mbeanInfo);
}
 
Example 10
Project: freeVM   File: MemoryMXBeanImplTest.java   View Source Code Vote up 5 votes
public final void testAddNotificationListener() throws Exception {
    // Add a listener with a handback object.
    SimpleTestListener listener = new SimpleTestListener();
    ArrayList<String> arr = new ArrayList<String>();
    arr.add("Hegemony or survival ?");
    notifierBean.addNotificationListener(listener, null, arr);

    // Fire off a notification and ensure that the listener receives it.
    MemoryUsage mu = new MemoryUsage(1, 2, 3, 4);
    MemoryNotificationInfo info = new MemoryNotificationInfo("Lloyd", mu,
            42);
    CompositeData cd = ManagementUtils
            .toMemoryNotificationInfoCompositeData(info);
    Notification notification = new Notification(
            MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
            new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
    notification.setUserData(cd);
    notifierBean.sendNotification(notification);
    assertEquals(1, listener.getNotificationsReceivedCount());

    // Verify that the handback is as expected.
    assertNotNull(listener.getHandback());
    assertSame(arr, listener.getHandback());
    ArrayList arr2 = (ArrayList) listener.getHandback();
    assertTrue(arr2.size() == 1);
    assertEquals("Hegemony or survival ?", arr2.get(0));
}
 
Example 11
Project: cn1   File: MemoryNotificationThread.java   View Source Code Vote up 4 votes
/**
 * A helper method called from within the native
 * {@link #processNotificationLoop(int)} method to construct and dispatch
 * notification objects.
 * 
 * @param min
 *            the initial amount in bytes of memory that can be allocated by
 *            this virtual machine
 * @param used
 *            the number of bytes currently used for memory
 * @param committed
 *            the number of bytes of committed memory
 * @param max
 *            the maximum number of bytes that can be used for memory
 *            management purposes
 * @param count
 *            the number of times that the memory usage of the memory pool
 *            in question has met or exceeded the relevant threshold
 * @param sequenceNumber
 *            the sequence identifier of the current notification
 * @param isCollectionUsageNotification
 *            a <code>boolean</code> indication of whether or not the new
 *            notification is as a result of the collection threshold being
 *            exceeded. If this value is <code>false</code> then the
 *            implication is that a memory threshold has been exceeded.
 */
@SuppressWarnings("unused")
// IMPORTANT: for use by VM
private void dispatchNotificationHelper(long min, long used,
        long committed, long max, long count, long sequenceNumber,
        boolean isCollectionUsageNotification) {
    MemoryNotificationInfo info = new MemoryNotificationInfo(memPool
            .getName(), new MemoryUsage(min, used, committed, max), count);
    Notification n = new Notification(
            isCollectionUsageNotification ? MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED
                    : MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
            "java.lang:type=Memory", sequenceNumber);
    n.setUserData(ManagementUtils
            .toMemoryNotificationInfoCompositeData(info));
    memBean.sendNotification(n);
}
 
Example 12
Project: cn1   File: MemoryMXBeanImplTest.java   View Source Code Vote up 4 votes
public final void testRemoveNotificationListenerNotificationListenerNotificationFilterObject()
        throws Exception {
    // Register a listener
    NotificationFilterSupport filter = new NotificationFilterSupport();
    filter.enableType(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED);
    SimpleTestListener listener = new SimpleTestListener();
    notifierBean.addNotificationListener(listener, filter, null);

    // Fire off a notification and ensure that the listener receives it.
    MemoryUsage mu = new MemoryUsage(1, 2, 3, 4);
    MemoryNotificationInfo info = new MemoryNotificationInfo("Tim", mu, 42);
    CompositeData cd = ManagementUtils
            .toMemoryNotificationInfoCompositeData(info);
    Notification notification = new Notification(
            MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
            new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
    notification.setUserData(cd);
    notifierBean.sendNotification(notification);
    assertEquals(1, listener.getNotificationsReceivedCount());

    // Remove the listener
    notifierBean.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);
    notifierBean.sendNotification(notification);
    assertEquals(0, listener.getNotificationsReceivedCount());

    // Try and remove the listener one more time. Should result in a
    // ListenerNotFoundException being thrown.
    try {
        notifierBean.removeNotificationListener(listener, filter, null);
        fail("Should have thrown a ListenerNotFoundException!");
    } catch (ListenerNotFoundException e) {
    }
}
 
Example 13
Project: cn1   File: MemoryMXBeanImplTest.java   View Source Code Vote up 4 votes
public final void testRemoveNotificationListenerNotificationListener()
        throws Exception {
    // Add a listener without a filter object.
    SimpleTestListener listener = new SimpleTestListener();
    notifierBean.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("Sinclair",
            mu, 42);
    CompositeData cd = ManagementUtils
            .toMemoryNotificationInfoCompositeData(info);
    Notification notification = new Notification(
            MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
            new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
    notification.setUserData(cd);
    notifierBean.sendNotification(notification);
    assertEquals(1, listener.getNotificationsReceivedCount());

    // Verify that the handback is as expected.
    assertNull(listener.getHandback());

    // Verify the user data of the notification.
    Notification n = listener.getNotification();
    assertNotNull(n);
    verifyNotificationUserData(n.getUserData());

    // Remove the listener
    notifierBean.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);
    notifierBean.sendNotification(notification);
    assertEquals(0, listener.getNotificationsReceivedCount());

    // Try and remove the listener one more time. Should result in a
    // ListenerNotFoundException being thrown.
    try {
        notifierBean.removeNotificationListener(listener);
        fail("Should have thrown a ListenerNotFoundException!");
    } catch (ListenerNotFoundException ignore) {
    }
}
 
Example 14
Project: tigase-server   File: MemMonitor.java   View Source Code Vote up 4 votes
@Override
public void init(JID jid, float treshold, SystemMonitorTask smTask) {
	super.init(jid, treshold, smTask);
	memoryMXBean = ManagementFactory.getMemoryMXBean();
	NotificationEmitter emitter = (NotificationEmitter)memoryMXBean;
	List<MemoryPoolMXBean> memPools = ManagementFactory.getMemoryPoolMXBeans();
	for (MemoryPoolMXBean memoryPoolMXBean : memPools) {
		try {
			emitter.removeNotificationListener(this, null, memoryPoolMXBean);
		} catch (Exception e) {	}
		MemoryUsage memUsage = memoryPoolMXBean.getUsage();
		if (memUsage != null) {
			if (memoryPoolMXBean.isUsageThresholdSupported()) {
				emitter.addNotificationListener(this, null, memoryPoolMXBean);
				long memUsageTreshold =
								new Double(new Long(memUsage.getMax()).doubleValue() *
								treshold).longValue();
				memoryPoolMXBean.setUsageThreshold(memUsageTreshold);
				log.config("Setting treshold: " + memUsageTreshold +
								" for memory pool: " + memoryPoolMXBean.getName() +
								", type: " + memoryPoolMXBean.getType().toString() +
								", memMax: " + memUsage.getMax() +
								", memUsed: " + memUsage.getUsed() +
								", config treeshold: " + treshold);
				if (memUsage.getUsed() > memUsageTreshold) {
					Notification not = new Notification(
									MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
									this, 1);
					handleNotification(not, memoryPoolMXBean);
				}
			} else {
				log.config("Memory pool name: " + memoryPoolMXBean.getName() +
								", type: " + memoryPoolMXBean.getType().toString() +
								" usage threshold is not supported.");
			}
		} else {
			log.config("Memory pool name: " + memoryPoolMXBean.getName() +
							", type: " + memoryPoolMXBean.getType().toString() +
							" is invalid.");
		}
	}
}
 
Example 15
Project: freeVM   File: MemoryNotificationThread.java   View Source Code Vote up 4 votes
/**
 * A helper method called from within the native
 * {@link #processNotificationLoop(int)} method to construct and dispatch
 * notification objects.
 * 
 * @param min
 *            the initial amount in bytes of memory that can be allocated by
 *            this virtual machine
 * @param used
 *            the number of bytes currently used for memory
 * @param committed
 *            the number of bytes of committed memory
 * @param max
 *            the maximum number of bytes that can be used for memory
 *            management purposes
 * @param count
 *            the number of times that the memory usage of the memory pool
 *            in question has met or exceeded the relevant threshold
 * @param sequenceNumber
 *            the sequence identifier of the current notification
 * @param isCollectionUsageNotification
 *            a <code>boolean</code> indication of whether or not the new
 *            notification is as a result of the collection threshold being
 *            exceeded. If this value is <code>false</code> then the
 *            implication is that a memory threshold has been exceeded.
 */
@SuppressWarnings("unused")
// IMPORTANT: for use by VM
private void dispatchNotificationHelper(long min, long used,
        long committed, long max, long count, long sequenceNumber,
        boolean isCollectionUsageNotification) {
    MemoryNotificationInfo info = new MemoryNotificationInfo(memPool
            .getName(), new MemoryUsage(min, used, committed, max), count);
    Notification n = new Notification(
            isCollectionUsageNotification ? MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED
                    : MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
            "java.lang:type=Memory", sequenceNumber);
    n.setUserData(ManagementUtils
            .toMemoryNotificationInfoCompositeData(info));
    memBean.sendNotification(n);
}
 
Example 16
Project: freeVM   File: MemoryMXBeanImplTest.java   View Source Code Vote up 4 votes
public final void testRemoveNotificationListenerNotificationListenerNotificationFilterObject()
        throws Exception {
    // Register a listener
    NotificationFilterSupport filter = new NotificationFilterSupport();
    filter.enableType(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED);
    SimpleTestListener listener = new SimpleTestListener();
    notifierBean.addNotificationListener(listener, filter, null);

    // Fire off a notification and ensure that the listener receives it.
    MemoryUsage mu = new MemoryUsage(1, 2, 3, 4);
    MemoryNotificationInfo info = new MemoryNotificationInfo("Tim", mu, 42);
    CompositeData cd = ManagementUtils
            .toMemoryNotificationInfoCompositeData(info);
    Notification notification = new Notification(
            MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
            new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
    notification.setUserData(cd);
    notifierBean.sendNotification(notification);
    assertEquals(1, listener.getNotificationsReceivedCount());

    // Remove the listener
    notifierBean.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);
    notifierBean.sendNotification(notification);
    assertEquals(0, listener.getNotificationsReceivedCount());

    // Try and remove the listener one more time. Should result in a
    // ListenerNotFoundException being thrown.
    try {
        notifierBean.removeNotificationListener(listener, filter, null);
        fail("Should have thrown a ListenerNotFoundException!");
    } catch (ListenerNotFoundException e) {
    }
}
 
Example 17
Project: freeVM   File: MemoryMXBeanImplTest.java   View Source Code Vote up 4 votes
public final void testRemoveNotificationListenerNotificationListener()
        throws Exception {
    // Add a listener without a filter object.
    SimpleTestListener listener = new SimpleTestListener();
    notifierBean.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("Sinclair",
            mu, 42);
    CompositeData cd = ManagementUtils
            .toMemoryNotificationInfoCompositeData(info);
    Notification notification = new Notification(
            MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
            new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
    notification.setUserData(cd);
    notifierBean.sendNotification(notification);
    assertEquals(1, listener.getNotificationsReceivedCount());

    // Verify that the handback is as expected.
    assertNull(listener.getHandback());

    // Verify the user data of the notification.
    Notification n = listener.getNotification();
    assertNotNull(n);
    verifyNotificationUserData(n.getUserData());

    // Remove the listener
    notifierBean.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);
    notifierBean.sendNotification(notification);
    assertEquals(0, listener.getNotificationsReceivedCount());

    // Try and remove the listener one more time. Should result in a
    // ListenerNotFoundException being thrown.
    try {
        notifierBean.removeNotificationListener(listener);
        fail("Should have thrown a ListenerNotFoundException!");
    } catch (ListenerNotFoundException ignore) {
    }
}
 
Example 18
Project: freeVM   File: MemoryNotificationThread.java   View Source Code Vote up 4 votes
/**
 * A helper method called from within the native
 * {@link #processNotificationLoop(int)} method to construct and dispatch
 * notification objects.
 * 
 * @param min
 *            the initial amount in bytes of memory that can be allocated by
 *            this virtual machine
 * @param used
 *            the number of bytes currently used for memory
 * @param committed
 *            the number of bytes of committed memory
 * @param max
 *            the maximum number of bytes that can be used for memory
 *            management purposes
 * @param count
 *            the number of times that the memory usage of the memory pool
 *            in question has met or exceeded the relevant threshold
 * @param sequenceNumber
 *            the sequence identifier of the current notification
 * @param isCollectionUsageNotification
 *            a <code>boolean</code> indication of whether or not the new
 *            notification is as a result of the collection threshold being
 *            exceeded. If this value is <code>false</code> then the
 *            implication is that a memory threshold has been exceeded.
 */
@SuppressWarnings("unused")
// IMPORTANT: for use by VM
private void dispatchNotificationHelper(long min, long used,
        long committed, long max, long count, long sequenceNumber,
        boolean isCollectionUsageNotification) {
    MemoryNotificationInfo info = new MemoryNotificationInfo(memPool
            .getName(), new MemoryUsage(min, used, committed, max), count);
    Notification n = new Notification(
            isCollectionUsageNotification ? MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED
                    : MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
            "java.lang:type=Memory", sequenceNumber);
    n.setUserData(ManagementUtils
            .toMemoryNotificationInfoCompositeData(info));
    memBean.sendNotification(n);
}
 
Example 19
Project: freeVM   File: MemoryMXBeanImplTest.java   View Source Code Vote up 4 votes
public final void testRemoveNotificationListenerNotificationListenerNotificationFilterObject()
        throws Exception {
    // Register a listener
    NotificationFilterSupport filter = new NotificationFilterSupport();
    filter.enableType(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED);
    SimpleTestListener listener = new SimpleTestListener();
    notifierBean.addNotificationListener(listener, filter, null);

    // Fire off a notification and ensure that the listener receives it.
    MemoryUsage mu = new MemoryUsage(1, 2, 3, 4);
    MemoryNotificationInfo info = new MemoryNotificationInfo("Tim", mu, 42);
    CompositeData cd = ManagementUtils
            .toMemoryNotificationInfoCompositeData(info);
    Notification notification = new Notification(
            MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
            new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
    notification.setUserData(cd);
    notifierBean.sendNotification(notification);
    assertEquals(1, listener.getNotificationsReceivedCount());

    // Remove the listener
    notifierBean.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);
    notifierBean.sendNotification(notification);
    assertEquals(0, listener.getNotificationsReceivedCount());

    // Try and remove the listener one more time. Should result in a
    // ListenerNotFoundException being thrown.
    try {
        notifierBean.removeNotificationListener(listener, filter, null);
        fail("Should have thrown a ListenerNotFoundException!");
    } catch (ListenerNotFoundException e) {
    }
}
 
Example 20
Project: freeVM   File: MemoryMXBeanImplTest.java   View Source Code Vote up 4 votes
public final void testRemoveNotificationListenerNotificationListener()
        throws Exception {
    // Add a listener without a filter object.
    SimpleTestListener listener = new SimpleTestListener();
    notifierBean.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("Sinclair",
            mu, 42);
    CompositeData cd = ManagementUtils
            .toMemoryNotificationInfoCompositeData(info);
    Notification notification = new Notification(
            MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
            new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
    notification.setUserData(cd);
    notifierBean.sendNotification(notification);
    assertEquals(1, listener.getNotificationsReceivedCount());

    // Verify that the handback is as expected.
    assertNull(listener.getHandback());

    // Verify the user data of the notification.
    Notification n = listener.getNotification();
    assertNotNull(n);
    verifyNotificationUserData(n.getUserData());

    // Remove the listener
    notifierBean.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);
    notifierBean.sendNotification(notification);
    assertEquals(0, listener.getNotificationsReceivedCount());

    // Try and remove the listener one more time. Should result in a
    // ListenerNotFoundException being thrown.
    try {
        notifierBean.removeNotificationListener(listener);
        fail("Should have thrown a ListenerNotFoundException!");
    } catch (ListenerNotFoundException ignore) {
    }
}