Last active
February 6, 2017 09:38
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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