public static < T > Collection < MemberResponse < T > > executeOptimistic ( IExecutorService execSvc , Set < Member > members , Callable < T > callable , long maxWaitTime , TimeUnit unit ) { Collection < MemberResponse < T >> result = new ArrayList < MemberResponse < T > > ( members . size ( ) ) ; Map < Member , Future < T > > resultFutures = execSvc . submitToMembers ( callable , members ) ; for ( Entry < Member , Future < T > > futureEntry : resultFutures . entrySet ( ) ) { Future < T > future = futureEntry . getValue ( ) ; Member member = futureEntry . getKey ( ) ; try { if ( maxWaitTime > 0 ) { result . add ( new MemberResponse < T > ( member , future . get ( maxWaitTime , unit ) ) ) ; } else { result . add ( new MemberResponse < T > ( member , future . get ( ) ) ) ; } //ignore exceptions... return what you can } catch ( InterruptedException e ) { Thread . currentThread ( ) . interrupt ( ) ; //restore interrupted status and return what we have return result ; } catch ( MemberLeftException e ) { log . warn ( "Member {} left while trying to get a distributed callable result" , member ) ; } catch ( ExecutionException e ) { if ( e . getCause ( ) instanceof InterruptedException ) { //restore interrupted state and return Thread . currentThread ( ) . interrupt ( ) ; return result ; } else { log . warn ( "Unable to execute callable on " + member + ". There was an error." , e ) ; } } catch ( TimeoutException e ) { log . error ( "Unable to execute task on " + member + " within 10 seconds." ) ; } catch ( RuntimeException e ) { log . error ( "Unable to execute task on " + member + ". An unexpected error occurred." , e ) ; } } return result ; }
We will always try to gather as many results as possible and never throw an exception .