|
27 | 27 | package engineering.swat.watch.impl.jdk; |
28 | 28 |
|
29 | 29 | import java.io.IOException; |
| 30 | +import java.io.UncheckedIOException; |
30 | 31 | import java.nio.file.FileVisitResult; |
31 | 32 | import java.nio.file.Files; |
32 | 33 | import java.nio.file.Path; |
33 | 34 | import java.nio.file.SimpleFileVisitor; |
34 | 35 | import java.nio.file.attribute.BasicFileAttributes; |
| 36 | +import java.util.concurrent.CompletableFuture; |
35 | 37 | import java.util.concurrent.ConcurrentHashMap; |
36 | 38 | import java.util.concurrent.ConcurrentMap; |
37 | 39 | import java.util.concurrent.Executor; |
@@ -72,25 +74,44 @@ private void handleCreate(WatchEvent ev) { |
72 | 74 | // but we don't want to delay the publication of this |
73 | 75 | // create till after the processing is done, so we schedule it in the background |
74 | 76 | var fullPath = ev.calculateFullPath(); |
75 | | - if (!activeWatches.containsKey(fullPath)) { |
76 | | - try { |
77 | | - if (Files.isDirectory(fullPath)) { |
78 | | - addNewDirectory(fullPath); |
79 | | - reportOverflow(fullPath); |
80 | | - } |
81 | | - } catch (IOException ex) { |
82 | | - logger.error("Could not locate new sub directories for: {}", ev.calculateFullPath(), ex); |
83 | | - } |
| 77 | + if (!activeWatches.containsKey(fullPath) && Files.isDirectory(fullPath)) { |
| 78 | + CompletableFuture |
| 79 | + .runAsync(() -> { |
| 80 | + try { |
| 81 | + addNewDirectory(fullPath); |
| 82 | + } catch (IOException e) { |
| 83 | + throw new UncheckedIOException(e); |
| 84 | + } |
| 85 | + }, exec) |
| 86 | + .thenRunAsync(() -> reportOverflow(fullPath), exec) |
| 87 | + .exceptionally(ex -> { |
| 88 | + logger.error("Could not locate new sub directories for: {}", fullPath, ex); |
| 89 | + return null; |
| 90 | + }); |
84 | 91 | } |
85 | 92 | } |
86 | 93 |
|
87 | 94 | private void handleOverflow(WatchEvent ev) { |
88 | 95 | var fullPath = ev.calculateFullPath(); |
89 | | - try (var children = Files.find(fullPath, 1, (p, attrs) -> p != fullPath && attrs.isDirectory())) { |
90 | | - children.forEach(JDKRecursiveDirectoryWatch.this::reportOverflow); |
91 | | - } catch (IOException e) { |
92 | | - logger.error("Could not handle overflow for: {} ({})", fullPath, e); |
93 | | - } |
| 96 | + CompletableFuture |
| 97 | + .supplyAsync(() -> { |
| 98 | + try { |
| 99 | + return Files.find(fullPath, 1, (p, attrs) -> p != fullPath && attrs.isDirectory()); |
| 100 | + } catch (IOException e) { |
| 101 | + throw new UncheckedIOException(e); |
| 102 | + } |
| 103 | + }, exec) |
| 104 | + .whenCompleteAsync((children, ex) -> { |
| 105 | + try { |
| 106 | + if (ex == null) { |
| 107 | + children.forEach(JDKRecursiveDirectoryWatch.this::reportOverflow); |
| 108 | + } else { |
| 109 | + logger.error("Could not handle overflow for: {} ({})", fullPath, ex); |
| 110 | + } |
| 111 | + } finally { |
| 112 | + children.close(); |
| 113 | + } |
| 114 | + }, exec); |
94 | 115 | } |
95 | 116 |
|
96 | 117 | private void handleDeleteDirectory(WatchEvent ev) { |
|
0 commit comments