|
2 | 2 |
|
3 | 3 | import com.google.common.base.Strings; |
4 | 4 | import org.apache.hc.client5.http.classic.methods.HttpGet; |
5 | | -import org.apache.hc.client5.http.config.RequestConfig; |
6 | 5 | import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
7 | 6 | import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; |
8 | | -import org.apache.hc.client5.http.impl.classic.HttpClients; |
9 | | -import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; |
10 | | -import org.apache.hc.core5.util.Timeout; |
11 | 7 | import org.slf4j.Logger; |
12 | 8 | import org.slf4j.LoggerFactory; |
13 | 9 |
|
@@ -50,6 +46,7 @@ private enum ConnectionState { |
50 | 46 | private final Function<StatusMessage, Void> _statusCallback; |
51 | 47 | private final AtomicReference<ConnectionState> _state = new AtomicReference<>(ConnectionState.CLOSED); |
52 | 48 | private final AtomicReference<CloseableHttpResponse> _ongoingResponse = new AtomicReference<>(); |
| 49 | + private final AtomicReference<HttpGet> _ongoingRequest = new AtomicReference<>(); |
53 | 50 |
|
54 | 51 | public SSEClient(Function<RawEvent, Void> eventCallback, |
55 | 52 | Function<StatusMessage, Void> statusCallback, |
@@ -91,6 +88,7 @@ public synchronized void close() { |
91 | 88 | if (_state.compareAndSet(ConnectionState.OPEN, ConnectionState.CLOSED)) { |
92 | 89 | if (_ongoingResponse.get() != null) { |
93 | 90 | try { |
| 91 | + _ongoingRequest.get().abort(); |
94 | 92 | _ongoingResponse.get().close(); |
95 | 93 | } catch (IOException e) { |
96 | 94 | _log.debug(String.format("Error closing SSEClient: %s", e.getMessage())); |
@@ -145,10 +143,11 @@ private void connectAndLoop(URI uri, CountDownLatch signal) { |
145 | 143 | } |
146 | 144 |
|
147 | 145 | private boolean establishConnection(URI uri, CountDownLatch signal) { |
148 | | - HttpGet request = new HttpGet(uri); |
| 146 | + |
| 147 | + _ongoingRequest.set(new HttpGet(uri)); |
149 | 148 |
|
150 | 149 | try { |
151 | | - _ongoingResponse.set(_client.execute(request)); |
| 150 | + _ongoingResponse.set(_client.execute(_ongoingRequest.get())); |
152 | 151 | if (_ongoingResponse.get().getCode() != 200) { |
153 | 152 | return false; |
154 | 153 | } |
|
0 commit comments