
flow.execution.RxJavaExecutionEngine Maven / Gradle / Ivy
package flow.execution;
import static java.util.Collections.emptyList;
import java.util.LinkedList;
import java.util.List;
import flow.Dependency;
import flow.FlowException;
import flow.Product;
import flow.Provider;
import flow.planning.ExecutionPlanner.ExecutionStep;
import lombok.SneakyThrows;
import rx.Single;
/**
* Execution engine that uses RxJava to execute providers.
* Steps will be executed in parallel and RxJava handles necessary waiting for dependent steps.
*
*/
public class RxJavaExecutionEngine, P extends Provider> extends AbstractExecutionEngine, D, Prod, P> {
@Override
protected Single executeStep(ExecutionStep step, List> dependencies) throws FlowException {
Single> deps;
if (dependencies.isEmpty())
deps = Single.just(emptyList());
else
deps = Single.zip(dependencies, this::convertArrayToTypedList);
return deps.map(ds -> executeStepInternal(step, ds)).cache();
}
@SuppressWarnings("unchecked")
private List convertArrayToTypedList(Object[] results) {
List castedObjects = new LinkedList();
for(Object r : results)
castedObjects.add((T)r);
return castedObjects;
}
@SneakyThrows
private Prod executeStepInternal(ExecutionStep step, List params) {
return step.getProvider().invoke(params);
}
@Override
protected Single wrapInputValue(Prod input) {
return Single.just(input);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy