监控内存队列

521 查看

queue的增强版

public class EnhancedQueue<E> extends LinkedBlockingQueue<E>{

    private Counter pendings;

    private Meter inQueueMeter;

    private Meter deQueueMeter;

    private String name;

    public EnhancedQueue(MetricRegistry metricRegistry,String name){
        this.name = name;
        this.pendings = metricRegistry.counter(MetricRegistry.name(EnhancedQueue.class + name, "pendings"));
        this.inQueueMeter = metricRegistry.meter(MetricRegistry.name(EnhancedQueue.class + name,"in-queue","tps"));
        this.deQueueMeter = metricRegistry.meter(MetricRegistry.name(EnhancedQueue.class + name,"de-queue","tps"));
    }

    @Override
    public E take() throws InterruptedException {
        E element = super.take();
        pendings.dec();
        deQueueMeter.mark();
        return element;
    }

    @Override
    public void put(E e) throws InterruptedException {
        super.put(e);
        pendings.inc();
        inQueueMeter.mark();
    }
}