Parallel HTTP requests with Retrofit
Retrofit uses an Executor
for queueing requests.
The default uses Executors.newCachedThreadPool
which allows for unlimited threads. This fits most use cases since normally you would only ever have one or two requests happening at once.
You can change this behavior, however, by supplying your own when building the RestAdapter
. Call setExecutors
and pass in an executor that uses a confined thread pool (limited to whatever number you would like). For the second argument, simply pass a new instance of MainThreadExecutor
so that callbacks happen on the main thread.
Related videos on Youtube
Vektor88
Updated on September 15, 2022Comments
-
Vektor88 over 1 year
I have an Android application that is currently using
Volley
library to make network requests and show downloaded images withNetworkImageView
.I would like to test
Retrofit
's capabilities and since I need to run lots of requests (thousands) I'm a bit concerned about the parallel execution.Volley
handles parallel requests with theRequestQueue
that limits the concurrent running requests to four, while the other requests are enqueued waiting to be executed. InRetrofit
documentations I haven't found any way to handle the number of concurrent requests and I suspect that such details are left to the developer in this library.Is this correct? If so, is there any android-oriented implementation/library available? Otherwise, what are the best practices to handle parallel requests?
-
Vektor88 about 10 yearsAre you suggesting to use a
ThreadPoolExecutor
to handle my threads? If yes, I was using this before migrating toVolley
, but I had two main problems: 1) I was losing some requests because the queue was full, Volley has an unbounded queue. 2) Some requests were removed from the queue because they were waiting too long. -
Jake Wharton about 10 yearsYes. Using an unbounded queue will solve both 1 and 2. There are a few in
java.util.concurrent
to choose from. -
Jake Wharton about 10 yearsAwesome! Glad it was helpful.
-
Kaloyan Roussev almost 9 yearsPlease provide code example of how to limit to max 5 requests at a time
-
Matthew Daumen almost 9 years
RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(SomeEndpoint) .setExecutors(Executors.newFixedThreadPool(5), null) .build();
Note the second arg is the callback executor - this will put the callback on the same thread as the client. square.github.io/retrofit/javadoc/retrofit/… -
remedy. almost 9 years@JakeWharton So when building the RestAdapter and using
setExecutors
, it will set a specified number of allowed requests to be put into the queue (if needed) correct? How would one be able to handle the concurrency upon receiving the response from theCallBack
? Does retrofit handle this? -
Riten almost 8 yearsCan this method be used with the latest Retrofit 2.0 ??
-
pba about 6 yearsWith retrofit 2.0, as in written in this retrofit github issue, OkHttpClient dispatcher has to be used. There is a sample in this other github issue.