Guest User

Untitled

a guest
Dec 7th, 2019
114
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Future<Object> future = Patterns.ask(cacheActor, getMessage, Config.TIMEOUT_MILLIS);
  2. CompletionStage<Object> stage = FutureConverters.toJava(future);
  3. stage.thenCompose(resultMessage -> {
  4.  
  5. System.out.println("ResultMessage " + ((ResultMessage) resultMessage).getURL() + " " + ((ResultMessage) resultMessage).getResponseTime() + " " + ((ResultMessage) resultMessage).isSuccess());
  6.  
  7. if (((ResultMessage) resultMessage).isSuccess()) {
  8. return CompletableFuture.completedFuture(resultMessage);
  9. } else {
  10.  
  11. return Source.from(Collections.singletonList(getMessage))
  12. .toMat(
  13. Flow.<GetMessage>create()
  14. .mapConcat(message -> Collections.nCopies(message.getCount(), message.getURL()))
  15. .mapAsync(1, URL -> {
  16.  
  17. long millisNow = System.currentTimeMillis();
  18. return asyncHttpClient
  19. .prepareGet(URL)
  20. .execute()
  21. .toCompletableFuture()
  22. .thenCompose(f ->
  23. CompletableFuture.completedFuture(System.currentTimeMillis() - millisNow));
  24. })
  25. .toMat(Sink.fold(0L, Long::sum), Keep.right()),
  26. Keep.right()
  27. )
  28. .run(materializer)
  29. .thenCompose(time ->
  30. CompletableFuture.completedFuture(new ResultMessage(
  31. getMessage.getURL(),
  32. (time / getMessage.getCount()),
  33. false
  34. )));
  35. }
  36. }
  37. );
  38.  
  39. return stage;
  40. })
RAW Paste Data