1
1
package io .split .engine .sse .client ;
2
2
3
3
import com .google .common .base .Strings ;
4
+ import com .google .common .util .concurrent .ThreadFactoryBuilder ;
4
5
import org .apache .hc .client5 .http .classic .methods .HttpGet ;
5
6
import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
6
7
import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
15
16
import java .net .SocketException ;
16
17
import java .net .URI ;
17
18
import java .util .concurrent .CountDownLatch ;
19
+ import java .util .concurrent .ExecutorService ;
20
+ import java .util .concurrent .Executors ;
18
21
import java .util .concurrent .TimeUnit ;
19
22
import java .util .concurrent .atomic .AtomicReference ;
20
23
import java .util .function .Function ;
@@ -41,6 +44,10 @@ private enum ConnectionState {
41
44
private final static long CONNECT_TIMEOUT = 30000 ;
42
45
private static final Logger _log = LoggerFactory .getLogger (SSEClient .class );
43
46
47
+ private final ExecutorService _connectionExecutor = Executors .newSingleThreadExecutor (new ThreadFactoryBuilder ()
48
+ .setDaemon (true )
49
+ .setNameFormat ("SPLIT-SSEConnection-%d" )
50
+ .build ());
44
51
private final CloseableHttpClient _client ;
45
52
private final Function <RawEvent , Void > _eventCallback ;
46
53
private final Function <StatusMessage , Void > _statusCallback ;
@@ -65,13 +72,11 @@ public synchronized boolean open(URI uri) {
65
72
_statusCallback .apply (StatusMessage .INITIALIZATION_IN_PROGRESS );
66
73
67
74
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 ));
71
76
try {
72
77
if (!signal .await (CONNECT_TIMEOUT , TimeUnit .SECONDS )) {
73
78
return false ;
74
- };
79
+ }
75
80
} catch (InterruptedException e ) {
76
81
Thread .currentThread ().interrupt ();
77
82
_log .debug (e .getMessage ());
0 commit comments