如何创建“耐用”订户?

时间:2019-04-06 10:52:24

标签: java reactive

我是反应式休息服务的新手,在尝试apache.cfx.cxf-rt-rs-extension-reactivestreams时,我无法使该流保持活动状态。

我以为我可以通过使用反压和阻塞来获取它,但是在收到第一个结果后,方法结束并且订阅请求被忽略。

我在做什么错了?

  private void asyncGet() {
    System.out.println("asyncGet");

    Client c = null;
    try {
        c = ClientBuilder.newBuilder()
                .executorService(Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()))
                .register(FlowableRxInvokerProvider.class)
                .build();

        Publisher<String> publisher = c.target("http://localhost:9081/newRequest/rxget")
                .request()
                .rx(FlowableRxInvoker.class).get(new GenericType<String>() {
                });

        Subscriber<? super String> subscriber = new FlowableSubscriber<String>() {

            Subscription subscription;

            @Override
            public void onSubscribe(Subscription subscription) {
                this.subscription = subscription;
                subscription.request(10);
            }

            @Override
            public void onNext(String request) {
                System.out.println("elaborating " + request);
                int random = (int) Math.random() * 10 + 1;

                //sleepFor(random);

                System.out.println("onNext");

                subscription.request(10);

                System.out.println("requested 10");
            }

            @Override
            public void onError(Throwable throwable) {
                System.out.println(throwable.getMessage());
            }

            @Override
            public void onComplete() {
                System.out.println("onComplete");
            }
        };
        ((Flowable<String>) publisher).observeOn(Schedulers.computation(), true).blockingSubscribe(subscriber);


        System.out.println("end!!!");



    } finally {
        if (c != null) {
            c.close();
        }
    }
}

调用5次方法后得到的是:

asyncGet
asyncGet
asyncGet
asyncGet
asyncGet
init!
Number of request = 50000
elaborating Request_4
onNext
requested 10
onComplete
end!!!
elaborating Request_3
onNext
requested 10
elaborating Request_0
onNext
requested 10
elaborating Request_2
elaborating Request_1
onNext
onComplete
end!!!
onComplete
end!!!
requested 10
onNext
requested 10
onComplete
end!!!
onComplete
end!!!

在此先感谢您的提示!

再见 法比奥

0 个答案:

没有答案