Guest User

Untitled

a guest
Feb 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. public class ProcessFiles {
  2. private static final Logger logger = LoggerFactory.getLogger(ProcessFiles.class.getSimpleName());
  3.  
  4. public static void main(String[] args) throws IOException, InterruptedException {
  5. long startTime = System.currentTimeMillis();
  6.  
  7. logger.info("Creating actor system");
  8. ActorSystem system = ActorSystem.create("actor_system");
  9.  
  10. Set<String> files = new HashSet<>();
  11. Stream<String> stringStream = Files.lines(Paths.get(fileName));
  12. stringStream.forEach(line -> files.addAll(Arrays.asList(line.split(","))));
  13. List<CompletableFuture<Object>> futureList = new ArrayList<>();
  14.  
  15. files.forEach((String file) -> {
  16. ActorRef actorRef = system.actorOf(Props.create(ProcessFile.class, file));
  17. futureList.add(PatternsCS.ask(actorRef, DEFAULT_TIMEOUT).toCompletableFuture());
  18. });
  19.  
  20. boolean isDone;
  21. do {
  22. Thread.sleep(30000);
  23. isDone = true;
  24. int count = 0;
  25. for (CompletableFuture<Object> future : futureList) {
  26. isDone = isDone & (future.isDone() || future.isCompletedExceptionally() || future.isCancelled());
  27. if (future.isDone() || future.isCompletedExceptionally() || future.isCancelled()) {
  28. ++count;
  29. }
  30. }
  31. logger.info("Process is completed for " + count + " files out of " + files.size() + " files.");
  32. } while (!isDone);
  33. logger.info("Process is done in " + (System.currentTimeMillis() - startTime) + " ms");
  34. system.terminate();
  35. }
  36. }
Add Comment
Please, Sign In to add comment