use of zipkin2.Callback in project okhttp by square.
the class AsynchronousGet method run.
public void run() throws Exception {
Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try (ResponseBody responseBody = response.body()) {
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(responseBody.string());
}
}
});
}
use of zipkin2.Callback in project materialistic by hidroh.
the class FileDownloader method downloadFile.
@WorkerThread
public void downloadFile(String url, String mimeType, FileDownloaderCallback callback) {
File outputFile = new File(mCacheDir, new File(url).getName());
if (outputFile.exists()) {
mMainHandler.post(() -> callback.onSuccess(outputFile.getPath()));
return;
}
final Request request = new Request.Builder().url(url).addHeader("Content-Type", mimeType).build();
mCallFactory.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
mMainHandler.post(() -> callback.onFailure(call, e));
}
@Override
public void onResponse(Call call, Response response) {
try {
BufferedSink sink = Okio.buffer(Okio.sink(outputFile));
sink.writeAll(response.body().source());
sink.close();
mMainHandler.post(() -> callback.onSuccess(outputFile.getPath()));
} catch (IOException e) {
this.onFailure(call, e);
}
}
});
}
use of zipkin2.Callback 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 zipkin2.Callback 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 zipkin2.Callback in project yyl_example by Relucent.
the class OkhttpTest2 method main.
public static void main(String[] args) throws IOException {
OkHttpClient client;
(client = //
new OkHttpClient.Builder().build()).newCall(//
new Request.Builder().url(//
"https://www.baidu.com/").header("Connection", // close | keep-alive
"close").get().build()).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println(response.body().string());
}
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
});
// 应用关闭时候需要关闭线程池
client.dispatcher().executorService().shutdown();
}
Aggregations