Source:
public void run() { log.info("Starting writer"); try { running = true; while (!isCanceled()) { if (Thread.interrupted()) { throw new InterruptedException(); } TimerContext batchTimeContext = timePerBatch.time(); Collection<Metric> metrics = metricsQueue.poll(batchSize); if (metrics.isEmpty()) { if (sleepWhenEmpty > 0) { log.debug("No metrics to send; sleeping a bit."); Thread.sleep(sleepWhenEmpty); } continue; } OpenTsdbClient client = null; try { client = clientPool.borrowObject(); long processed = 0; for (Metric m : metrics) { String message = convert(m); client.put(message); processed++; } if (processed > 0) { metricsQueue.incrementProcessed(processed); } client.flush(); try { String response = client.read(); if (response != null) { log.warn("Error detected writing metrics: {}", response); metricsQueue.incrementError(); } } catch (SocketTimeoutException ste) { } catch (Exception e) { log.warn("Caught exception checking for errors", e); client.close(); } } catch (Exception e) { log.warn("Caught unexpected exception processing messages", e); metricsQueue.addAll(metrics, true); if (client != null) { client.close(); } } finally { if (client != null) { try { clientPool.returnObject(client); } catch(Exception returnException) { log.warn("Failed to return TSDB client to connection pool", returnException); } } client = null; batchTimeContext.stop(); } } } catch(InterruptedException ie) { log.info("Exiting due to thread interrupt"); } finally { running = false; } }

Target:
public void run() { log.info("Starting writer"); try { writerRegistry.register(this); running = true; while (!isCanceled()) { if (Thread.interrupted()) { throw new InterruptedException(); } TimerContext batchTimeContext = timePerBatch.time(); Collection<Metric> metrics = metricsQueue.poll(batchSize); if (metrics.isEmpty()) { if (System.currentTimeMillis() > lastWorkTime + maxIdleTime) { log.info("Stopping execution due to dearth of work"); break; } else { log.debug("No work; sleeping for milliseconds", sleepWhenEmpty); Thread.sleep(sleepWhenEmpty); continue; } } OpenTsdbClient client = null; try { client = clientPool.borrowObject(); long processed = 0; for (Metric m : metrics) { String message = convert(m); client.put(message); processed++; } if (processed > 0) { metricsQueue.incrementProcessed(processed); } client.flush(); try { String response = client.read(); if (response != null) { log.warn("Error detected writing metrics: {}", response); metricsQueue.incrementError(); } } catch (SocketTimeoutException ste) { } catch (Exception e) { log.warn("Caught exception checking for errors", e); client.close(); } } catch (Exception e) { log.warn("Caught unexpected exception processing messages", e); metricsQueue.addAll(metrics, true); if (client != null) { client.close(); } } finally { if (client != null) { try { clientPool.returnObject(client); } catch(Exception returnException) { log.warn("Failed to return TSDB client to connection pool", returnException); } } client = null; batchTimeContext.stop(); lastWorkTime = System.currentTimeMillis(); } } } catch(InterruptedException ie) { log.info("Exiting due to thread interrupt"); Thread.currentThread().interrupt(); } finally { running = false; writerRegistry.unregister(this); } }