package de.rxjava.tests;
import de.rxjava.tests.service.impl.AsyncHTTPClient;
+import de.rxjava.tests.service.impl.FlatMapTestImpl;
public class MainRxJava {
public static void main(String[] args) {
- AsyncHTTPClient asyncHTTPClient = new AsyncHTTPClient();
-
- asyncHTTPClient.getPages();
+ new AsyncHTTPClient().getPages();
+ new FlatMapTestImpl().getWords();
}
}
package de.rxjava.tests.service.impl;
import java.io.IOException;
-import java.net.MalformedURLException;
import java.net.URL;
import de.rxjava.tests.httpclient.CustomHTTPClient;
});
- System.out.println("YOU SEE ME FIRST!!!!");
+ System.out.println("AsyncHTTPClient: YOU SEE ME FIRST!!!!");
try {
private Observable<String> getDataAsync(String uri) {
return getDataSync(uri)
.subscribeOn(Schedulers.io()); // Creates a pool of threads for us which will run the code implemented below :)
- // THIE METHOD DOES NOT START TO RUN MY CODE!!! IT IS DONE BY subscribe METHOD!!!
+ // THIS METHOD DOES NOT START TO RUN MY CODE!!! IT IS DONE BY subscribe METHOD!!!
}
private Observable<String> getDataSync(String uri) {
try {
data = CustomHTTPClient.newInstance("RxJavaTest").retrieveDataAsString(new URL(uri));
- // Making it slower as if having a bad connection :)
+ // Making it slower as if I had a bad connection :)
Thread.sleep(2000);
} catch (InterruptedException exception) {
// Do not forget good patterns when dealing with InterruptedException :(
--- /dev/null
+package de.rxjava.tests.service.impl;
+
+import java.util.Arrays;
+import java.util.List;
+
+import rx.Observable;
+import rx.schedulers.Schedulers;
+
+public class FlatMapTestImpl {
+
+
+ public void getWords() {
+
+ getWordsAsync()
+ .flatMap(words -> { // flatmap method enables us to return one Observable. The subscribe method does not have such feature.
+ return Observable.from(words);
+ })
+ .subscribe(word -> { //Unlike JavaScript Promises, we can call many times the same promise without resolving it.
+ //This stuff would be impossible in JavaScript :)
+ System.out.println(word);
+ });
+
+ System.out.println("FlatMapTestImpl: YOU SEE ME FIRST!!!!");
+
+ try {
+ Thread.sleep(30000);
+ } catch (InterruptedException exception) {
+ // Do not forget good patterns when dealing with InterruptedException :(
+ Thread.currentThread().interrupt();
+ }
+
+ }
+
+ private Observable<List<String>> getWordsAsync() {
+ return getWordsSync()
+ .subscribeOn(Schedulers.io()); // Creates a pool of threads for us which will run the code implemented below :)
+ // THIS METHOD DOES NOT START TO RUN MY CODE!!! IT IS DONE BY subscribe METHOD!!!
+ }
+
+ private Observable<List<String>> getWordsSync() {
+ return Observable.create(observer -> {
+ System.out.println(Thread.currentThread().getName());
+
+ String[] words = { "gus", "stuff", "given", "no", "way" };
+ try {
+ // Making it slower as if having a bad connection :)
+ Thread.sleep(2000);
+ } catch (InterruptedException exception) {
+ // Do not forget good patterns when dealing with InterruptedException :(
+ Thread.currentThread().interrupt();
+
+ observer.onError(exception);
+ }
+
+ // When do you use this stuff?
+ // observer.onCompleted();
+
+ observer.onNext(Arrays.asList(words));
+ });
+ }
+}