I’m not really sure if the code snippet below is 100% correct… Schedulers.io() is not probably the way to go ;) I wanted to try RxJava for two Async Rest Calls with blocking at the end.
package org.rx.java.tutorial;
import java.util.List;
import rx.Observable;
import rx.functions.Func2;
import rx.schedulers.Schedulers;
public class App {
public static void main(String[] args) {
List<Person> zipped = Observable
.zip(getObservableName(), getObservableAge(), new Func2<String, Integer, Person>() {
@Override
public Person call(String name, Integer age) {
Person person = new Person();
person.setName(name);
person.setAge(age);
return person;
}
}).toList().toBlocking().single();
zipped.stream().forEach(person -> {
System.out.println("[Name : " + person.getName() + ", Age : " + person.getAge() + "]");
// [Name : Maciek, Age : 18]
// [Name : Rysiek, Age : 20]
// [Name : Zdzichu, Age : 45]
});
}
// Rest Call simulation, emits entries of Lists.newArrayList("Maciek", "Rysiek", "Zdzichu");
private static Observable<String> getObservableName() {
return Observable.from(RestService.getNames()).subscribeOn(Schedulers.io());
}
// Rest Call simulation, emits entries of Lists.newArrayList(18, 20, 45);
private static Observable<Integer> getObservableAge() {
return Observable.from(RestService.getAges()).subscribeOn(Schedulers.io());
}
}