简体   繁体   中英

Rest Service with Java Observable from Executer Service

I have a rest web service which returns DeferredResults. I implemented JavaRx Observable from executer service that returns Future. I wanted this web service to be non blocking but it seems it is blocking the operation.

Do you think the way I used executer service is healthy?

DeferredResult result = new DeferredResult();
    Observable<ResultEntity> observable = Observable
                    .from(executerService.submit(callable));
            observable.subscribe(new Subscriber<ResultEntity>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable throwable) {
                    result.setErrorResult(throwable.getMessage());
                }

                @Override
                public void onNext(ResultEntity r) {
                    result.setResult(t);

                }

            });

If you have a Callable , you can use fromCallable :

Observable.fromCallable(callable)
.subscribeOn(Schedulers.from(executorService))
.subscribe(...)

Observable.from(Future) is blocking operation, as mentioned in documentation. You should use Observable.from(Future,Scheduler) instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM