简体   繁体   English

使用Rx Java Observable一次进行多个异步调用(触发并忘记调用)

[英]Make multiple asynchronous calls(fire and forget calls) at once using Rx java Observable

I have a list of downstream api calls(about 10) that I need to call at once asynchronously. 我有需要异步异步调用的下游api调用列表(约10个)。 I was using callables till now where I was using 直到现在我一直在使用可调用对象

List<RequestContextPreservingCallable <FutureResponse>> callables

I would add the api calls to this list and submit it at the end using executeAsyncNoReturnRequestContextPreservingCallables. 我会将api调用添加到此列表中,并在最后使用executeAsyncNoReturnRequestContextPreservingCallables提交。

Using Rx java Observables how do I do this? 使用Rx Java Observables我该怎么做?

List<RequestContextPreservingCallable<FutureResponse>> callables = new 
ArrayList<RequestContextPreservingCallable<FutureResponse>>();

callables.add(apiOneConnector.CallToApiOne(name));
callables.add(apiTwoConnector.CallToApiTWO(sessionId));
....

//execute all the calls
executeAsyncNoReturnRequestContextPreservingCallables(callables);

You could make use of the zip operator. 您可以使用zip运算符。 The zip operator can take multiple observables and execute them simultaneously, and it will proceed after all the results have arrived. zip运算符可以获取多个可观察对象并同时执行它们,并且在所有结果到达后它将​​继续进行。

You could then transform these result into your needed form and pass to the next level. 然后,您可以将这些结果转换为所需的形式并传递到下一个级别。

As per your example. 按照你的例子。 Say you have multiple API calls for getting name and session etc, as shown below 假设您有多个用于获取名称和会话等的API调用,如下所示

Observable.zip(getNameRequest(), getSessionIdRequest(), new BiFunction<String, String, Object>() {
        @Override
        public Object apply(String name, String sessionId) throws Exception {
            // here you will get all the results once everything is completed. you can then take these 
            // results and transform into another object and returnm from here. I decided to transform the results into an Object[]
            // the retuen type of this apply funtion is generic, so you can choose what to return
            return new Object[]{name, sessionId};
        }
    })
    .subscribeOn(Schedulers.io())  // will start this entire chain in an IO thread
    .observeOn(AndroidSchedulers.mainThread()) // observeOn will filp the thread to the given one , so that the downstream will be executed in the specified thread. here I'm switching to main at this point onwards
    .subscribeWith(new DisposableObserver<Object>() {
        @Override
        public void onNext(Object finalResult) {
           // here you will get the final result with all the api results
        }

        @Override
        public void onError(Throwable e) {
            // any error during the entire process will be triggered here
        }

        @Override
        public void onComplete() {
             //will be called once the whole chain is completed and terminated
        }
    });

You could even pass a list of observables to the zip as follows 您甚至可以按如下方式将可观察对象列表传递给zip

    List<Observable<String>> requests = new ArrayList<>();
    requests.add(getNameRequest());
    requests.add(getSessionIdRequest());

    Observable.zip(requests, new Function<Object[], Object[]>() {
        @Override
        public Object[] apply(Object[] objects) throws Exception {
            return new Object[]{objects[0], objects[1]};
        }
    }).subscribeWith(new DisposableObserver<Object[]>() {
        @Override
        public void onNext(Object[] objects) {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onComplete() {

        }
    })          

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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