List of usage examples for com.squareup.okhttp Dispatcher Dispatcher
public Dispatcher(ExecutorService executorService)
From source file:com.raskasa.metrics.okhttp.InstrumentedOkHttpClient.java
License:Apache License
private void instrumentExecutorService() { InstrumentedExecutorService executorService = new InstrumentedExecutorService( rawClient.getDispatcher().getExecutorService(), registry, name(OkHttpClient.class, this.name)); rawClient.setDispatcher(new Dispatcher(executorService)); }
From source file:com.raskasa.metrics.okhttp.InstrumentedOkHttpClientTest.java
License:Apache License
@Test public void executorServiceIsInstrumented() throws Exception { server.enqueue(new MockResponse().setBody("one")); server.enqueue(new MockResponse().setBody("two")); HttpUrl baseUrl = server.url("/"); rawClient.setDispatcher(new Dispatcher(MoreExecutors.newDirectExecutorService())); // Force the requests to execute on this unit tests thread. InstrumentedOkHttpClient client = new InstrumentedOkHttpClient(registry, rawClient, null); assertThat(registry.getMeters().get(client.metricId("network-requests-submitted")).getCount()).isEqualTo(0); assertThat(registry.getMeters().get(client.metricId("network-requests-completed")).getCount()).isEqualTo(0); Request req1 = new Request.Builder().url(baseUrl).build(); Request req2 = new Request.Builder().url(baseUrl).build(); client.newCall(req1).enqueue(new TestCallback()); client.newCall(req2).enqueue(new TestCallback()); assertThat(registry.getMeters().get(client.metricId("network-requests-submitted")).getCount()).isEqualTo(2); assertThat(registry.getMeters().get(client.metricId("network-requests-completed")).getCount()).isEqualTo(2); }
From source file:lumbermill.internal.elasticsearch.ElasticsearchClientFactory.java
License:Apache License
private ElasticSearchOkHttpClientImpl createClient(MapWrap config) { boolean isPrefix; String index;/*from ww w .j a v a 2 s . co m*/ String url = config.asString("url"); if (config.exists("index_prefix")) { isPrefix = true; index = config.asString("index_prefix"); } else { isPrefix = false; index = config.asString("index"); } String cacheKey = cacheKey(url, index, isPrefix); if (cachedClients.containsKey(cacheKey)) { LOGGER.trace("Using cached Elasticsearch client"); return cachedClients.get(cacheKey); } LOGGER.trace("Creating new Elasticsearch client"); final ElasticSearchOkHttpClientImpl es = new ElasticSearchOkHttpClientImpl(url, index, config.asString("type"), isPrefix); if (config.exists("document_id")) { es.withDocumentId(config.asString("document_id")); } if (config.exists("signer")) { es.withSigner(config.getObject("signer")); } if (config.exists("basic_auth")) { if (config.exists("signer")) { LOGGER.warn("A client cannot have both signed (AWS) and basic auth. Disabling basic auth"); } else { String auth = config.asString("basic_auth"); if (!auth.contains(":")) { throw new IllegalArgumentException( "Invalid basic_auth value, expected 'user:passwd' but was " + auth); } if (auth.length() > 1) { String[] split = auth.split(":"); es.withBasicAuth(split[0], split[1]); } } } if (config.exists("timestamp_field")) { es.withTimestampField(config.asString("timestamp_field")); } if (config.exists("retry")) { MapWrap retryConfig = MapWrap.of(config.getObject("retry")).assertExists("policy"); es.withRetryTimer(Observables.timer(retryConfig), retryConfig.asInt("attempts", DEFAULT_ATTEMPTS)); } if (config.exists("dispatcher")) { LOGGER.info("Configuring http dispatcher"); MapWrap dispatchConfig = MapWrap.of(config.getObject("dispatcher")); Dispatcher dispatcher = dispatchConfig.exists("threadpool") ? new Dispatcher(dispatchConfig.getObject("threadpool")) : new Dispatcher(); dispatcher.setMaxRequests(dispatchConfig.exists("max_concurrent_requests") ? dispatchConfig.asInt("max_concurrent_requests") : dispatcher.getMaxRequests()); dispatcher.setMaxRequestsPerHost(dispatchConfig.exists("max_concurrent_requests") ? dispatchConfig.asInt("max_concurrent_requests") : dispatcher.getMaxRequestsPerHost()); es.withDispatcher(dispatcher); } cachedClients.put(cacheKey, es); return es; }
From source file:net.yatomiya.e4.services.image.ImageService.java
License:Open Source License
public void initialize(IEclipseContext context) { this.context = context; entryMap = new WeakValueHashMap<>(); Application app = context.get(Application.class); rootDirectory = new File(app.getDataPath(), ROOT_PATH); try {//from ww w . j a va 2s.c o m IOUtils.createDirectory(rootDirectory); } catch (IOException e) { throw new IllegalStateException(e); } storageManager = new StorageManager(this); storageManager.load(); storageUpdateExecutor = new QueueThreadExecutor(16); { priorityQueue = new PriorityBlockingQueue<Runnable>(16, new StandardHttpClient.CallComparator<UpdateHandler>((o1, o2) -> o1.compareTo(o2))); httpThreadExecutor = new ThreadPoolExecutor(1, Integer.MAX_VALUE, 60, TimeUnit.SECONDS, priorityQueue); httpClient = new StandardHttpClient(); httpClient.setDispatcher(new Dispatcher(httpThreadExecutor)); } undisposedImagesCleaner = new UndisposedImagesCleaner(); backgroundSaver = new RepeatableRunner((int) (Math.random() * 1000 * 60 * 5), 1000 * 60 * 5, () -> storageManager.save()); thumbnailSize = new Point(64, 64); memoryCacheRetainTime = 1000 * 60; }
From source file:org.dkf.jed2k.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.setHostnameVerifier(new HostnameVerifier() { @Override//from ww w . j a v a2 s.c o m public boolean verify(String hostname, SSLSession session) { return true; } }); searchClient.setSslSocketFactory(CUSTOM_SSL_SOCKET_FACTORY); searchClient.setConnectTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS); // Maybe we should use a custom connection pool here. Using default. //searchClient.setConnectionPool(?); return searchClient; }