SHARE
TWEET

Untitled

a guest Feb 13th, 2017 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    [...]
  2.         recover(
  3.                 // Get events from Elastic search
  4.                 VertxUtil.<GetResponse>withNewFuture(future ->
  5.                         client.prepareGet(req.bucketId(), IndexTypes.EVENT, req.eventId()).execute(completer(future))
  6.                 ).map(resp -> resp.isExists() ?
  7.                         // Success
  8.                         GetEventResponse.create(resp.getSourceAsString()) :
  9.                         // Event not found
  10.                         GetEventResponse.eventNotFound()
  11.                 ), e -> isIndexNotFound(e) ?
  12.                         // Recover from index not found
  13.                         Future.succeededFuture(GetEventResponse.bucketNotFound()) :
  14.                         // Unexpected exception, handle later
  15.                         Future.failedFuture(e)
  16.         ).setHandler(
  17.                 // Send response
  18.                 reply(message)
  19.         );
  20.     [...]
  21.  
  22.     public static <Resp> Handler<AsyncResult<Resp>> responseDispatcher(Consumer<Throwable> errorHandler, Consumer<Resp> successHandler) {
  23.         return resp -> {
  24.             if (resp.succeeded())
  25.                 successHandler.accept(resp.result());
  26.             else
  27.                 errorHandler.accept(resp.cause());
  28.         };
  29.     }
  30.  
  31.     public static <T> Future<T> withNewFuture(Consumer<Future<T>> f) {
  32.         Future<T> future = Future.future();
  33.         f.accept(future);
  34.         return future;
  35.     }
  36.  
  37.     public static <T> Future<T> recover(Future<T> future, Function<Throwable, Future<T>> f) {
  38.         return withNewFuture(newF ->
  39.                 future.setHandler(responseDispatcher(
  40.                         e -> f.apply(e).setHandler(newF.completer()),
  41.                         newF::complete
  42.                 ))
  43.         );
  44.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top