Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.codahale.metrics;
  
  import java.io.Closeable;
  import java.util.Locale;
The abstract base class for all scheduled reporters (i.e., reporters which process a registry's metrics periodically).

 
 public abstract class ScheduledReporter implements Closeable {
    
A simple named thread factory.
 
     @SuppressWarnings("NullableProblems")
     private static class NamedThreadFactory implements ThreadFactory {
         private final ThreadGroup group;
         private final AtomicInteger threadNumber = new AtomicInteger(1);
         private final String namePrefix;
 
         private NamedThreadFactory(String name) {
             final SecurityManager s = System.getSecurityManager();
             this. = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
             this. = "metrics-" + name + "-thread-";
         }
 
         @Override
         public Thread newThread(Runnable r) {
             final Thread t = new Thread(r + .getAndIncrement(), 0);
             t.setDaemon(true);
             if (t.getPriority() != .) {
                 t.setPriority(.);
             }
             return t;
         }
     }
 
     private final MetricRegistry registry;
     private final ScheduledExecutorService executor;
     private final MetricFilter filter;
     private final double durationFactor;
     private final String durationUnit;
     private final double rateFactor;
     private final String rateUnit;

    
Creates a new ScheduledReporter instance.

Parameters:
registry the MetricRegistry containing the metrics this reporter will report
name the reporter's name
filter the filter for which metrics to report
 
     protected ScheduledReporter(MetricRegistry registry,
                                 String name,
                                 MetricFilter filter,
                                 TimeUnit rateUnit,
                                 TimeUnit durationUnit) {
         this. = registry;
         this. = filter;
         this. = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory(name));
         this. = rateUnit.toSeconds(1);
         this. = calculateRateUnit(rateUnit);
         this. = 1.0 / durationUnit.toNanos(1);
         this. = durationUnit.toString().toLowerCase(.);
     }

    
Starts the reporter polling at the given period.

Parameters:
period the amount of time between polls
unit the unit for period
 
     public void start(long periodTimeUnit unit) {
         .scheduleAtFixedRate(new Runnable() {
             @Override
             public void run() {
                 report();
             }
         }, periodperiodunit);
     }

    
Stops the reporter and shuts down its thread of execution.
 
     public void stop() {
         .shutdown();
         try {
             .awaitTermination(1, .);
         } catch (InterruptedException ignored) {
            // do nothing
        }
    }

    
Stops the reporter and shuts down its thread of execution.
    @Override
    public void close() {
        stop();
    }

    
Report the current values of all metrics in the registry.
    public void report() {
               .getCounters(),
               .getHistograms(),
               .getMeters(),
               .getTimers());
    }

    
Called periodically by the polling thread. Subclasses should report all the given metrics.

Parameters:
gauges all of the gauges in the registry
counters all of the counters in the registry
histograms all of the histograms in the registry
meters all of the meters in the registry
timers all of the timers in the registry
    public abstract void report(SortedMap<StringGaugegauges,
                                SortedMap<StringCountercounters,
                                SortedMap<StringHistogramhistograms,
                                SortedMap<StringMetermeters,
                                SortedMap<StringTimertimers);
    protected String getRateUnit() {
        return ;
    }
    protected String getDurationUnit() {
        return ;
    }
    protected double convertDuration(double duration) {
        return duration * ;
    }
    protected double convertRate(double rate) {
        return rate * ;
    }
    private String calculateRateUnit(TimeUnit unit) {
        final String s = unit.toString().toLowerCase(.);
        return s.substring(0, s.length() - 1);
    }
New to GrepCode? Check out our FAQ X