How to make Parallel multiple non-blocking service request with RxJava2 & Retrofit2 - android

I need some help in implementing parallel asynchronous calls using RxJava2 & Retrofit2.
My requirements are;
1) I have multiple Insurer(for now I take only two),and I need to send multiple parallel requests using that insurer name.
2)If any of them give server error then remaining requests should not gets block.
Following is what I tried until now;
ArrayList<String> arrInsurer = new ArrayList<>();
arrInsurer.add(AppConstant.HDFC);
arrInsurer.add(AppConstant.ITGI);
RequestInterface service = getService(ServiceAPI.CAR_BASE_URL);
for (String insurerName : arrInsurer) {
service.viewQuote(Utils.getPrefQuoteId(QuoteListActivity.this), insurerName)
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<ViewQuoteResDTO>() {
#Override
public void accept(#NonNull ViewQuoteResDTO viewQuoteResDTO) throws Exception {
Log.e("Demo", viewQuoteResDTO.getPremiumData().getIDV()+"");
updateList();
}
}, new Consumer<Throwable>() {
#Override
public void accept(#NonNull Throwable throwable) throws Exception {
Log.e("Demo", throwable.getMessage());
}
});
}
private RequestInterface getService(String baseUrl) {
Gson gson = new GsonBuilder()
.setLenient()
.create();
return new Retrofit.Builder()
.baseUrl(baseUrl)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build().create(RequestInterface.class);
}
Now, Above code works fine only if both request give successful response. But when any of request give response as a internal server error then rest of request also gets block.
following Log error which I get when any of request give Failure response;
E/Demo: HTTP 500 Aww Snap, Some thing happened at server. Please try back again later.
E/Demo: unexpected end of stream on Connection{100.xxx.xxx.xx:portNo, proxy=DIRECT# hostAddress=/100.xxx.xxx.xx:portNo cipherSuite=none protocol=http/1.1}
How to handle this error?

I guess like any other Rx related question this has multiple answers. I will give you mine which I use in our app and solves exactly this use case. Hope it helps.
Short version - This relies on mergeDelayError. Check it out here
Why merge? Because unlike concat it will execute the observables in parallel. Why mergeDelayError? It delays the error... essentially it will execute every observable and deliver the error when everything finishes. This makes sure that even if one or several error, the others will still be executed.
You have to be careful with some details. The order of events is no longer preserved, meaning the merge operator may interleave some of the observable events (Given how you were doing things before, this shouldn't be an issue). As far as I know, even if multiple observables fail, you'll only get one onError call. If both of these are ok, then you could try the following:
List<Observable<ViewQuoteResDTO>> observables = new ArrayList<>();
for (String insurerName : arrInsurer) {
observables.add(service.viewQuote(
Utils.getPrefQuoteId(QuoteListActivity.this), insurerName));
}
Observable.mergeDelayError(observables)
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(/* subscriber calls if you need them */);
The idea is to create all observables that you're going to run and then use mergeDelayError to trigger them.

Related

How to use RxJava to wait the end of two lists of Retrofit requests

Let me describe my situation:
I want to register new records via an API.
I want to update some records via an API.
I need to be notified when all of these requests have finished, to start another task.
Specifically I have two ArrayList:
ArrayList<Report> createdReports = myHelper.getOfflineCreatedReports();
ArrayList<Report> editedReports = myHelper.getOfflineEditedReports();
Each report can use methods to get Observable instances from my ApiService (Retrofit implementation).
Observable<NewReportResponse> createdReportsObs = Observable.from(createdReports) // .just() != .from()
.flatMap(new Func1<Report, Observable<NewReportResponse>>() {
#Override
public Observable<NewReportResponse> call(Report report) {
return report.postToServer();
}
});
Observable<NewReportResponse> editedReportsObs = Observable.from(editedReports)
.flatMap(new Func1<Report, Observable<NewReportResponse>>() {
#Override
public Observable<NewReportResponse> call(Report report) {
return report.updateInServer();
}
});
I am using the flatMap operator to get one Observable for each report.
But I am not sure how to wait until all of the requests have finished.
I was thinking in using the zip operator.
Observable.zip(createdReportsObs, editedReportsObs, new Func2<NewReportResponse, NewReportResponse, Boolean>() {
#Override
public Boolean call(NewReportResponse justOneResponse, NewReportResponse justOneResponse2) {
return false;
}
});
Unfortunately I saw some examples where zip is used to create pairs of Observables.
Please suggest me what operator I can use to achieve it. Or how to do it using rxJava with a different approach.
Thank you in advance.
Are you using RxJava 2? If so you can use the new completable api. This is assuming you don't need to know any of the server results, just need to wait for them to complete.
Completeable.merge(createdReportsObs.toCompleteable(),
editedReportsObs.toCompleteable())
.subscribe()
This is my way. May not best practice.
Observable.merge(createdReportsObs, editedReportsObs)
.toList()
.flatMap(Observable::from)
.xxx //Now they are completed, do what you want
.subscribe();

Get one result from 2 observables with RxJava

I am new at RxJava and I have some pain to execute my first 'difficult' query.
I have two Observables generated from Retrofit, one that 'ping' a new api, the other the old one. The first one will query 'http://myurl.com/newapi/ping', the second one 'http://myurl.com/oldapi/ping'. Result from this request doesn't matter, I just want to know if the server is using the new or old api.
So I would like to call both observables at the same time, and finally have a boolean at the end to know if I'm using old or new api.
I tried something like that
Observable.mergeDelayError(obsOldApi,obsNewApi)
.observeOn(AndroidSchedulers.mainThread(), true)
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<String>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext(String s) {
}
});
But onError will be called once (I would like it to be called only if both request failed) and when onNext is called, I don't know from which request it came (old or new api ?).
Thank you for you help
For simplicity, let say that you'll received "NEW" or "OLD" regarding which api is available.
The difficulty of your operation is to manage errors : RxJava deals errors as terminal state. So you'll have to ignore this error, using .onErrorResumeNext() for example.
Observable<String> theOld = oldApi.map(r -> "OLD")
// ignore errors
.onErrorResumeNext(Obervable.empty());
Observable<String> theNew = newApi.map(r -> "NEW")
.onErrorResumeNext(Obervable.empty());
Observable.merge(theOld, theNew)
.first() // if both api are in errors
.subscribe(api -> System.out.println("Available API : "+api));
I added the operator first : it will take only the first result ("OLD" or "NEW") but trigger an error if the previous Observable is empty, which is the case if both API are unavaible.

