com.addc.commons.statistcs.collector.StatisticsHolder Maven / Gradle / Ivy
package com.addc.commons.statistcs.collector;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.addc.commons.Mutex;
import com.addc.commons.jmx.MBeanServerHelper;
/**
* The StatisticsHolder supplies a singleton factory class for managing
* {@link Accumulator}s, {@link Counter}s and {@link Timer}s. It is used by the {@link StatisticsFactory}
* to a VM global uniqie singleton for storing stastics objects for all the classloaders in the VM
*/
final class StatisticsHolder {
private static final Logger LOGGER= LoggerFactory.getLogger(StatisticsHolder.class);
private static final StatisticsHolder INSTANCE= new StatisticsHolder();
/**
* Get the singleton
*
* @return the singleton
*/
static StatisticsHolder getInstance() {
return INSTANCE;
}
private Map counters;
private Map timers;
private Map accumulators;
private Mutex countersMutex;
private Mutex timersMutex;
private Mutex accumulatorsMutex;
/**
* Get a reference to a given {@link Counter} creating it if one with the
* given name does yet exist
*
* @param name
* The name of the counter
* @return The Counter
*/
Counter getCounter(String name) {
Counter counter= null;
synchronized (countersMutex) {
LOGGER.debug("Get counter {}", name);
counter= counters.get(name);
if (counter == null) {
counter= new Counter(name);
counters.put(name, counter);
MBeanServerHelper.getInstance().registerMBean(counter,
"addc:Type=Statistics,SubType=Counters,id=" + name);
}
}
return counter;
}
/**
* get a set of the available counters' names
*
* @return A set of the available counters' names
*/
Set getCounterKeys() {
Set keys;
synchronized (countersMutex) {
keys= new HashSet<>(counters.keySet());
}
return keys;
}
/**
* Get a reference to a given {@link Timer} creating it if one with the
* given name does yet exist
*
* @param name
* The name of the timer
* @return The Timer
*/
Timer getTimer(String name) {
Timer timer= null;
synchronized (countersMutex) {
LOGGER.debug("Get timer {}", name);
timer= timers.get(name);
if (timer == null) {
timer= new Timer(name);
timers.put(name, timer);
MBeanServerHelper.getInstance().registerMBean(timer, "addc:Type=Statistics,SubType=Timers,id=" + name);
}
}
return timer;
}
/**
* Get a set of the available timers' names
*
* @return A set of the available timers' names
*/
Set getTimerKeys() {
Set keys;
synchronized (timersMutex) {
keys= new HashSet<>(timers.keySet());
}
return keys;
}
/**
* Get a reference to a given {@link Accumulator} creating it if one with
* the given name does yet exist
*
* @param name
* The name of the accumulator
* @return The Accumulator
*/
Accumulator getAccumulator(String name) {
Accumulator accumulator= null;
synchronized (accumulatorsMutex) {
LOGGER.debug("Get accumulator {}", name);
accumulator= accumulators.get(name);
if (accumulator == null) {
accumulator= new Accumulator(name);
accumulators.put(name, accumulator);
MBeanServerHelper.getInstance().registerMBean(accumulator,
"addc:Type=Statistics,SubType=Accumulators,id=" + name);
}
}
return accumulator;
}
/**
* Get a set of the available accumulators' names
*
* @return A set of the available accumulators' names
*/
Set getAccumulatorKeys() {
Set keys;
synchronized (accumulatorsMutex) {
keys= new HashSet<>(accumulators.keySet());
}
return keys;
}
/**
* Clear the maps of {@link Accumulator}s, {@link Timer}s and
* {@link Counter}s
*/
void reset() {
LOGGER.info("Clear statistics bject and MBeans");
MBeanServerHelper mbsh= MBeanServerHelper.getInstance();
synchronized (countersMutex) {
for (Counter counter : counters.values()) {
mbsh.unregisterMBean(counter);
}
counters.clear();
}
synchronized (timersMutex) {
for (Timer timer : timers.values()) {
mbsh.unregisterMBean(timer);
}
timers.clear();
}
synchronized (accumulatorsMutex) {
for (Accumulator accumulator : accumulators.values()) {
mbsh.unregisterMBean(accumulator);
}
accumulators.clear();
}
}
private StatisticsHolder() {
counters= new ConcurrentHashMap<>();
timers= new ConcurrentHashMap<>();
accumulators= new ConcurrentHashMap<>();
countersMutex= new Mutex();
timersMutex= new Mutex();
accumulatorsMutex= new Mutex();
}
}