use of okhttp3.Dispatcher in project okhttp by square.
the class WebSocketHttpTest method readTimeoutAppliesWithinFrames.
/**
* There's no read timeout when reading the first byte of a new frame. But as soon as we start
* reading a frame we enable the read timeout. In this test we have the server returning the first
* byte of a frame but no more frames.
*/
@Test
public void readTimeoutAppliesWithinFrames() {
webServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) {
return upgradeResponse(request).setBody(// Truncated frame.
new Buffer().write(ByteString.decodeHex("81"))).removeHeader("Content-Length").setSocketPolicy(SocketPolicy.KEEP_OPEN);
}
});
WebSocket webSocket = newWebSocket();
clientListener.assertOpen();
clientListener.assertFailure(SocketTimeoutException.class, "timeout", "Read timed out");
assertThat(webSocket.close(1000, null)).isFalse();
}
use of okhttp3.Dispatcher in project flink by apache.
the class FlinkDistribution method startFlinkCluster.
public void startFlinkCluster() throws IOException {
LOG.info("Starting Flink cluster.");
AutoClosableProcess.create(bin.resolve("start-cluster.sh").toAbsolutePath().toString()).setEnv(env -> env.remove("FLINK_LOG_DIR")).runBlocking();
final OkHttpClient client = new OkHttpClient();
final Request request = new Request.Builder().get().url("http://localhost:8081/taskmanagers").build();
Exception reportedException = null;
for (int retryAttempt = 0; retryAttempt < 30; retryAttempt++) {
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
final String json = response.body().string();
final JsonNode taskManagerList = OBJECT_MAPPER.readTree(json).get("taskmanagers");
if (taskManagerList != null && taskManagerList.size() > 0) {
LOG.info("Dispatcher REST endpoint is up.");
return;
}
}
} catch (IOException ioe) {
reportedException = ExceptionUtils.firstOrSuppressed(ioe, reportedException);
}
LOG.info("Waiting for dispatcher REST endpoint to come up...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
reportedException = ExceptionUtils.firstOrSuppressed(e, reportedException);
}
}
throw new AssertionError("Dispatcher REST endpoint did not start in time.", reportedException);
}
use of okhttp3.Dispatcher in project pinpoint by naver.
the class OkHttpClient_3_0_0_to_3_3_x_IT method enqueue.
@Test
public void enqueue() throws Exception {
Request request = new Request.Builder().url(webServer.getCallHttpUrl()).build();
OkHttpClient client = new OkHttpClient();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Response> responseRef = new AtomicReference<>(null);
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
latch.countDown();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
responseRef.set(response);
latch.countDown();
}
});
latch.await(3, TimeUnit.SECONDS);
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.awaitTrace(event(ASYNC.getName(), "Asynchronous Invocation"), 20, 3000);
verifier.printCache();
Method realCallEnqueueMethod = Class.forName("okhttp3.RealCall").getDeclaredMethod("enqueue", Class.forName("okhttp3.Callback"));
verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), realCallEnqueueMethod));
Method dispatcherEnqueueMethod = Class.forName("okhttp3.Dispatcher").getDeclaredMethod("enqueue", Class.forName("okhttp3.RealCall$AsyncCall"));
verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), dispatcherEnqueueMethod));
verifier.verifyTrace(event(ASYNC.getName(), "Asynchronous Invocation"));
Method executeMethod = Class.forName("okhttp3.RealCall$AsyncCall").getDeclaredMethod("execute");
verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), executeMethod));
Method sendRequestMethod = Class.forName("okhttp3.internal.http.HttpEngine").getDeclaredMethod("sendRequest");
verifier.verifyTrace(event(OK_HTTP_CLIENT.getName(), sendRequestMethod, null, null, webServer.getHostAndPort(), annotation("http.url", request.url().toString())));
String hostAndPort = HostAndPort.toHostAndPortString(request.url().host(), request.url().port());
Method connectMethod = Class.forName("okhttp3.internal.http.HttpEngine").getDeclaredMethod("connect");
verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), connectMethod, annotation("http.internal.display", hostAndPort)));
Response response = responseRef.get();
Method readResponseMethod = Class.forName("okhttp3.internal.http.HttpEngine").getDeclaredMethod("readResponse");
verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), readResponseMethod, annotation("http.status.code", response.code())));
verifier.verifyTraceCount(0);
}
use of okhttp3.Dispatcher in project pinpoint by naver.
the class OkHttpClient_3_4_0_BaseIT method enqueue.
@Test
public void enqueue() throws Exception {
Request request = new Request.Builder().url(webServer.getCallHttpUrl()).build();
OkHttpClient client = new OkHttpClient();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Response> responseRef = new AtomicReference<>(null);
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
latch.countDown();
}
@Override
public void onResponse(Call call, Response response) {
responseRef.set(response);
latch.countDown();
}
});
latch.await(3, TimeUnit.SECONDS);
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.awaitTrace(event(ASYNC.getName(), "Asynchronous Invocation"), 20, 3000);
verifier.printCache();
Method realCallEnqueueMethod = Class.forName("okhttp3.RealCall").getDeclaredMethod("enqueue", Class.forName("okhttp3.Callback"));
verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), realCallEnqueueMethod));
Method dispatcherEnqueueMethod = Class.forName("okhttp3.Dispatcher").getDeclaredMethod("enqueue", Class.forName("okhttp3.RealCall$AsyncCall"));
verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), dispatcherEnqueueMethod));
verifier.verifyTrace(event(ASYNC.getName(), "Asynchronous Invocation"));
Method executeMethod = Class.forName("okhttp3.RealCall$AsyncCall").getDeclaredMethod("execute");
verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), executeMethod));
Response response = responseRef.get();
Method interceptMethod = Class.forName("okhttp3.internal.http.BridgeInterceptor").getDeclaredMethod("intercept", Class.forName("okhttp3.Interceptor$Chain"));
verifier.verifyTrace(event(OK_HTTP_CLIENT.getName(), interceptMethod, null, null, webServer.getHostAndPort(), annotation("http.url", request.url().toString()), annotation("http.status.code", response.code())));
String hostAndPort = HostAndPort.toHostAndPortString(request.url().host(), request.url().port());
Method connectMethod = getConnectMethod(Class.forName("okhttp3.internal.connection.RealConnection"));
verifier.verifyTrace(event(OK_HTTP_CLIENT_INTERNAL.getName(), connectMethod, annotation("http.internal.display", hostAndPort)));
verifier.verifyTraceCount(0);
}
use of okhttp3.Dispatcher in project MVPArms by JessYanCoding.
the class ClientModule method provideClient.
/**
* 提供 {@link OkHttpClient}
*
* @param application {@link Application}
* @param configuration {@link OkhttpConfiguration}
* @param builder {@link OkHttpClient.Builder}
* @param intercept {@link Interceptor}
* @param interceptors {@link List<Interceptor>}
* @param handler {@link GlobalHttpHandler}
* @param executorService {@link ExecutorService}
* @return {@link OkHttpClient}
*/
@Singleton
@Provides
static OkHttpClient provideClient(Application application, @Nullable OkhttpConfiguration configuration, OkHttpClient.Builder builder, Interceptor intercept, @Nullable List<Interceptor> interceptors, @Nullable GlobalHttpHandler handler, ExecutorService executorService) {
builder.connectTimeout(TIME_OUT, TimeUnit.SECONDS).readTimeout(TIME_OUT, TimeUnit.SECONDS).addNetworkInterceptor(intercept);
if (handler != null) {
builder.addInterceptor(chain -> chain.proceed(handler.onHttpRequestBefore(chain, chain.request())));
}
// 如果外部提供了 Interceptor 的集合则遍历添加
if (interceptors != null) {
for (Interceptor interceptor : interceptors) {
builder.addInterceptor(interceptor);
}
}
// 为 OkHttp 设置默认的线程池
builder.dispatcher(new Dispatcher(executorService));
if (configuration != null) {
configuration.configOkhttp(application, builder);
}
return builder.build();
}
Aggregations