Skip to content

Commit cd9bf61

Browse files
Merge pull request #207 from stevejagodzinski/feature/flush-appender
Expose flush() method on Logback and Log4j appenders to flush underlying HttpEventCollectorSender and OkHttpClient
2 parents 0af11b5 + b08d564 commit cd9bf61

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

src/main/java/com/splunk/logging/HttpEventCollectorLog4jAppender.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ public void append(final LogEvent event)
245245
);
246246
}
247247

248+
public void flush() {
249+
sender.flush();
250+
}
251+
248252
@Override
249253
public boolean stop(long timeout, TimeUnit timeUnit) {
250254
this.sender.close();

src/main/java/com/splunk/logging/HttpEventCollectorLogbackAppender.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ public void start() {
119119
super.start();
120120
}
121121

122+
public void flush() {
123+
if (started) {
124+
sender.flush();
125+
}
126+
}
127+
122128
@Override
123129
public void stop() {
124130
if (!started)

src/main/java/com/splunk/logging/HttpEventCollectorSender.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)