Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2012 Ness Computing, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.nesscomputing.concurrent;
 
 
 import  com.yammer.metrics.core.Meter;
 import  com.yammer.metrics.core.MetricsRegistry;
 import  com.yammer.metrics.core.Timer;
 
 class TimerWrapper extends CallableWrapper
 {
     private final String threadPoolName;
     private Metrics metrics;
 
     public TimerWrapper(String threadPoolName)
     {
         this. = threadPoolName;
     }
 
     TimerWrapper setMetricsRegistry(MetricsRegistry registry)
     {
         this. = new Metrics(registry);
         return this;
     }
 
     @Override
     public <T> Callable<T> wrap(final Callable<T> callable)
     {
         final long enqueueNanos = System.nanoTime();
         final Metrics myMetrics = ;
         if (myMetrics == null) {
             return callable;
         }
 
         myMetrics.enqueueMeter.mark();
 
         return new Callable<T>() {
             @Override
             public T call() throws Exception
             {
                 myMetrics.queueTimer.update(System.nanoTime() - enqueueNanos.);
                 myMetrics.dequeueMeter.mark();
                 try {
                     return callable.call();
                 } catch (Throwable t) {
                     myMetrics.exceptionMeter.mark();
                     throw t;
                 } finally {
                     myMetrics.totalTimer.update(System.nanoTime() - enqueueNanos.);
                 }
             }
         };
     }
 
     static class Metrics
     {
         Meter exceptionMeter;
         Meter enqueueMeter;
         Meter dequeueMeter;
         Timer queueTimer;
         Timer totalTimer;
 
         public Metrics(String threadPoolName, MetricsRegistry registry)
         {
             String baseName = threadPoolName + ".";
              = registry.newMeter(TimedExecutors.classbaseName + "exception""exception".);
              = registry.newMeter(TimedExecutors.classbaseName + "enqueue""enqueue".);
              = registry.newMeter(TimedExecutors.classbaseName + "dequeue""dequeue".);
              = registry.newTimer(TimedExecutors.classbaseName + "queued-duration");
              = registry.newTimer(TimedExecutors.classbaseName + "total-duration");
         }
     }
 }
 
 {
     private MetricsRegistry registry;
     private final String threadPoolName;
 
     public TimerWrapperProvider(String threadPoolName)
     {
         this. = threadPoolName;
    }
    @Inject(optional=true)
    void setMetricsRegistry(MetricsRegistry registry)
    {
        this. = registry;
    }
    @Override
    public TimerWrapper get()
    {
        TimerWrapper wrapper = new TimerWrapper();
        if ( != null) {
            wrapper = wrapper.setMetricsRegistry();
        }
        return wrapper;
    }
New to GrepCode? Check out our FAQ X