Parallel HTTP requests with Retrofit

14,760

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.

Share:
14,760

Related videos on Youtube

Vektor88
Author by

Vektor88

Updated on September 15, 2022

Comments

  • Vektor88
    Vektor88 over 1 year

    I have an Android application that is currently using Volley library to make network requests and show downloaded images with NetworkImageView.

    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 the RequestQueue that limits the concurrent running requests to four, while the other requests are enqueued waiting to be executed. In Retrofit 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
    Vektor88 about 10 years
    Are you suggesting to use a ThreadPoolExecutor to handle my threads? If yes, I was using this before migrating to Volley, 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
    Jake Wharton about 10 years
    Yes. Using an unbounded queue will solve both 1 and 2. There are a few in java.util.concurrent to choose from.
  • Jake Wharton
    Jake Wharton about 10 years
    Awesome! Glad it was helpful.
  • Kaloyan Roussev
    Kaloyan Roussev almost 9 years
    Please provide code example of how to limit to max 5 requests at a time
  • Matthew Daumen
    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.
    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 the CallBack? Does retrofit handle this?
  • Riten
    Riten almost 8 years
    Can this method be used with the latest Retrofit 2.0 ??
  • pba
    pba about 6 years
    With 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.