@@ -196,9 +196,18 @@ public synchronized void send(final String message) {
196196 }
197197
198198 /**
199- * Flush all pending events
199+ * Flush all pending events to the underlying HTTP client
200+ * and then flush the HTTP client itself (keeping the client
201+ * open to accept further events)
200202 */
201203 public synchronized void flush () {
204+ flush (false );
205+ }
206+
207+ /**
208+ * Flush all pending events to the underlying HTTP client
209+ */
210+ private synchronized void flushEvents () {
202211 if (eventsBatch .size () > 0 ) {
203212 postEventsAsync (eventsBatch );
204213 }
@@ -210,9 +219,11 @@ public synchronized void flush() {
210219 }
211220
212221 public synchronized void flush (boolean close ) {
213- flush ();
222+ flushEvents ();
214223 if (close ) {
215224 stopHttpClient ();
225+ } else {
226+ flushHttpClient ();
216227 }
217228 }
218229
@@ -222,8 +233,7 @@ public synchronized void flush(boolean close) {
222233 void close () {
223234 if (timer != null )
224235 timer .cancel ();
225- flush ();
226- stopHttpClient ();
236+ flush (true );
227237 super .cancel ();
228238 }
229239
@@ -232,7 +242,7 @@ void close() {
232242 */
233243 @ Override // TimerTask
234244 public void run () {
235- flush ();
245+ flushEvents ();
236246 }
237247
238248 /**
@@ -261,6 +271,28 @@ public static void putIfPresent(JsonObject collection, String tag, Object value)
261271 }
262272 }
263273
274+ private void flushHttpClient () {
275+ flushHttpClient (timeoutSettings .terminationTimeout );
276+ }
277+
278+ private void flushHttpClient (long timeout ) {
279+ if (httpClient != null && timeout > 0 ) {
280+ Dispatcher dispatcher = httpClient .dispatcher ();
281+
282+ long start = System .currentTimeMillis ();
283+
284+ while (dispatcher .queuedCallsCount () > 0 &&
285+ dispatcher .runningCallsCount () > 0 &&
286+ start + timeout > System .currentTimeMillis ()) {
287+ try {
288+ TimeUnit .MILLISECONDS .sleep (30 );
289+ } catch (InterruptedException e ) {
290+ Thread .currentThread ().interrupt ();
291+ break ;
292+ }
293+ }
294+ }
295+ }
264296
265297 private void stopHttpClient () {
266298 if (httpClient != null ) {
0 commit comments