diff --git a/build.gradle b/build.gradle index 75b2d73..08f4f17 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,6 @@ allprojects { dependencies { compile "com.squareup.okhttp3:okhttp:3.8.1" compile "org.slf4j:slf4j-api:1.7.22" - compile "com.google.guava:guava:19.0" testRuntime "ch.qos.logback:logback-classic:1.1.9" testCompile "org.mockito:mockito-core:1.10.19" testCompile "junit:junit:4.11" diff --git a/src/main/java/com/launchdarkly/eventsource/EventSource.java b/src/main/java/com/launchdarkly/eventsource/EventSource.java index 0ecc98d..587f030 100644 --- a/src/main/java/com/launchdarkly/eventsource/EventSource.java +++ b/src/main/java/com/launchdarkly/eventsource/EventSource.java @@ -1,6 +1,5 @@ package com.launchdarkly.eventsource; -import com.google.common.util.concurrent.ThreadFactoryBuilder; import okhttp3.*; import okio.BufferedSource; import okio.Okio; @@ -21,12 +20,15 @@ import java.security.KeyStore; import java.util.Arrays; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Random; import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import static com.launchdarkly.eventsource.ReadyState.*; +import static java.lang.String.format; /** * Client for Server-Sent Events @@ -60,19 +62,29 @@ public class EventSource implements ConnectionHandler, Closeable { this.uri = builder.uri; this.headers = addDefaultHeaders(builder.headers); this.reconnectTimeMs = builder.reconnectTimeMs; - ThreadFactory eventsThreadFactory = new ThreadFactoryBuilder() - .setNameFormat("okhttp-eventsource-events-[" + name + "]-%d") - .build(); + ThreadFactory eventsThreadFactory = createThreadFactory("okhttp-eventsource-events"); this.eventExecutor = Executors.newSingleThreadExecutor(eventsThreadFactory); - ThreadFactory streamThreadFactory = new ThreadFactoryBuilder() - .setNameFormat("okhttp-eventsource-stream-[" + name + "]-%d") - .build(); + ThreadFactory streamThreadFactory = createThreadFactory("okhttp-eventsource-stream"); this.streamExecutor = Executors.newSingleThreadExecutor(streamThreadFactory); this.handler = new AsyncEventHandler(this.eventExecutor, builder.handler); this.readyState = new AtomicReference<>(RAW); this.client = builder.clientBuilder.build(); } + private ThreadFactory createThreadFactory(final String type) { + final ThreadFactory backingThreadFactory = + Executors.defaultThreadFactory(); + final AtomicLong count = new AtomicLong(0); + return new ThreadFactory() { + @Override + public Thread newThread(Runnable runnable) { + Thread thread = backingThreadFactory.newThread(runnable); + thread.setName(format(Locale.ROOT, "%s-[%s]-%d", type, name, count.getAndIncrement())); + return thread; + } + }; + } + public void start() { if (!readyState.compareAndSet(RAW, CONNECTING)) { logger.info("Start method called on this already-started EventSource object. Doing nothing"); diff --git a/src/main/java/com/launchdarkly/eventsource/ModernTLSSocketFactory.java b/src/main/java/com/launchdarkly/eventsource/ModernTLSSocketFactory.java index 9c9509f..7fc0bb8 100644 --- a/src/main/java/com/launchdarkly/eventsource/ModernTLSSocketFactory.java +++ b/src/main/java/com/launchdarkly/eventsource/ModernTLSSocketFactory.java @@ -1,7 +1,5 @@ package com.launchdarkly.eventsource; -import com.google.common.annotations.VisibleForTesting; - import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; @@ -74,7 +72,6 @@ public Socket createSocket(InetAddress inetAddress, int i, InetAddress inetAddre * @param s the socket * @return */ - @VisibleForTesting static Socket setModernTlsVersionsOnSocket(Socket s) { if (s != null && (s instanceof SSLSocket)) { List defaultEnabledProtocols = Arrays.asList(((SSLSocket) s).getSupportedProtocols());