Skip to content

Commit b926a03

Browse files
committed
Add samples for EventListener
1 parent dcabf48 commit b926a03

File tree

2 files changed

+341
-0
lines changed

2 files changed

+341
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
* Copyright (C) 2017 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package okhttp3.recipes;
17+
18+
import java.io.IOException;
19+
import java.net.InetAddress;
20+
import java.net.InetSocketAddress;
21+
import java.net.Proxy;
22+
import java.util.List;
23+
import java.util.concurrent.atomic.AtomicLong;
24+
import okhttp3.Call;
25+
import okhttp3.Callback;
26+
import okhttp3.Connection;
27+
import okhttp3.EventListener;
28+
import okhttp3.Handshake;
29+
import okhttp3.OkHttpClient;
30+
import okhttp3.Protocol;
31+
import okhttp3.Request;
32+
import okhttp3.Response;
33+
import okhttp3.ResponseBody;
34+
35+
public final class PrintEvents {
36+
private final OkHttpClient client = new OkHttpClient.Builder()
37+
.eventListenerFactory(PrintingEventListener.FACTORY)
38+
.build();
39+
40+
public void run() throws Exception {
41+
Request washingtonPostRequest = new Request.Builder()
42+
.url("https://www.washingtonpost.com/")
43+
.build();
44+
client.newCall(washingtonPostRequest).enqueue(new Callback() {
45+
@Override public void onFailure(Call call, IOException e) {
46+
}
47+
48+
@Override public void onResponse(Call call, Response response) throws IOException {
49+
try (ResponseBody body = response.body()) {
50+
// Consume and discard the response body.
51+
body.source().readByteString();
52+
}
53+
}
54+
});
55+
56+
Request newYorkTimesRequest = new Request.Builder()
57+
.url("https://www.nytimes.com/")
58+
.build();
59+
client.newCall(newYorkTimesRequest).enqueue(new Callback() {
60+
@Override public void onFailure(Call call, IOException e) {
61+
}
62+
63+
@Override public void onResponse(Call call, Response response) throws IOException {
64+
try (ResponseBody body = response.body()) {
65+
// Consume and discard the response body.
66+
body.source().readByteString();
67+
}
68+
}
69+
});
70+
}
71+
72+
public static void main(String... args) throws Exception {
73+
new PrintEvents().run();
74+
}
75+
76+
private static final class PrintingEventListener extends EventListener {
77+
private static final Factory FACTORY = new Factory() {
78+
final AtomicLong nextCallId = new AtomicLong(1L);
79+
80+
@Override public EventListener create(Call call) {
81+
long callId = nextCallId.getAndIncrement();
82+
System.out.printf("%04d %s%n", callId, call.request().url());
83+
return new PrintingEventListener(callId, System.nanoTime());
84+
}
85+
};
86+
87+
final long callId;
88+
final long callStartNanos;
89+
90+
public PrintingEventListener(long callId, long callStartNanos) {
91+
this.callId = callId;
92+
this.callStartNanos = callStartNanos;
93+
}
94+
95+
private void printEvent(String name) {
96+
long elapsedNanos = System.nanoTime() - callStartNanos;
97+
System.out.printf("%04d %.3f %s%n", callId, elapsedNanos / 1000000000d, name);
98+
}
99+
100+
@Override public void callStart(Call call) {
101+
printEvent("callStart");
102+
}
103+
104+
@Override public void dnsStart(Call call, String domainName) {
105+
printEvent("dnsStart");
106+
}
107+
108+
@Override public void dnsEnd(Call call, String domainName, List<InetAddress> inetAddressList) {
109+
printEvent("dnsEnd");
110+
}
111+
112+
@Override public void connectStart(
113+
Call call, InetSocketAddress inetSocketAddress, Proxy proxy) {
114+
printEvent("connectStart");
115+
}
116+
117+
@Override public void secureConnectStart(Call call) {
118+
printEvent("secureConnectStart");
119+
}
120+
121+
@Override public void secureConnectEnd(Call call, Handshake handshake) {
122+
printEvent("secureConnectEnd");
123+
}
124+
125+
@Override public void connectEnd(
126+
Call call, InetSocketAddress inetSocketAddress, Proxy proxy, Protocol protocol) {
127+
printEvent("connectEnd");
128+
}
129+
130+
@Override public void connectFailed(Call call, InetSocketAddress inetSocketAddress, Proxy proxy,
131+
Protocol protocol, IOException ioe) {
132+
printEvent("connectFailed");
133+
}
134+
135+
@Override public void connectionAcquired(Call call, Connection connection) {
136+
printEvent("connectionAcquired");
137+
}
138+
139+
@Override public void connectionReleased(Call call, Connection connection) {
140+
printEvent("connectionReleased");
141+
}
142+
143+
@Override public void requestHeadersStart(Call call) {
144+
printEvent("requestHeadersStart");
145+
}
146+
147+
@Override public void requestHeadersEnd(Call call, Request request) {
148+
printEvent("requestHeadersEnd");
149+
}
150+
151+
@Override public void requestBodyStart(Call call) {
152+
printEvent("requestBodyStart");
153+
}
154+
155+
@Override public void requestBodyEnd(Call call, long byteCount) {
156+
printEvent("requestBodyEnd");
157+
}
158+
159+
@Override public void responseHeadersStart(Call call) {
160+
printEvent("responseHeadersStart");
161+
}
162+
163+
@Override public void responseHeadersEnd(Call call, Response response) {
164+
printEvent("responseHeadersEnd");
165+
}
166+
167+
@Override public void responseBodyStart(Call call) {
168+
printEvent("responseBodyStart");
169+
}
170+
171+
@Override public void responseBodyEnd(Call call, long byteCount) {
172+
printEvent("responseBodyEnd");
173+
}
174+
175+
@Override public void callEnd(Call call) {
176+
printEvent("callEnd");
177+
}
178+
179+
@Override public void callFailed(Call call, IOException ioe) {
180+
printEvent("callFailed");
181+
}
182+
}
183+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright (C) 2017 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package okhttp3.recipes;
17+
18+
import java.io.IOException;
19+
import java.net.InetAddress;
20+
import java.net.InetSocketAddress;
21+
import java.net.Proxy;
22+
import java.util.List;
23+
import okhttp3.Call;
24+
import okhttp3.Connection;
25+
import okhttp3.EventListener;
26+
import okhttp3.Handshake;
27+
import okhttp3.OkHttpClient;
28+
import okhttp3.Protocol;
29+
import okhttp3.Request;
30+
import okhttp3.Response;
31+
32+
/**
33+
* This prints events for a single in-flight call. It won't work for multiple concurrent calls
34+
* because we don't know what callStartNanos refers to.
35+
*/
36+
public final class PrintEventsNonConcurrent {
37+
private final OkHttpClient client = new OkHttpClient.Builder()
38+
.eventListener(new PrintingEventListener())
39+
.build();
40+
41+
public void run() throws Exception {
42+
Request request = new Request.Builder()
43+
.url("https://publicobject.com/helloworld.txt")
44+
.build();
45+
46+
System.out.println("REQUEST 1 (new connection)");
47+
try (Response response = client.newCall(request).execute()) {
48+
// Consume and discard the response body.
49+
response.body().source().readByteString();
50+
}
51+
52+
System.out.println("REQUEST 2 (pooled connection)");
53+
try (Response response = client.newCall(request).execute()) {
54+
// Consume and discard the response body.
55+
response.body().source().readByteString();
56+
}
57+
}
58+
59+
public static void main(String... args) throws Exception {
60+
new PrintEventsNonConcurrent().run();
61+
}
62+
63+
private static final class PrintingEventListener extends EventListener {
64+
long callStartNanos;
65+
66+
private void printEvent(String name) {
67+
long nowNanos = System.nanoTime();
68+
if (name.equals("callStart")) {
69+
callStartNanos = nowNanos;
70+
}
71+
long elapsedNanos = nowNanos - callStartNanos;
72+
System.out.printf("%.3f %s%n", elapsedNanos / 1000000000d, name);
73+
}
74+
75+
@Override public void callStart(Call call) {
76+
printEvent("callStart");
77+
}
78+
79+
@Override public void dnsStart(Call call, String domainName) {
80+
printEvent("dnsStart");
81+
}
82+
83+
@Override public void dnsEnd(Call call, String domainName, List<InetAddress> inetAddressList) {
84+
printEvent("dnsEnd");
85+
}
86+
87+
@Override public void connectStart(
88+
Call call, InetSocketAddress inetSocketAddress, Proxy proxy) {
89+
printEvent("connectStart");
90+
}
91+
92+
@Override public void secureConnectStart(Call call) {
93+
printEvent("secureConnectStart");
94+
}
95+
96+
@Override public void secureConnectEnd(Call call, Handshake handshake) {
97+
printEvent("secureConnectEnd");
98+
}
99+
100+
@Override public void connectEnd(
101+
Call call, InetSocketAddress inetSocketAddress, Proxy proxy, Protocol protocol) {
102+
printEvent("connectEnd");
103+
}
104+
105+
@Override public void connectFailed(Call call, InetSocketAddress inetSocketAddress, Proxy proxy,
106+
Protocol protocol, IOException ioe) {
107+
printEvent("connectFailed");
108+
}
109+
110+
@Override public void connectionAcquired(Call call, Connection connection) {
111+
printEvent("connectionAcquired");
112+
}
113+
114+
@Override public void connectionReleased(Call call, Connection connection) {
115+
printEvent("connectionReleased");
116+
}
117+
118+
@Override public void requestHeadersStart(Call call) {
119+
printEvent("requestHeadersStart");
120+
}
121+
122+
@Override public void requestHeadersEnd(Call call, Request request) {
123+
printEvent("requestHeadersEnd");
124+
}
125+
126+
@Override public void requestBodyStart(Call call) {
127+
printEvent("requestBodyStart");
128+
}
129+
130+
@Override public void requestBodyEnd(Call call, long byteCount) {
131+
printEvent("requestBodyEnd");
132+
}
133+
134+
@Override public void responseHeadersStart(Call call) {
135+
printEvent("responseHeadersStart");
136+
}
137+
138+
@Override public void responseHeadersEnd(Call call, Response response) {
139+
printEvent("responseHeadersEnd");
140+
}
141+
142+
@Override public void responseBodyStart(Call call) {
143+
printEvent("responseBodyStart");
144+
}
145+
146+
@Override public void responseBodyEnd(Call call, long byteCount) {
147+
printEvent("responseBodyEnd");
148+
}
149+
150+
@Override public void callEnd(Call call) {
151+
printEvent("callEnd");
152+
}
153+
154+
@Override public void callFailed(Call call, IOException ioe) {
155+
printEvent("callFailed");
156+
}
157+
}
158+
}

0 commit comments

Comments
 (0)