Guest User

Untitled

a guest
Mar 20th, 2018
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import com.google.common.util.concurrent.FutureCallback;
  2. import com.google.common.util.concurrent.Futures;
  3. import com.google.common.util.concurrent.ListenableFuture;
  4. import com.google.common.util.concurrent.MoreExecutors;
  5. import io.vertx.core.Future;
  6.  
  7. /**
  8. * @author Billy Yuan <billy112487983@gmail.com>
  9. */
  10.  
  11. public class AsyncUtils {
  12. /**
  13. * Convert a Guava ListenableFuture to a Vert.x Future, running the callback code
  14. * in the current Thread.
  15. *
  16. * @param listenableFuture the ListenableFuture to be converted
  17. * @param <T> the wrapped result
  18. * @return the converted Vert.x Future
  19. */
  20. public static <T> Future<T> convertToVertxFutureGracefully(ListenableFuture<T> listenableFuture) {
  21. Future<T> vertxFuture = Future.future();
  22.  
  23. Futures.addCallback(listenableFuture, new FutureCallback<T>() {
  24. @Override
  25. public void onSuccess(T result) {
  26. vertxFuture.complete(result);
  27. }
  28.  
  29. @Override
  30. public void onFailure(Throwable t) {
  31. vertxFuture.fail(t);
  32. }
  33. }, MoreExecutors.newDirectExecutorService());
  34. return vertxFuture;
  35. }
  36. }
Add Comment
Please, Sign In to add comment