Example usage for com.squareup.okhttp OkHttpClient getDispatcher

List of usage examples for com.squareup.okhttp OkHttpClient getDispatcher

Introduction

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

Prototype

public Dispatcher getDispatcher() 

Source Link

Usage

From source file:com.dzhyun.sdk.DzhChannel.java

License:Open Source License

@ReactMethod
public void connect(final String url, final int id) {
    OkHttpClient client = new OkHttpClient();

    client.setConnectTimeout(10, TimeUnit.SECONDS);
    client.setWriteTimeout(10, TimeUnit.SECONDS);
    // Disable timeouts for read
    client.setReadTimeout(0, TimeUnit.MINUTES);

    Request request = new Request.Builder().tag(id).url(url).build();

    WebSocketCall.create(client, request).enqueue(new WebSocketListener() {

        @Override//from  w  w  w. j a  v  a  2 s  .  co m
        public void onOpen(WebSocket webSocket, Response response) {
            mWebSocketConnections.put(id, webSocket);
            WritableMap params = Arguments.createMap();
            params.putInt("id", id);
            sendEvent("dzhChannelOpen", params);
        }

        @Override
        public void onClose(int code, String reason) {
            WritableMap params = Arguments.createMap();
            params.putInt("id", id);
            params.putInt("code", code);
            params.putString("reason", reason);
            sendEvent("dzhChannelClosed", params);
        }

        @Override
        public void onFailure(IOException e, Response response) {
            notifyWebSocketFailed(id, e.getMessage());
        }

        @Override
        public void onPong(Buffer buffer) {
        }

        @Override
        public void onMessage(BufferedSource bufferedSource, WebSocket.PayloadType payloadType) {
            String message;
            if (payloadType == WebSocket.PayloadType.BINARY) {

                try {
                    message = Pb2Json.toJson(bufferedSource.readByteArray());
                    bufferedSource.close();
                } catch (IOException e) {
                    FLog.e(ReactConstants.TAG, "decode pb failed " + id, e);
                    return;
                }

            } else {
                try {
                    message = bufferedSource.readUtf8();
                } catch (IOException e) {
                    notifyWebSocketFailed(id, e.getMessage());
                    return;
                }
                try {
                    bufferedSource.close();
                } catch (IOException e) {
                    FLog.e(ReactConstants.TAG, "Could not close BufferedSource for WebSocket id " + id, e);
                }

            }

            WritableMap params = Arguments.createMap();
            params.putInt("id", id);
            params.putString("data", message);
            sendEvent("dzhChannelMessage", params);
        }
    });

    // Trigger shutdown of the dispatcher's executor so this process can exit cleanly
    client.getDispatcher().getExecutorService().shutdown();
}

From source file:com.facebook.imagepipeline.backends.okhttp.OkHttpNetworkFetcher.java

License:Open Source License

/**
 * @param okHttpClient client to use/*from   w ww  .  ja  v a  2 s.c  o  m*/
 */
public OkHttpNetworkFetcher(OkHttpClient okHttpClient) {
    mOkHttpClient = okHttpClient;
    mCancellationExecutor = okHttpClient.getDispatcher().getExecutorService();
}

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);// w  ww .  jav a 2s .c om

    return c;
}

From source file:com.raskasa.metrics.okhttp.InstrumentedOkHttpClientsTest.java

License:Apache License

private void assertThatClientsAreEqual(OkHttpClient clientA, OkHttpClient clientB) {
    assertThat(clientA.getDispatcher()).isEqualTo(clientB.getDispatcher());
    assertThat(clientA.getProxy()).isEqualTo(clientB.getProxy());
    assertThat(clientA.getProtocols()).isEqualTo(clientB.getProtocols());
    assertThat(clientA.getConnectionSpecs()).isEqualTo(clientB.getConnectionSpecs());
    assertThat(clientA.getProxySelector()).isEqualTo(clientB.getProxySelector());
    assertThat(clientA.getCookieHandler()).isEqualTo(clientB.getCookieHandler());
    assertThat(clientA.getCache()).isEqualTo(clientB.getCache());
    assertThat(clientA.getSocketFactory()).isEqualTo(clientB.getSocketFactory());
    assertThat(clientA.getSslSocketFactory()).isEqualTo(clientB.getSslSocketFactory());
    assertThat(clientA.getHostnameVerifier()).isEqualTo(clientB.getHostnameVerifier());
    assertThat(clientA.getCertificatePinner()).isEqualTo(clientB.getCertificatePinner());
    assertThat(clientA.getAuthenticator()).isEqualTo(clientB.getAuthenticator());
    assertThat(clientA.getConnectionPool()).isEqualTo(clientB.getConnectionPool());
    assertThat(clientA.getFollowSslRedirects()).isEqualTo(clientB.getFollowSslRedirects());
    assertThat(clientA.getFollowRedirects()).isEqualTo(clientB.getFollowRedirects());
    assertThat(clientA.getRetryOnConnectionFailure()).isEqualTo(clientB.getRetryOnConnectionFailure());
    assertThat(clientA.getConnectTimeout()).isEqualTo(clientB.getConnectTimeout());
    assertThat(clientA.getReadTimeout()).isEqualTo(clientB.getReadTimeout());
    assertThat(clientA.getWriteTimeout()).isEqualTo(clientB.getWriteTimeout());
}

From source file:com.spotify.apollo.http.client.OkHttpClientProvider.java

License:Apache License

@Override
public OkHttpClient get() {
    final OkHttpClient client = new OkHttpClient();

    //timeouts settings
    config.connectTimeoutMillis().ifPresent(millis -> client.setConnectTimeout(millis, TimeUnit.MILLISECONDS));

    config.readTimeoutMillis().ifPresent(millis -> client.setReadTimeout(millis, TimeUnit.MILLISECONDS));

    config.writeTimeoutMillis().ifPresent(millis -> client.setWriteTimeout(millis, TimeUnit.MILLISECONDS));

    // connection pool settings
    client.setConnectionPool(new ConnectionPool(
            // defaults that come from com.squareup.okhttp.ConnectionPool
            config.maxIdleConnections().orElse(5),
            config.connectionKeepAliveDurationMillis().orElse(5 * 60 * 1000)));

    // async dispatcher settings
    config.maxAsyncRequests().ifPresent(max -> client.getDispatcher().setMaxRequests(max));

    config.maxAsyncRequestsPerHost().ifPresent(max -> client.getDispatcher().setMaxRequestsPerHost(max));

    closer.register(ExecutorServiceCloser.of(client.getDispatcher().getExecutorService()));

    return client;
}

From source file:com.spotify.apollo.http.client.OkHttpClientProviderTest.java

License:Apache License

@Test
public void testMaxRequests() {
    final OkHttpClient client = buildClient("http.client.async.maxRequests: 72");
    assertEquals(72, client.getDispatcher().getMaxRequests());
}

From source file:com.spotify.apollo.http.client.OkHttpClientProviderTest.java

License:Apache License

@Test
public void testMaxRequestsPerHost() {
    final OkHttpClient client = buildClient("http.client.async.maxRequestsPerHost: 79");
    assertEquals(79, client.getDispatcher().getMaxRequestsPerHost());
}