Package com.cloudhopper.mq.queue.impl

Source Code of com.cloudhopper.mq.queue.impl.AverageQueueSize

package com.cloudhopper.mq.queue.impl;

/*
* #%L
* ch-mq
* %%
* Copyright (C) 2009 - 2013 Cloudhopper by Twitter
* %%
* 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.
* #L%
*/

import com.cloudhopper.mq.queue.Queue;
import com.codahale.metrics.ExponentiallyDecayingReservoir;
import com.codahale.metrics.Reservoir;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.WeakHashMap;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A periodic probe that measures average queue size. Currently runs every 15 seconds.
* @author garth
*/
public class AverageQueueSize implements Runnable {
    private static final Logger logger = LoggerFactory.getLogger(AverageQueueSize.class);

    private static final long PROBE_EACH = 15000L; //15 seconds

    private static AverageQueueSize instance;
    static {
  instance = new AverageQueueSize();
    }
    public static AverageQueueSize get() {
  return instance;
    }

    private AverageQueueSize() {
  this.executor = new ScheduledThreadPoolExecutor(1);
  this.executor.scheduleAtFixedRate(this, 0L, PROBE_EACH, TimeUnit.MILLISECONDS);
  this.queues = Collections.synchronizedMap(new WeakHashMap<Queue,Reservoir>());
    }

    private final ScheduledThreadPoolExecutor executor;
    private final Map<Queue,Reservoir> queues;

    public void add(Queue queue) {
  queues.put(queue, new ExponentiallyDecayingReservoir());
    }

    public void addReservoir(Queue queue, Reservoir reservoir) {
  queues.put(queue, reservoir);
    }

    public double getMean(Queue queue) {
  return queues.get(queue).getSnapshot().getMean();
    }

    public double getMedian(Queue queue) {
  return queues.get(queue).getSnapshot().getMedian();
    }

    public void run() {
  try {
      synchronized(queues) {
    for (Map.Entry<Queue,Reservoir> entry:queues.entrySet()) {
        entry.getValue().update(entry.getKey().getSize());
    }
      }
  } catch (ConcurrentModificationException ce) {
      logger.warn("Average queue size map altered during probe.", ce);
  }
    }

}
TOP

Related Classes of com.cloudhopper.mq.queue.impl.AverageQueueSize

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.