Skip to content

Instantly share code, notes, and snippets.

@DarinM223
Last active February 6, 2017 09:38
Show Gist options
  • Save DarinM223/ae1cea03bf9d5f5d6a7b36d69f69999c to your computer and use it in GitHub Desktop.
Save DarinM223/ae1cea03bf9d5f5d6a7b36d69f69999c to your computer and use it in GitHub Desktop.
Prints "The timer was called!" and the response body to a www.google.com request every second in Java using rxjava
import org.asynchttpclient.*;
import org.asynchttpclient.extras.rxjava.*;
import rx.Observable;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
AsyncHttpClient client = new DefaultAsyncHttpClient();
Observable<Long> timer = Observable.timer(1, 1, TimeUnit.SECONDS);
Observable<Response> timedRequests = timer.flatMap(id ->
AsyncHttpObservable.toObservable(() -> client.prepareGet("http://www.google.com")));
timer.subscribe(id -> System.out.println("The timer was called!"));
timedRequests.subscribe(
resp -> System.out.println(resp.getResponseBody()),
err -> System.err.println("RIP: " + err)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment