public void run ( ) { ConnectionHandle connection = null ; long tmp ; long nextCheckInMs = this . maxAgeInMs ; int partitionSize = this . partition . getAvailableConnections ( ) ; long currentTime = System . currentTimeMillis ( ) ; for ( int i = 0 ; i < partitionSize ; i ++ ) { try { connection = this . partition . getFreeConnections ( ) . poll ( ) ; if ( connection != null ) { connection . setOriginatingPartition ( this . partition ) ; tmp = this . maxAgeInMs - ( currentTime - connection . getConnectionCreationTimeInMs ( ) ) ; if ( tmp < nextCheckInMs ) { nextCheckInMs = tmp ; } if ( connection . isExpired ( currentTime ) ) { // kill off this connection closeConnection ( connection ) ; continue ; } if ( this . lifoMode ) { // we can't put it back normally or it will end up in front again. if ( ! ( connection . getOriginatingPartition ( ) . getFreeConnections ( ) . offer ( connection ) ) ) { connection . internalClose ( ) ; } } else { this . pool . putConnectionBackInPartition ( connection ) ; } Thread . sleep ( 20L ) ; // test slowly, this is not an operation that we're in a hurry to deal with (avoid CPU spikes)... } } catch ( Throwable e ) { logger . error ( "Connection max age thread exception." , e ) ; } } // throw it back on the queue }
Invoked periodically .