Example usage for com.squareup.okhttp Dispatcher Dispatcher

List of usage examples for com.squareup.okhttp Dispatcher Dispatcher

Introduction

In this page you can find the example usage for com.squareup.okhttp Dispatcher Dispatcher.

Prototype

public Dispatcher(ExecutorService executorService) 

Source Link

Usage

From source file:co.paralleluniverse.fibers.okhttp.InterceptorTest.java

License:Open Source License

/**
 * When an interceptor throws an unexpected exception, asynchronous callers are left hanging. The
 * exception goes to the uncaught exception handler.
 *
 * TODO(jwilson): test that resources are not leaked when this happens.
 *///  www  . j  av a  2  s  .  co  m
private void interceptorThrowsRuntimeExceptionAsynchronous(List<Interceptor> interceptors) throws Exception {
    interceptors.add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            throw new RuntimeException("boom!");
        }
    });

    ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
    client.setDispatcher(new Dispatcher(executor));

    Request request = new Request.Builder().url(server.getUrl("/")).build();
    client.newCall(request).enqueue(callback);

    assertEquals("boom!", executor.takeException().getMessage());
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

/**
 * When an interceptor throws an unexpected exception, asynchronous callers are left hanging. The
 * exception goes to the uncaught exception handler.
 *
 * TODO(jwilson): test that resources are not leaked when this happens.
 *//* w  w  w  .j a  v  a 2 s .co m*/
private void interceptorThrowsRuntimeExceptionAsynchronous(List<Interceptor> interceptors) throws Exception {
    interceptors.add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            throw new RuntimeException("boom!");
        }
    });

    ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
    client.setDispatcher(new Dispatcher(executor));

    Request request = new Request.Builder().url(server.url("/")).build();
    client.newCall(request).enqueue(callback);

    assertEquals("boom!", executor.takeException().getMessage());
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

@Ignore
@Test//from   www.  j  a  v  a 2  s  .  c  o m
public void applicationInterceptorReturnsNull() throws Exception {
    server.enqueue(new MockResponse());

    Interceptor interceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            chain.proceed(chain.request());
            return null;
        }
    };
    client.interceptors().add(interceptor);

    ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
    client.setDispatcher(new Dispatcher(executor));

    Request request = new Request.Builder().url(server.url("/")).build();
    try {
        client.newCall(request).execute();
        fail();
    } catch (NullPointerException expected) {
        assertEquals("application interceptor " + interceptor + " returned null", expected.getMessage());
    }
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

@Ignore
@Test//from  www. j a  v  a 2 s.c o m
public void networkInterceptorReturnsNull() throws Exception {
    server.enqueue(new MockResponse());

    Interceptor interceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            chain.proceed(chain.request());
            return null;
        }
    };
    client.networkInterceptors().add(interceptor);

    ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
    client.setDispatcher(new Dispatcher(executor));

    Request request = new Request.Builder().url(server.url("/")).build();
    try {
        client.newCall(request).execute();
        fail();
    } catch (NullPointerException expected) {
        assertEquals("network interceptor " + interceptor + " returned null", expected.getMessage());
    }
}

From source file:com.frostwire.http.HttpClient.java

License:Open Source License

private static OkHttpClient buildClient(Params params) {
    OkHttpClient c = Loader.DEFAULT_CLIENT.clone();
    ExecutorService pool = params.pool != null ? params.pool : c.getDispatcher().getExecutorService();

    Dispatcher d = new Dispatcher(pool);
    d.setMaxRequests(params.maxRequests);
    d.setMaxRequestsPerHost(params.maxRequestsPerHost);
    c.setDispatcher(d);//from  w w  w.j ava 2  s .c om

    return c;
}

From source file:com.frostwire.util.http.OKHTTPClient.java

License:Open Source License

public static OkHttpClient newOkHttpClient(ThreadPool pool) {
    OkHttpClient searchClient = new OkHttpClient();
    searchClient.setDispatcher(new Dispatcher(pool));
    searchClient.setFollowRedirects(true);
    searchClient.setFollowSslRedirects(true);
    searchClient.setConnectTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
    // Maybe we should use a custom connection pool here. Using default.
    //searchClient.setConnectionPool(?);
    return searchClient;
}

From source file:com.github.mmatczuk.ablb.benchmark.OkHttpAsync.java

License:Apache License

@Override
public void prepare(final Benchmark benchmark) {
    concurrencyLevel = benchmark.concurrencyLevel;
    targetBacklog = benchmark.targetBacklog;

    client = new OkHttpClient();
    client.setDispatcher(new Dispatcher(new ThreadPoolExecutor(benchmark.concurrencyLevel,
            benchmark.concurrencyLevel, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>())));

    callback = new Callback() {
        @Override/* w w  w  . j a va  2  s.  c  o m*/
        public void onFailure(Request request, IOException e) {
            System.out.println("Failed: " + e);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            ResponseBody body = response.body();
            long total = readAllAndClose(body.byteStream());
            long finish = System.nanoTime();
            if (VERBOSE) {
                long start = (Long) response.request().tag();
                System.out.printf("Transferred % 8d bytes in %4d ms%n", total,
                        TimeUnit.NANOSECONDS.toMillis(finish - start));
            }
            requestsInFlight.decrementAndGet();
        }
    };
}

From source file:com.google.maps.ApiContext.java

License:Open Source License

public ApiContext(String url) {
    rateLimitExecutorService = new RateLimitExecutorService();
    client.setDispatcher(new Dispatcher(rateLimitExecutorService));
    host = url;
}

From source file:com.google.maps.GeoApiContext.java

License:Open Source License

public GeoApiContext() {
    rateLimitExecutorService = new RateLimitExecutorService();
    client.setDispatcher(new Dispatcher(rateLimitExecutorService));
}

From source file:com.google.maps.OkHttpRequestHandler.java

License:Open Source License

public OkHttpRequestHandler() {
    rateLimitExecutorService = new RateLimitExecutorService();
    client.setDispatcher(new Dispatcher(rateLimitExecutorService));
}