我是RxJava的新手,我正在尝试使用RxJava 2来处理动作事件流。我在流(列表)中有两种类型 - 必须以阻塞方式执行的“立即”和必须非阻塞执行的“延迟” - 两个组都必须并行执行它们的一组操作。在阅读了有关选项之后,我认为groupBy运算符在这里发挥作用,但我仍然坚持如何处理对组的订阅,因此处理是合适的。这是我提出的代码片段(显然是错误的),但我无法想到这种方法。我正在寻找关于如何正确使用两组可观察量的建议(以便立即组以阻塞方式执行,而延迟组以非阻塞方式执行)。提前谢谢。
List<Map<String,String>> actionMaps = getActionMaps();
if(actionMaps != null && !actionMaps.isEmpty()){
actionMaps.removeIf(Objects::isNull);
Observable.fromIterable(actionMaps)
.subscribeOn(Schedulers.computation())
.map(actionMap -> {
String configForAction = jsonBasedConfigProvider.getValueForKey(Constants.CLASSIFIER_ACTIONS, getType() + "." + actionMap.get("name"));
if(configForAction != null){
Utilities.convertJsonStringToMap(configForAction).forEach(actionMap::putIfAbsent);
}
return actionMap;
})
.groupBy(actionMap -> actionMap.get("executionType"))
.subscribe(group -> {
//Group all the immediates for special processing
group
.toList()
.flatMapObservable(Observable::fromIterable)
.takeWhile(actionMap -> "immediate".equals(actionMap.get("executionType")))
.subscribeOn(Schedulers.computation())
.blockingSubscribe(actionMap -> {
System.out.println("Key is: " + group.getKey() + ", thread is: " + Thread.currentThread().getName() + ",item is:" + actionMap );
//ActionFactory.getAction(actionMap.get("name")).execute(actionMap);
//Any error handling required here?
});
//Group all the deferreds
group
.toList()
.flatMapObservable(Observable::fromIterable)
.takeWhile(actionMap -> "deferred".equals(actionMap.get("executionType")))
.subscribeOn(Schedulers.computation())
.subscribe(actionMap -> {
System.out.println("Key is: " + group.getKey() + ", thread is: " + Thread.currentThread().getName() + ",item is:" + actionMap );
//ActionFactory.getAction(actionMap.get("name")).execute(actionMap);
});
});
}