Rxjava, retrofit and multiple calls

I've recently started using Rxjava and retrofit, and looking for any ideas on how to perform n number of retrofit post calls and track them via rxjava. Once all actions have been completed a UI event will then occur.
I found this article: http://randomdotnext.com/retrofit-rxjava/ however it uses a for loop for initiating multiple request observables. Maybe there is a more elegant way besides a for loop? What is the best rxjava operator for this kind of effort?
Instead of using for loop, you can create an Observable sequence from the List/Array then use flatMap/concatMap operator.
Using for loop:
GithubService service = ServiceFactory.createRetrofitService(GithubService.class, GithubService.SERVICE_ENDPOINT);
for(String login : Data.githubList) {
service.getUser(login)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Github>() {
#Override
public final void onCompleted() {
// do nothing
}
#Override
public final void onError(Throwable e) {
Log.e("GithubDemo", e.getMessage());
}
#Override
public final void onNext(Github response) {
mCardAdapter.addData(response);
}
});
}
Pure Rx:
GithubService service = ServiceFactory.createRetrofitService(GithubService.class, GithubService.SERVICE_ENDPOINT);
Observable.from(Data.githubList)
.flatMap(login -> service.getUser(login))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...);
RxJava provides a lot operators to combine multiple observables.
In your situation, you can use operator merge, and do UI work at onComplete()
When multiple call depend on the same thing you can use flat map or concat map to utilize your call. Then finally update your view.
Use the zip operator.
For Example :
you have 3 Retrofit Api and they are all return a string , and what you need is a long string merge by the 3 string.
So you need wait for the 3 api call are all return . and merge the return string with zip operator.
Code will be like:
Observable.zip(
api1,
api2,
api3,
(resp1, resp2, resp3) -> resp1 + resp2 + resp3
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(resp -> {
// do something
});

Do we have any possibility to stop request in OkHttp Interceptor?

In our app we met with one special case - if our App.specialFlag == true, we need stop any request from our code. And we think, that the best approach in this situation is include special Interceptor which will stop any our requests, something like this:
if (App.specialFlag) {
// Somehow stop request
} else {
return chain.proceed(chain.request());
}
Also, we should note, that we use RxJavaCallAdapterFactory for wrapping responses into Observables.
But we don't see any methods for stopping request in OkHttp Interceptors.
We came up with two ways to solve our issue:
a) Create special ApiService.class for wrapping every request in our API like this:
public Observable<User> getUserDetails() {
if (App.specialFlag) {
return Observable.just(null);
}
return api.getUserDetails();
}
But we think that it is ugly and cumbersome solution.
b) Throw a RuntimeException in Interceptor, then catch it in every place we use our API.
The second solution is also not good, because we need to include a lot of onErrorResumeNext operators in our chains.
May be someone knows, how to handle our situation in more 'clever' way?..
Thanks in advance!
One thing missing in the accepted answer is that you need to specify the protocol and the message. If you don't specify that you will get an Exception. Looking like this:
if (App.specialFlag) {
return new Response.Builder()
.code(418) // Whatever code
.body(ResponseBody.create(null, "")) // Whatever body
.protocol(Protocol.HTTP_2)
.message("Dummy response")
.request(chain.request())
.build();
} else {
return chain.proceed(chain.request());
}
One way to prevent request from being executed(before it starts) it to simply do as you tried to, but with an addition of Response.Builder:
if (App.specialFlag) {
return new Response.Builder()
.code(600) //Simply put whatever value you want to designate to aborted request.
.protocol(Protocol.HTTP_2)
.message("Dummy response")
.request(chain.request())
.build();
} else {
return chain.proceed(chain.request());
}
EDIT 26/09/2019
Updated answer with details mentioned by Bobstring
I would suggest to put the control logic script outside of the OkHttp, since it is used for making http request only. How to trigger request and how to handle the response to update the UI, it doesn't need to care about;
If you wanna, you may create a Class inherited from okhttp3.Interceptor, then override the function intercept and put your control logic script inside.
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.addInterceptor(new Interceptor())
.build();

Rx-Java centralized error handling

I use Retrofit as network library with Rx-Java. I want to make some centralized error checking for most requests and handle errors or pass it to subscriber's onError() if I cannot handle it. How could I do this?
Something like this
projectListObservable
.subscribeOn(Schedulers.newThread())
.timeout(App.NETWORK_TIMEOUT_SEC, TimeUnit.SECONDS)
.retry(App.NETWORK_RETRY_COUNT)
.onError(e -> {
if (e instanseOf HttpError && ((HttpError)e).getCode == 403){
App.getInstance.getNetworkManager.reAuth();
} else {
throw(e);
}})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new ProjectListSubscriber());
Also, I should stop retrying in that case, but keep retry if it's a network problem (instanceof IOException).
I think you want to implement too many tihngs with a single observable.
You can have some HttpErrorHandler which will have method
boolean fix(Exception e)
if error handler fixed the exception(renew token etc) return true. You can have differrent error handlers for different cases or one for everything. TokenErrorHandler RetryErrorHandler and then make a chain.
If all error hadlers return false, throw this exception to the up level

Categories

Resources