Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set httpExecutor in Retrofit 2? #1259

Closed
dmarusic35 opened this issue Nov 5, 2015 · 7 comments
Closed

How to set httpExecutor in Retrofit 2? #1259

dmarusic35 opened this issue Nov 5, 2015 · 7 comments

Comments

@dmarusic35
Copy link

Retrofit 1 has RestAdapter.Builder().setExecutors method which sets both httpExecutor and callbackExecutor while Retrofit 2 allows only to set callbackExecutor. Is there any way to set httpExecutor in Retrofit 2?

@JakeWharton
Copy link
Member

Retrofit 2 uses OkHttp's async API (assuming you use Call.enqueue) which has manages its own threads per host for execution. OkHttp allows setting a Dispatcher which is the object the owns the threading, and can be created with an ExecutorService. if you just want to monitor the background execution, you can ask the OkHttpClient for its Dispatcher and then ask that instance for its ExecutorService.

@dmarusic35
Copy link
Author

Thanks for quick reply

@JakeWharton
Copy link
Member

Our of curiosity, are you just trying to monitor the background threads or
do you need to supply your own due some reason?

On Thu, Nov 5, 2015, 3:03 PM Damian Marusic notifications@github.com
wrote:

Thanks for quick reply


Reply to this email directly or view it on GitHub
#1259 (comment).

@dmarusic35
Copy link
Author

I needed to supply my own Executor because when I run tests using Robolectric I want calls to be executed synchronously, on main thread. Manage to do it now by setting up Dispatcher, which is created by my implementation of ExecutorService, as you suggested.

@JakeWharton
Copy link
Member

Ah, OK. Great!

On Thu, Nov 5, 2015, 4:01 PM Damian Marusic notifications@github.com
wrote:

I needed to supply my own Executor because when I run tests using
Robolectric I want calls to be executed synchronously, on main thread.
Manage to do it now by setting up Dispatcher, which is created by my
implementation of ExecutorService, as you suggested.


Reply to this email directly or view it on GitHub
#1259 (comment).

@pkhivesara
Copy link

@dmarusic35 Can you provide a snippet of how to go about creating the dispatcher and setting it up on the rest client?

@dmarusic35
Copy link
Author

@pkhivesara I provide Dispatcher using Dagger. For app, I use default implementation:

@Provides
@Singleton
public Dispatcher provideDispatcher() {
    return new Dispatcher();
}

On the other hand, for tests, I use Dispatcher with my own implementation of ExecutorService (method execute(Runnable command) is where magic happens):

@Provides
@Singleton
public Dispatcher provideDispatcher() {
    // this dispatcher will ensure networking is done on the main thread
    return new Dispatcher(new SynchronousExecutorService());
}

private class SynchronousExecutorService implements ExecutorService {

    @Override
    public void shutdown() {

    }

    @NonNull
    @Override
    public List<Runnable> shutdownNow() {
        return null;
    }

    @Override
    public boolean isShutdown() {
        return false;
    }

    @Override
    public boolean isTerminated() {
        return false;
    }

    @Override
    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
        return false;
    }

    @NonNull
    @Override
    public <T> Future<T> submit(Callable<T> task) {
        return null;
    }

    @NonNull
    @Override
    public <T> Future<T> submit(Runnable task, T result) {
        return null;
    }

    @NonNull
    @Override
    public Future<?> submit(Runnable task) {
        return null;
    }

    @NonNull
    @Override
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
        return null;
    }

    @NonNull
    @Override
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
            throws InterruptedException {
        return null;
    }

    @NonNull
    @Override
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
        return null;
    }

    @Override
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
            throws InterruptedException, ExecutionException, TimeoutException {
        return null;
    }

    @Override
    public void execute(Runnable command) {
        command.run();
    }
}

And I just set provided dispatcher to OkHttpClient:

OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
okHttpClientBuilder.dispatcher(dispatcher);
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants