@ Override public void run ( ) { if ( ! stateUpdater . compareAndSet ( this , 1 , 2 ) ) { return ; } List < JDBCLogAttribute > messages = new ArrayList <> ( ) ; JDBCLogAttribute msg = null ; //only grab at most 1000 messages at a time for ( int i = 0 ; i < 1000 ; ++ i ) { msg = pendingMessages . poll ( ) ; if ( msg == null ) { break ; } messages . add ( msg ) ; } try { if ( ! messages . isEmpty ( ) ) { writeMessage ( messages ) ; } } finally { Executor executor = this . executor ; stateUpdater . set ( this , 0 ) ; //check to see if there is still more messages //if so then run this again if ( ! pendingMessages . isEmpty ( ) ) { if ( stateUpdater . compareAndSet ( this , 0 , 1 ) ) { executor . execute ( this ) ; } } } }
insert the log record to database