Catch exception and log instead of failing

Some singleton clashes are inevitable if running multiple
apps i nteh same JVM, but we can at least try and not fall off
a cliff when they happen.
This commit is contained in:
Dave Syer
2015-02-03 10:38:57 +01:00
parent 4f39b64f26
commit 4afc44acaf

View File

@@ -22,6 +22,7 @@ import java.util.List;
import java.util.concurrent.TimeUnit;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.actuate.metrics.reader.MetricReader;
@@ -52,7 +53,15 @@ public class ServoMetricCollector implements DisposableBean {
BasicMetricFilter.MATCH_ALL, true, observers);
if (!PollScheduler.getInstance().isStarted()) {
PollScheduler.getInstance().start();
try {
PollScheduler.getInstance().start();
}
catch (Exception e) {
// Can fail due to race condition with evil singletons (if more than one
// app in same JVM)
log.error("Could not start servo metrics poller", e);
return;
}
}
// TODO Make poll interval configurable
PollScheduler.getInstance().addPoller(task, 5, TimeUnit.SECONDS);
@@ -62,7 +71,12 @@ public class ServoMetricCollector implements DisposableBean {
public void destroy() throws Exception {
log.info("Stopping Servo PollScheduler");
if (PollScheduler.getInstance().isStarted()) {
PollScheduler.getInstance().stop();
try {
PollScheduler.getInstance().stop();
}
catch (Exception e) {
log.error("Could not stop servo metrics poller", e);
}
}
}