private synchronized void complete ( String message ) { // This is a weird problem with writing stuff while idling. Need to investigate it more, but // for now just ignore it. if ( MESSAGE_COULDNT_BE_FETCHED_REGEX . matcher ( message ) . matches ( ) ) { log . warn ( "Some messages in the batch could not be fetched for {}\n" + "---cmd---\n{}\n---wire---\n{}\n---end---\n" , new Object [ ] { config . getUsername ( ) , getCommandTrace ( ) , getWireTrace ( ) } ) ; errorStack . push ( new Error ( completions . peek ( ) , message , wireTrace . list ( ) ) ) ; final CommandCompletion completion = completions . peek ( ) ; String errorMsg = "Some messages in the batch could not be fetched for user " + config . getUsername ( ) ; RuntimeException ex = new RuntimeException ( errorMsg ) ; if ( completion != null ) { completion . error ( errorMsg , new MailHandlingException ( getWireTrace ( ) , errorMsg , ex ) ) ; completions . poll ( ) ; } else { throw ex ; } } CommandCompletion completion = completions . peek ( ) ; if ( completion == null ) { if ( "+ idling" . equalsIgnoreCase ( message ) ) { synchronized ( idleMutex ) { idler . idleStart ( ) ; log . trace ( "IDLE entered." ) ; idleAcknowledged . set ( true ) ; } } else { log . error ( "Could not find the completion for message {} (Was it ever issued?)" , message ) ; errorStack . push ( new Error ( null , "No completion found!" , wireTrace . list ( ) ) ) ; } return ; } if ( completion . complete ( message ) ) { completions . poll ( ) ; } }
This is synchronized to ensure that we process the queue serially .