private synchronized void registerCacheEventListener ( EventListenerWrapper < K , V > wrapper ) { if ( aSyncListenersList . contains ( wrapper ) || syncListenersList . contains ( wrapper ) ) { throw new IllegalStateException ( "Cache Event Listener already registered: " + wrapper . getListener ( ) ) ; } if ( wrapper . isOrdered ( ) && orderedListenerCount ++ == 0 ) { storeEventSource . setEventOrdering ( true ) ; } switch ( wrapper . getFiringMode ( ) ) { case ASYNCHRONOUS : aSyncListenersList . add ( wrapper ) ; break ; case SYNCHRONOUS : syncListenersList . add ( wrapper ) ; break ; default : throw new AssertionError ( "Unhandled EventFiring value: " + wrapper . getFiringMode ( ) ) ; } if ( listenersCount ++ == 0 ) { storeEventSource . addEventListener ( eventListener ) ; } }
Synchronized to make sure listener addition is atomic in order to prevent having the same listener registered under multiple configurations