public void dispatchEvent ( ManagerEvent event ) { // shouldn't happen if ( event == null ) { logger . error ( "Unable to dispatch null event. This should never happen. Please file a bug." ) ; return ; } dispatchLegacyEventIfNeeded ( event ) ; if ( logger . isDebugEnabled ( ) ) { logger . debug ( "Dispatching event:\n" + event . toString ( ) ) ; } // Some events need special treatment besides forwarding them to the // registered eventListeners (clients) // These events are handled here at first: // Dispatch ResponseEvents to the appropriate responseEventListener if ( event instanceof ResponseEvent ) { ResponseEvent responseEvent ; String internalActionId ; responseEvent = ( ResponseEvent ) event ; internalActionId = responseEvent . getInternalActionId ( ) ; if ( internalActionId != null ) { synchronized ( responseEventListeners ) { ManagerEventListener listener ; listener = responseEventListeners . get ( internalActionId ) ; if ( listener != null ) { try { listener . onManagerEvent ( event ) ; } catch ( Exception e ) { logger . warn ( "Unexpected exception in response event listener " + listener . getClass ( ) . getName ( ) , e ) ; } } } } else { // ResponseEvent without internalActionId: // this happens if the same event class is used as response // event // and as an event that is not triggered by a Manager command // Example: QueueMemberStatusEvent. // logger.debug("ResponseEvent without " // + "internalActionId:\n" + responseEvent); } // NOPMD } if ( event instanceof DisconnectEvent ) { // When we receive get disconnected while we are connected start // a new reconnect thread and set the state to RECONNECTING. synchronized ( this ) { if ( state == CONNECTED ) { state = RECONNECTING ; // close socket if still open and remove reference to // readerThread // After sending the DisconnectThread that thread will die // anyway. cleanup ( ) ; Thread reconnectThread = new Thread ( new Runnable ( ) { public void run ( ) { reconnect ( ) ; } } ) ; reconnectThread . setName ( "Asterisk-Java ManagerConnection-" + id + "-Reconnect-" + reconnectThreadCounter . getAndIncrement ( ) ) ; reconnectThread . setDaemon ( true ) ; reconnectThread . start ( ) ; // now the DisconnectEvent is dispatched to registered // eventListeners // (clients) and after that the ManagerReaderThread is gone. // So effectively we replaced the reader thread by a // ReconnectThread. } else { // when we receive a DisconnectEvent while not connected we // ignore it and do not send it to clients return ; } } } if ( event instanceof ProtocolIdentifierReceivedEvent ) { ProtocolIdentifierReceivedEvent protocolIdentifierReceivedEvent ; String protocolIdentifier ; protocolIdentifierReceivedEvent = ( ProtocolIdentifierReceivedEvent ) event ; protocolIdentifier = protocolIdentifierReceivedEvent . getProtocolIdentifier ( ) ; setProtocolIdentifier ( protocolIdentifier ) ; // no need to send this event to clients return ; } fireEvent ( event ) ; }
This method is called by the reader whenever a ManagerEvent is received . The event is dispatched to all registered ManagerEventHandlers .