package demo;

import java.lang.management.ManagementFactory;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.codahale.metrics.*;
import com.codahale.metrics.jvm.BufferPoolMetricSet;
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
import com.codahale.metrics.jvm.MemoryUsageGaugeSet;
import com.codahale.metrics.jvm.ThreadStatesGaugeSet;

@Component
public class AppConfig implements InitializingBean {
	
	@Autowired
	MetricRegistry metricRegistry;

	//need separate registry for threads data as it does not mix well with regular spring metrics
	//probably down to name=threads.deadlocks, value=[]
	final MetricRegistry threadRegistry = new MetricRegistry();
	
	//jvm metrics setup from http://jansipke.nl/getting-metrics-from-the-java-virtual-machine-jvm/
	
	private void registerAll(String prefix, MetricSet metricSet, MetricRegistry registry) {
	    for (Entry<String, Metric> entry : metricSet.getMetrics().entrySet()) {
	        if (entry.getValue() instanceof MetricSet) {
	            registerAll(prefix + "." + entry.getKey(), (MetricSet) entry.getValue(), registry);
	        } else {
	            registry.register(prefix + "." + entry.getKey(), entry.getValue());
	        }
	    }
	}
	@Override
	public void afterPropertiesSet() throws Exception {
		registerAll("gc", new GarbageCollectorMetricSet(), metricRegistry);
		registerAll("buffers", new BufferPoolMetricSet(ManagementFactory.getPlatformMBeanServer()), metricRegistry);
		registerAll("memory", new MemoryUsageGaugeSet(), metricRegistry);
		registerAll("threads", new ThreadStatesGaugeSet(), threadRegistry);
		
		final Slf4jReporter lreporter = Slf4jReporter.forRegistry(metricRegistry)
				.outputTo(LoggerFactory.getLogger("demo"))
				.convertRatesTo(TimeUnit.SECONDS)
				.convertDurationsTo(TimeUnit.MILLISECONDS).build();
		lreporter.start(1, TimeUnit.MINUTES);
		
		final Slf4jReporter tlreporter = Slf4jReporter.forRegistry(threadRegistry)
				.outputTo(LoggerFactory.getLogger("demo"))
				.convertRatesTo(TimeUnit.SECONDS)
				.convertDurationsTo(TimeUnit.MILLISECONDS).build();
		tlreporter.start(1, TimeUnit.MINUTES);

		final ConsoleReporter creporter = ConsoleReporter
				.forRegistry(metricRegistry).convertRatesTo(TimeUnit.SECONDS)
				.convertDurationsTo(TimeUnit.MILLISECONDS).build();
		creporter.start(1, TimeUnit.MINUTES);
		
		final ConsoleReporter tcreporter = ConsoleReporter
				.forRegistry(threadRegistry).convertRatesTo(TimeUnit.SECONDS)
				.convertDurationsTo(TimeUnit.MILLISECONDS).build();
		tcreporter.start(1, TimeUnit.MINUTES);

	}

}
@@ -133,90 +133,71 @@
 ort 
-org.slf4j.LoggerFactory;%0Aimport org.springframework.beans.factory.InitializingBean
+javax.annotation.PostConstruct;%0A%0Aimport org.slf4j.LoggerFactory
 ;%0Aim
@@ -288,28 +288,40 @@
 ork.
-stereotype.Component
+context.annotation.Configuration
 ;%0A%0Ai
@@ -577,15 +577,19 @@
 %0A@Co
-mponent
+nfiguration
 %0Apub
@@ -612,36 +612,8 @@
 fig 
-implements InitializingBean 
 %7B%0A%09%0A
@@ -1409,16 +1409,21 @@
 %7D%0A%09@
-Override
+PostConstruct
 %0A%09pu

update for newer form of configuration