BAEL-4205 - A Guide to Events in OkHTTP (#10734)
* BAEL-4706 - Spring Boot with Spring Batch * BAEL-3948 - Fix test(s) in spring-batch which leaves repository.sqlite changed * BAEL-4736 - Convert JSONArray to List of Object using camel-jackson * BAEL-4756 - Mockito MockSettings * BAEL-4756 - Mockito MockSettings - fix spelling * BAEL-2674 - Upgrade the Okhttp article * BAEL-4204 - Adding Interceptors in OkHTTP * BAEL-4836 - Mocking Static Methods with Mockito * BAEL-4205 - A Guide to Events in OkHTTP Co-authored-by: Jonathan Cook <jcook@sciops.esa.int>
This commit is contained in:
parent
2763903467
commit
f09a24c8fc
|
@ -0,0 +1,156 @@
|
|||
package com.baeldung.okhttp.events;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Proxy;
|
||||
import java.util.List;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Connection;
|
||||
import okhttp3.EventListener;
|
||||
import okhttp3.Handshake;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.Protocol;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class EventTimer extends EventListener {
|
||||
|
||||
private long start;
|
||||
|
||||
private void logTimedEvent(String name) {
|
||||
long now = System.nanoTime();
|
||||
if (name.equals("callStart")) {
|
||||
start = now;
|
||||
}
|
||||
long elapsedNanos = now - start;
|
||||
System.out.printf("%.3f %s%n", elapsedNanos / 1000000000d, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void callStart(Call call) {
|
||||
logTimedEvent("callStart");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void proxySelectStart(Call call, HttpUrl url) {
|
||||
logTimedEvent("proxySelectStart");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void proxySelectEnd(Call call, HttpUrl url, List<Proxy> proxies) {
|
||||
logTimedEvent("proxySelectEnd");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dnsStart(Call call, String domainName) {
|
||||
logTimedEvent("dnsStart");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dnsEnd(Call call, String domainName, List<InetAddress> inetAddressList) {
|
||||
logTimedEvent("dnsEnd");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connectStart(Call call, InetSocketAddress inetSocketAddress, Proxy proxy) {
|
||||
logTimedEvent("connectStart");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void secureConnectStart(Call call) {
|
||||
logTimedEvent("secureConnectStart");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void secureConnectEnd(Call call, Handshake handshake) {
|
||||
logTimedEvent("secureConnectEnd");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connectEnd(Call call, InetSocketAddress inetSocketAddress, Proxy proxy, Protocol protocol) {
|
||||
logTimedEvent("connectEnd");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connectFailed(Call call, InetSocketAddress inetSocketAddress, Proxy proxy, Protocol protocol, IOException ioe) {
|
||||
logTimedEvent("connectFailed");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connectionAcquired(Call call, Connection connection) {
|
||||
logTimedEvent("connectionAcquired");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connectionReleased(Call call, Connection connection) {
|
||||
logTimedEvent("connectionReleased");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestHeadersStart(Call call) {
|
||||
logTimedEvent("requestHeadersStart");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestHeadersEnd(Call call, Request request) {
|
||||
logTimedEvent("requestHeadersEnd");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestBodyStart(Call call) {
|
||||
logTimedEvent("requestBodyStart");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestBodyEnd(Call call, long byteCount) {
|
||||
logTimedEvent("requestBodyEnd");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestFailed(Call call, IOException ioe) {
|
||||
logTimedEvent("requestFailed");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void responseHeadersStart(Call call) {
|
||||
logTimedEvent("responseHeadersStart");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void responseHeadersEnd(Call call, Response response) {
|
||||
logTimedEvent("responseHeadersEnd");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void responseBodyStart(Call call) {
|
||||
logTimedEvent("responseBodyStart");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void responseBodyEnd(Call call, long byteCount) {
|
||||
logTimedEvent("responseBodyEnd");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void responseFailed(Call call, IOException ioe) {
|
||||
logTimedEvent("responseFailed");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void callEnd(Call call) {
|
||||
logTimedEvent("callEnd");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void callFailed(Call call, IOException ioe) {
|
||||
logTimedEvent("callFailed");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void canceled(Call call) {
|
||||
logTimedEvent("canceled");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.okhttp.events;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.EventListener;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class SimpleLogEventsListener extends EventListener {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleLogEventsListener.class);
|
||||
|
||||
@Override
|
||||
public void callStart(Call call) {
|
||||
LOGGER.info("callStart at {}", LocalDateTime.now());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestHeadersEnd(Call call, Request request) {
|
||||
LOGGER.info("requestHeadersEnd at {} with headers {}", LocalDateTime.now(), request.headers());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void responseHeadersEnd(Call call, Response response) {
|
||||
LOGGER.info("responseHeadersEnd at {} with headers {}", LocalDateTime.now(), response.headers());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void callEnd(Call call) {
|
||||
LOGGER.info("callEnd at {}", LocalDateTime.now());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.okhttp.events;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class EventTimerLiveTest {
|
||||
|
||||
@Test
|
||||
public void givenSimpleEventTimer_whenRequestSent_thenCallsLogged() throws IOException {
|
||||
|
||||
OkHttpClient client = new OkHttpClient.Builder()
|
||||
.eventListener(new EventTimer())
|
||||
.build();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url("https://www.baeldung.com/")
|
||||
.build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
assertEquals("Response code should be: ", 200, response.code());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.baeldung.okhttp.events;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.SocketTimeoutException;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.mockwebserver.MockResponse;
|
||||
import okhttp3.mockwebserver.MockWebServer;
|
||||
|
||||
public class LogEventsListenerIntegrationTest {
|
||||
|
||||
@Rule
|
||||
public MockWebServer server = new MockWebServer();
|
||||
|
||||
@Test
|
||||
public void givenSimpleEventLogger_whenRequestSent_thenCallsLogged() throws IOException {
|
||||
server.enqueue(new MockResponse().setBody("Hello Baeldung Readers!"));
|
||||
|
||||
OkHttpClient client = new OkHttpClient.Builder()
|
||||
.eventListener(new SimpleLogEventsListener())
|
||||
.build();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
assertEquals("Response code should be: ", 200, response.code());
|
||||
assertEquals("Body should be: ", "Hello Baeldung Readers!", response.body().string());
|
||||
}
|
||||
}
|
||||
|
||||
@Test (expected = SocketTimeoutException.class)
|
||||
public void givenConnectionError_whenRequestSent_thenFailedCallsLogged() throws IOException {
|
||||
OkHttpClient client = new OkHttpClient.Builder()
|
||||
.eventListener(new EventTimer())
|
||||
.build();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.build();
|
||||
|
||||
client.newCall(request).execute();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue