从异步http调用传递状态而不阻塞线程

时间:2019-03-27 17:00:45

标签: spring reactive-programming project-reactor

我想创建一个Flux,根据当前过程中得到的将来结果,将新状态传递给下一个操作。

我已经像这样实现了……



public static Flux<MyResult> create(MyHttpClient myClient) {
        final MyGenerator generator = new MyGenerator(myClient);
        return Flux.generate(Optional::empty, generator);
}

private final static class MyGenerator implements BiFunction<Optional<String>, SynchronousSink<MyResult>, Optional<String>> {

        private final MyHttpClient myHttpClient;

        private MyGenerator(MyHttpClient myHttpClient) {
            this.myHttpClient = myHttpClient;
        }

        @Override
        public Optional<String> apply(Optional<String> lastState, SynchronousSink<Object> objectSynchronousSink) {
            MyResult myResult = lastState.map((ls) -> myHttpClient.blockingRequestWithState(ls)).orElseGet(() -> myHttpClient.blockingRequestForInitial());
            if (myResult.isFinished()) {
                sink.complete();
                return Optional.empty();
            }
            sink.next(myResult);
            return Optional.of(myResult.getLastState());
        }
    }

但是,我想避免通过调用'myHttpClient.blockingRequest ...'方法来阻塞线程,并异步发出请求,并将此将来值作为lastState传递。

您对此有何建议?

0 个答案:

没有答案