11package io .split .engine .sse .client ;
22
33import com .google .common .base .Strings ;
4+ import com .google .common .util .concurrent .ThreadFactoryBuilder ;
45import org .apache .hc .client5 .http .classic .methods .HttpGet ;
56import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
67import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
1516import java .net .SocketException ;
1617import java .net .URI ;
1718import java .util .concurrent .CountDownLatch ;
19+ import java .util .concurrent .ExecutorService ;
20+ import java .util .concurrent .Executors ;
1821import java .util .concurrent .TimeUnit ;
1922import java .util .concurrent .atomic .AtomicReference ;
2023import java .util .function .Function ;
@@ -41,6 +44,10 @@ private enum ConnectionState {
4144 private final static long CONNECT_TIMEOUT = 30000 ;
4245 private static final Logger _log = LoggerFactory .getLogger (SSEClient .class );
4346
47+ private final ExecutorService _connectionExecutor = Executors .newSingleThreadExecutor (new ThreadFactoryBuilder ()
48+ .setDaemon (true )
49+ .setNameFormat ("SPLIT-SSEConnection-%d" )
50+ .build ());
4451 private final CloseableHttpClient _client ;
4552 private final Function <RawEvent , Void > _eventCallback ;
4653 private final Function <StatusMessage , Void > _statusCallback ;
@@ -65,13 +72,11 @@ public synchronized boolean open(URI uri) {
6572 _statusCallback .apply (StatusMessage .INITIALIZATION_IN_PROGRESS );
6673
6774 CountDownLatch signal = new CountDownLatch (1 );
68- Thread thread = new Thread (() -> connectAndLoop (uri , signal ));
69- thread .setDaemon (true );
70- thread .start ();
75+ _connectionExecutor .submit (() -> connectAndLoop (uri , signal ));
7176 try {
7277 if (!signal .await (CONNECT_TIMEOUT , TimeUnit .SECONDS )) {
7378 return false ;
74- };
79+ }
7580 } catch (InterruptedException e ) {
7681 Thread .currentThread ().interrupt ();
7782 _log .debug (e .getMessage ());
0 commit comments