@ -16,9 +16,13 @@
package org.springframework.boot.actuate.metrics.export ;
import java.io.Closeable ;
import java.io.IOException ;
import java.util.HashMap ;
import java.util.HashSet ;
import java.util.Map ;
import java.util.Map.Entry ;
import java.util.Set ;
import org.springframework.boot.actuate.metrics.reader.MetricReader ;
import org.springframework.boot.actuate.metrics.writer.GaugeWriter ;
@ -32,7 +36,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
* @author Dave Syer
* @since 1.3 .0
* /
public class MetricExporters implements SchedulingConfigurer {
public class MetricExporters implements SchedulingConfigurer , Closeable {
private MetricReader reader ;
@ -42,6 +46,8 @@ public class MetricExporters implements SchedulingConfigurer {
private final Map < String , Exporter > exporters = new HashMap < String , Exporter > ( ) ;
private final Set < String > closeables = new HashSet < String > ( ) ;
public MetricExporters ( MetricExportProperties properties ) {
this . properties = properties ;
}
@ -78,6 +84,7 @@ public class MetricExporters implements SchedulingConfigurer {
if ( trigger ! = null ) {
MetricCopyExporter exporter = getExporter ( writer , trigger ) ;
this . exporters . put ( name , exporter ) ;
this . closeables . add ( name ) ;
ExportRunner runner = new ExportRunner ( exporter ) ;
IntervalTask task = new IntervalTask ( runner , trigger . getDelayMillis ( ) ,
trigger . getDelayMillis ( ) ) ;
@ -99,6 +106,16 @@ public class MetricExporters implements SchedulingConfigurer {
return this . exporters ;
}
@Override
public void close ( ) throws IOException {
for ( String name : this . closeables ) {
Exporter exporter = this . exporters . get ( name ) ;
if ( exporter instanceof Closeable ) {
( ( Closeable ) exporter ) . close ( ) ;
}
}
}
private static class ExportRunner implements Runnable {
private final Exporter exporter ;