1 23 24 29 package com.sun.enterprise.management.base; 30 31 import javax.management.Notification ; 32 import javax.management.NotificationListener ; 33 34 import com.sun.appserv.management.base.NotificationEmitterService; 35 import com.sun.appserv.management.base.NotificationEmitterServiceKeys; 36 import com.sun.appserv.management.base.Util; 37 import com.sun.appserv.management.util.jmx.NotificationBuilder; 38 39 40 import com.sun.enterprise.management.AMXTestBase; 41 import com.sun.enterprise.management.Capabilities; 42 43 44 46 public final class NotificationEmitterServiceTest extends AMXTestBase 47 { 48 public 49 NotificationEmitterServiceTest( ) 50 { 51 } 52 public static Capabilities 53 getCapabilities() 54 { 55 return getOfflineCapableCapabilities( true ); 56 } 57 58 public NotificationEmitterService 59 getNotificationEmitterService() 60 { 61 return getDomainRoot().getDomainNotificationEmitterService(); 62 } 63 64 public void 65 testGet() 66 { 67 assert getNotificationEmitterService() != null; 68 } 69 70 71 private final static class testEmitListener implements NotificationListener 72 { 73 static final String TEST_TYPE = "unittests.testEmitListener"; 74 private Notification mNotification; 75 private int mNumHeard; 76 77 public testEmitListener() 78 { 79 mNumHeard = 0; 80 mNotification = null; 81 } 82 83 public void 84 handleNotification( final Notification notif, final Object handback ) 85 { 86 mNotification = notif; 87 ++mNumHeard; 88 } 89 90 public Notification getLast() { return mNotification; } 91 public int getNumHeard() { return mNumHeard; } 92 public void clear() { mNumHeard = 0; mNotification = null; } 93 } 94 95 private static final String TEST_SOURCE = "NotificationEmitterServiceTest"; 96 private static final String TEST_MESSAGE = "Message"; 97 private static final String TEST_KEY = "TestKey"; 98 private static final String TEST_VALUE = "test value"; 99 100 public void 101 testEmit() 102 { 103 final NotificationEmitterService nes = getNotificationEmitterService(); 104 105 final NotificationBuilder builder = 106 new NotificationBuilder( testEmitListener.TEST_TYPE, TEST_SOURCE ); 107 108 final testEmitListener listener = new testEmitListener(); 109 nes.addNotificationListener( listener, null, null); 110 final Notification notif = builder.buildNew( TEST_MESSAGE); 111 builder.putMapData( notif, TEST_KEY, TEST_VALUE ); 112 113 nes.emitNotification( notif ); 115 while( listener.getLast() == null ) 116 { 117 mySleep( 20 ); 119 } 120 final Notification retrieved = listener.getLast(); 121 assert( retrieved.getType().equals( notif.getType() ) ); 122 assert( Util.getAMXNotificationValue( retrieved, TEST_KEY, String .class).equals( TEST_VALUE ) ); 123 assert( retrieved.getSource().equals( TEST_SOURCE ) ); 124 assert( retrieved.getMessage().equals( TEST_MESSAGE ) ); 125 126 listener.clear(); 128 long start = now(); 129 final int ITER = 200; 130 for( int i = 0; i < ITER; ++i) 131 { 132 final Notification temp = builder.buildNew( TEST_MESSAGE); 133 builder.putMapData( notif, TEST_KEY, TEST_VALUE ); 134 nes.emitNotification( temp ); 135 } 136 printElapsedIter( "Emitted Notifications", start, ITER ); 137 start = now(); 138 while( listener.getNumHeard() < ITER ) 139 { 140 mySleep( 10 ); 141 } 142 printElapsedIter( "After sending, received emitted Notifications", start, ITER ); 143 } 144 } 145 146 147 148 149 150 151 152 153 154 155 | Popular Tags |