Skip to content

Commit

Permalink
as per @itaiy suggested will close then try to connect. (apache#3669)
Browse files Browse the repository at this point in the history
* as per @itaiy suggested will close then try to connect.

* use close instead of flush

* git fix comments

* break the loop in case of interrupted
  • Loading branch information
b-slim authored and nishantmonu51 committed Dec 13, 2016
1 parent 469ab21 commit 7b18fb7
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,14 @@ public void emit(Event event)

private class ConsumerRunnable implements Runnable
{
private PickledGraphite pickledGraphite = new PickledGraphite(
graphiteEmitterConfig.getHostname(),
graphiteEmitterConfig.getPort(),
graphiteEmitterConfig.getBatchSize()
);

@Override
public void run()
{
try {
try (PickledGraphite pickledGraphite = new PickledGraphite(
graphiteEmitterConfig.getHostname(),
graphiteEmitterConfig.getPort(),
graphiteEmitterConfig.getBatchSize()
)) {
if (!pickledGraphite.isConnected()) {
log.info("trying to connect to graphite server");
pickledGraphite.connect();
Expand Down Expand Up @@ -174,12 +172,16 @@ public void run()
log.error(e, e.getMessage());
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
break;
} else if (e instanceof SocketException) {
// This is antagonistic to general Closeable contract in Java,
// it is needed to allow re-connection in case of the socket is closed due long period of inactivity
pickledGraphite.close();
log.warn("Trying to re-connect to graphite server");
pickledGraphite.connect();
}
}
}
pickledGraphite.flush();
}
catch (Exception e) {
log.error(e, e.getMessage());
Expand Down

0 comments on commit 7b18fb7

Please sign in to comment.