MemoryMXBeanImplTest.java 文件源码

java
阅读 19 收藏 0 点赞 0 评论 0

项目:cn1 作者:
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) {
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号