My goal is to limit the number of parallel executing request by throwing an exception.
For example, I want only one executing request:
someApi.getUser(
result -> print("ok: " + result), exception -> print("error: " + exception)
); // this request will be executed in 5 seconds
someApi.getServerInfo(
result -> print("ok: " + result), exception -> print("error: " + exception)
); // there I want to catch exception like ExecutorIsBusy
How can I implement it with Retrofit 2.0?
I am not sure that throwing an Exception
is the best way to do it, but I don't know your use case so I will not discuss this point :)
Anyway, the comment of @Daniel actually points to a pretty good direction. If you're using retrofit
with OkHttp
, it's the OkHttpClient
that will handle the "concurrent requests" stuff. Reading the docs, you could see that OkHttp uses a Dispatcher
to handle parallel asynchronous requests (Dispatcher docs).
So two interesting points are :
setMaxRequests(int maxRequests)
: defines the maximum concurrent requestsexecuted(RealCall call)
: actually executes a requestsI think you could do this to achieve your goal :
Dispatcher
classexecuted(RealCall call)
method throwing an exception if the number of currents request is superior to maxRequests
Dispatcher
in the OkHttpClient
you're using with retrofit
Using RxJava & taking the approach in the above comments, here is an example:
Assume that these operations are attached to buttons.
public void runObservable1(View view) {
if (!taskRunning){
try{
taskRunning = true;
subsonicService.runTask1()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(this.<Result>bindUntilEvent(ActivityEvent.DESTROY))
.subscribe(new Subscriber<Result>() {
@Override
public void onCompleted() {
taskRunning = false;
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Result result) {
//your logic here
}
});
} catch (IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(this, "Task is running you must wait", Toast.LENGTH_SHORT).show();
}
}
public void runObservable2(View view) {
if (!taskRunning){
try{
taskRunning = true;
subsonicService.runTask2()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(this.<Result>bindUntilEvent(ActivityEvent.DESTROY))
.subscribe(new Subscriber<Result>() {
@Override
public void onCompleted() {
taskRunning = false;
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Result result) {
//Logic here
}
});
} catch (IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(this, "Task is running you must wait", Toast.LENGTH_SHORT).show();
}
}
I am also not an expert at RxJava so there may be an operator that makes this easier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With