Skip to content

Commit 1aea502

Browse files
author
michele
committed
ImportDocumentExample: added pending request count
1 parent 9837794 commit 1aea502

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

src/test/java/com/arangodb/example/document/ImportDocumentExample.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,53 @@
3131
import java.util.UUID;
3232
import java.util.concurrent.CompletableFuture;
3333
import java.util.concurrent.ExecutionException;
34+
import java.util.concurrent.atomic.AtomicLong;
3435
import java.util.stream.Collectors;
3536
import java.util.stream.IntStream;
37+
import java.util.stream.Stream;
3638

3739
/**
38-
* @author Mark Vollmary
40+
* @author Michele Rastelli
3941
*/
4042
public class ImportDocumentExample extends ExampleBase {
4143

4244
private static final Logger log = LoggerFactory.getLogger(ImportDocumentExample.class);
45+
private static final int MAX_PENDING_REQUESTS = 100;
4346

4447
@Test
4548
public void importDocument() {
46-
List<CompletableFuture<DocumentImportEntity>> completableFutures =
47-
IntStream.range(0, 100)
48-
.mapToObj(i -> IntStream.range(0, 500)
49-
.mapToObj(it -> new TestEntity(UUID.randomUUID().toString())).collect(Collectors.toList())
50-
)
51-
.map(p -> collection.importDocuments(p, new DocumentImportOptions()))
52-
.collect(Collectors.toList());
49+
AtomicLong pendingReqsCount = new AtomicLong();
50+
51+
Stream<List<TestEntity>> chunks = IntStream.range(0, 1000)
52+
.mapToObj(i -> IntStream.range(0, 500)
53+
.mapToObj(it -> new TestEntity(UUID.randomUUID().toString())).collect(Collectors.toList())
54+
);
55+
56+
List<CompletableFuture<DocumentImportEntity>> completableFutures = chunks
57+
.map(p -> {
58+
// wait for pending requests
59+
while (pendingReqsCount.get() > MAX_PENDING_REQUESTS) {
60+
try {
61+
Thread.sleep(10);
62+
} catch (InterruptedException e) {
63+
e.printStackTrace();
64+
}
65+
}
66+
67+
pendingReqsCount.incrementAndGet();
68+
return collection.importDocuments(p, new DocumentImportOptions())
69+
.thenApply(it -> {
70+
pendingReqsCount.decrementAndGet();
71+
log.info(String.valueOf(it.getCreated()));
72+
return it;
73+
});
74+
}
75+
)
76+
.collect(Collectors.toList());
5377

5478
completableFutures.forEach(cf -> {
5579
try {
56-
log.info(String.valueOf(cf.get().getCreated()));
80+
cf.get();
5781
} catch (InterruptedException | ExecutionException e) {
5882
e.printStackTrace();
5983
}

0 commit comments

Comments
 (0)