private < T > CompletableFuture < T > executeOnTransientConnection ( Address address , Function < ClientConnection , CompletableFuture < T > > callback , Executor executor ) { CompletableFuture < T > future = new CompletableFuture <> ( ) ; if ( address . equals ( returnAddress ) ) { callback . apply ( localConnection ) . whenComplete ( ( result , error ) -> { if ( error == null ) { executor . execute ( ( ) -> future . complete ( result ) ) ; } else { executor . execute ( ( ) -> future . completeExceptionally ( error ) ) ; } } ) ; return future ; } openChannel ( address ) . whenComplete ( ( channel , channelError ) -> { if ( channelError == null ) { callback . apply ( getOrCreateClientConnection ( channel ) ) . whenComplete ( ( result , sendError ) -> { if ( sendError == null ) { executor . execute ( ( ) -> future . complete ( result ) ) ; } else { executor . execute ( ( ) -> future . completeExceptionally ( sendError ) ) ; } channel . close ( ) ; } ) ; } else { executor . execute ( ( ) -> future . completeExceptionally ( channelError ) ) ; } } ) ; return future ; }
Executes the given callback on a transient connection .