I've a list of integers and for each integer, I want to fetch a string value from database, and put it into a Map where Integer is the int value from the parent list, and String is the string value fetched from database. I also want, fetching the strings to run in parallel.
This is how I've implemented it with RxJava. My code works and I'm getting the expected result, but I don't think fetching names are running in parallel.
public Observable<Map<Integer, String>> getVitalsForConnectedDevices(int accountId) {
List<Integer> ids = Lists.newArrayList(2, 3, 5);
return Observable<Map<Integer, String>> obs = Observable.from(ids)
.flatMap((Func1<Integer, Observable<Map.Entry<Integer, String>>>) integer
-> Observable.just(Maps.immutableEntry(integer, deviceDAO.getVitalName(integer)))
.subscribeOn(Schedulers.io()), 20)
.toMap(entry -> entry.getKey(), entry -> entry.getValue());
}
Here's the getVitalName() methods
public String getVitalName(int vitalId) {
log.debug("id: " + vitalId);
String query = "SELECT name FROM vitals WHERE vital_id=?";
String name = v1JdbcTemplate.queryForObject(query, String.class, vitalId);
log.debug("name: " + name);
return name;
}
its printing the debug statements from the above function in this order:
09-10-2017 02:05:37 DEBUG DeviceDAO:118 - id: 2
09-10-2017 02:05:37 DEBUG DeviceDAO:121 - name: Steps
09-10-2017 02:05:37 DEBUG DeviceDAO:118 - id: 3
09-10-2017 02:05:37 DEBUG DeviceDAO:121 - name: Floors
09-10-2017 02:05:37 DEBUG DeviceDAO:118 - id: 5
09-10-2017 02:05:37 DEBUG DeviceDAO:121 - name: Distance
If it was running parallel, shouldn't it be printing all the id's first and names later? What am I doing wrong here? How do I get them to run in parallel?