Skip to content

Instantly share code, notes, and snippets.

@hohonuuli
Last active May 2, 2019 04:45
Show Gist options
  • Save hohonuuli/c657f1560d83bc137cc7b83620693acc to your computer and use it in GitHub Desktop.
Save hohonuuli/c657f1560d83bc137cc7b83620693acc to your computer and use it in GitHub Desktop.
Scheduling Page Requests example code
// This function takes a page, which defines the limit and offset
// and makes the web service call using that limit/offset
Function<RequestPager.Page, List<MyObject>> function = (page) -> {
try {
return service.someApiCall(page.getLimit(), page.getOffset())
.get(timeout.toMillis(), TimeUnit.MILLISECONDS);
} catch (Exception e) {
throw new RuntimeException("A page request failed", e);
}
};
// You'll need to know how many objects you're trying to retrieve.
// I'm assuming you have an API call to return this count.
int expectedCount = service.countMyObjects();
// define our parameters
int pageSize = 50;
int retryAttempts = 2
int threadCount = 3 // Number of simultaneous REST calls
// -- 1. Create a new Request pager
RequestPager<List<MyObject>> pager = new RequestPager<>(function, retryAttempts, threadCount);
// -- 2. Here we generate a runner which has our Supplier queue, ExecutorService and
// the Observable we subscribe to to consume the web service return values
RequestPager.Runner<List<MyObject>> runner = pager.build(expectedCount, pageSize);
// -- 3. We get the observable from the runner and set up our subscribers
Observable<List<MyObject>> observable = runner.getObservable();
observable.subscribeOn(Schedulers.io())
.subscribe(xs -> System.out.println("Retrieved " xs.size() + " objects"),
ex -> System.err.println("Bummer ... Exception was thrown"),
() -> System.out.println("All page requests were completed"));
// -- 4. Start fetching data
runner.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment