Skip to content

Commit f0bb90d

Browse files
authored
Add check in ScheduledReporter's stop method (#4246)
Closes #4135
1 parent 80a0e2d commit f0bb90d

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

metrics-core/src/main/java/com/codahale/metrics/ScheduledReporter.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,13 @@ public void stop() {
220220
executor.shutdown(); // Disable new tasks from being submitted
221221
}
222222

223-
try {
224-
report(); // Report metrics one last time
225-
} catch (Exception e) {
226-
LOG.warn("Final reporting of metrics failed.", e);
223+
if (this.scheduledFuture != null) {
224+
// Reporter started, try to report metrics one last time
225+
try {
226+
report();
227+
} catch (Exception e) {
228+
LOG.warn("Final reporting of metrics failed.", e);
229+
}
227230
}
228231

229232
if (shutdownExecutorOnStop) {

metrics-graphite/src/test/java/com/codahale/metrics/graphite/GraphiteReporterTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ public void closesConnectionIfGraphiteIsUnavailable() throws Exception {
445445

446446
@Test
447447
public void closesConnectionOnReporterStop() throws Exception {
448+
reporter.start(1, TimeUnit.SECONDS);
448449
reporter.stop();
449450

450451
final InOrder inOrder = inOrder(graphite);

0 commit comments

Comments
 (0)