Skip to content

Commit c56ed84

Browse files
committed
Added more read me code
1 parent 5d81d9f commit c56ed84

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

README.md

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,29 +87,29 @@ You can then use it to load values which will be `CompleteableFuture` promises t
8787
```
8888

8989
or you can use it to compose future computations as follows. The key requirement is that you call
90-
`dataloader.dispatch()` at some point in order to make the underlying calls happen to the batch loader.
91-
In this version of data loader, this does not happen automatically. More on this later.
90+
`dataloader.dispatch()` or its variant `dataloader.dispatchAndJoin()` at some point in order to make the underlying calls happen to the batch loader.
91+
In this version of data loader, this does not happen automatically. More on this in [Manual dispatching](#manual-dispatching) .
9292

9393
```java
94-
userLoader.load(1L)
95-
.thenAccept(user -> {
96-
System.out.println("user = " + user);
97-
userLoader.load(user.getInvitedByID())
98-
.thenAccept(invitedBy -> {
99-
System.out.println("invitedBy = " + invitedBy);
100-
});
101-
});
102-
103-
userLoader.load(2L)
104-
.thenAccept(user -> {
105-
System.out.println("user = " + user);
106-
userLoader.load(user.getInvitedByID())
107-
.thenAccept(invitedBy -> {
108-
System.out.println("invitedBy = " + invitedBy);
109-
});
110-
});
111-
112-
userLoader.dispatch().join();
94+
userLoader.load(1L)
95+
.thenAccept(user -> {
96+
System.out.println("user = " + user);
97+
userLoader.load(user.getInvitedByID())
98+
.thenAccept(invitedBy -> {
99+
System.out.println("invitedBy = " + invitedBy);
100+
});
101+
});
102+
103+
userLoader.load(2L)
104+
.thenAccept(user -> {
105+
System.out.println("user = " + user);
106+
userLoader.load(user.getInvitedByID())
107+
.thenAccept(invitedBy -> {
108+
System.out.println("invitedBy = " + invitedBy);
109+
});
110+
});
111+
112+
userLoader.dispatchAndJoin();
113113

114114
```
115115

@@ -124,6 +124,13 @@ concurrent requests will be coalesced and presented to your batch loading functi
124124
application to safely distribute data fetching requirements throughout your application and
125125
maintain minimal outgoing data requests.
126126

127+
In the example above, the first call to dispatch will cause the batched user keys (1 and 2) to be fired at the BatchLoader function to load 2 users.
128+
129+
Since each `thenAccept` callback made more calls to `userLoader` to get the "user they they invited", another 2 user keys are fired at the BatchLoader function for them.
130+
131+
In this case the `userLoader.dispatchAndJoin()` is used to fire a dispatch call, wait for it (aka join it), see if the data loader has more batched entries, (which is does)
132+
and then it repeats this until the data loader internal queue of keys is empty. At this point we have made 2 batched calls instead of the niave 4 calls we might have made if
133+
we did not "batch" the calls to load data.
127134

128135
## Differences to reference implementation
129136

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ ext {
2424
}
2525

2626
apply plugin: 'java'
27+
apply plugin: 'maven'
2728
apply from: "$projectDir/gradle/publishing.gradle"
2829

2930
repositories {

0 commit comments

Comments
 (0)