RxJava Async Rest Calls

I’m not really sure if the code snippet below is 100% correct… 🙂
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());

	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s