public class CallQueue implements BlockingQueue<Runnable> {

  private static Log LOG = LogFactory.getLog(CallQueue.class);



  private final BlockingQueue<Call> underlyingQueue;

  private final ThriftMetrics metrics;



  public CallQueue(BlockingQueue<Call> underlyingQueue,

                   ThriftMetrics metrics) {

    this.underlyingQueue = underlyingQueue;

    this.metrics = metrics;

  }



  private static long now() {

    return System.nanoTime();

  }

//访问线程

public static class Call implements Runnable {

    final long startTime;

    final Runnable underlyingRunnable;



    Call(Runnable underlyingRunnable) {

      this.underlyingRunnable = underlyingRunnable;

      this.startTime = now();

    }



    @Override

    public void run() {

      underlyingRunnable.run();

    }



    public long timeInQueue() {

      return now() - startTime;

    }



    @Override

    public boolean equals(Object other) {

      if (other instanceof Call) {

        Call otherCall = (Call)(other);

        return this.underlyingRunnable.equals(otherCall.underlyingRunnable);

      } else if (other instanceof Runnable) {

        return this.underlyingRunnable.equals(other);

      }

      return false;

    }



    @Override

    public int hashCode() {

      return this.underlyingRunnable.hashCode();

    }

  }

//在队列中获取默认Runnable

  @Override

  public Runnable poll() {

    Call result = underlyingQueue.poll();

    updateMetrics(result);

    return result;

  }



  private void updateMetrics(Call result) {

    if (result == null) {

      return;

    }

    metrics.incTimeInQueue(result.timeInQueue());

    metrics.setCallQueueLen(this.size());

  }

//在队列中获取Runnable

  @Override

  public Runnable poll(long timeout, TimeUnit unit) throws InterruptedException {

    Call result = underlyingQueue.poll(timeout, unit);

    updateMetrics(result);

    return result;

  }

 //队列中删除runnable

  @Override

  public Runnable remove() {

    Call result = underlyingQueue.remove();

    updateMetrics(result);

    return result;

  }



  @Override

  public Runnable take() throws InterruptedException {

    Call result = underlyingQueue.take();

    updateMetrics(result);

    return result;

  }

//添加到队列中

@Override

  public int drainTo(Collection<? super Runnable> destination) {

    return drainTo(destination, Integer.MAX_VALUE);

  }

//添加到队列中

@Override

  public int drainTo(Collection<? super Runnable> destination,

                     int maxElements) {

    if (destination == this) {

      throw new IllegalArgumentException(

          "A BlockingQueue cannot drain to itself.");

    }

    List<Call> drained = new ArrayList<Call>();

    underlyingQueue.drainTo(drained, maxElements);

    for (Call r : drained) {

      updateMetrics(r);

    }

    destination.addAll(drained);

    int sz = drained.size();

    LOG.info("Elements drained: " + sz);

    return sz;

  }

//队列中是否能提供call

@Override

  public boolean offer(Runnable element) {

    return underlyingQueue.offer(new Call(element));

  }



  @Override

  public boolean offer(Runnable element, long timeout, TimeUnit unit)

      throws InterruptedException {

    return underlyingQueue.offer(new Call(element), timeout, unit);

  }

@Override

public void put(Runnable element) throws InterruptedException {

    underlyingQueue.put(new Call(element));

  }



  @Override

  public boolean add(Runnable element) {

    return underlyingQueue.add(new Call(element));

  }



  @Override

  public boolean addAll(Collection<? extends Runnable> elements) {

    int added = 0;

    for (Runnable r : elements) {

      added += underlyingQueue.add(new Call(r)) ? 1 : 0;

    }

    return added != 0;

  }



  @Override

  public Runnable element() {

    return underlyingQueue.element();

  }



  @Override

  public Runnable peek() {

    return underlyingQueue.peek();

  }

//清空队列

  @Override

  public void clear() {

    underlyingQueue.clear();

  }



  @Override

  public boolean containsAll(Collection<?> elements) {

    return underlyingQueue.containsAll(elements);

  }



  @Override

  public boolean isEmpty() {

    return underlyingQueue.isEmpty();

  }



  @Override

  public Iterator<Runnable> iterator() {

    return new Iterator<Runnable>() {

      final Iterator<Call> underlyingIterator = underlyingQueue.iterator();

      @Override

      public Runnable next() {

        return underlyingIterator.next();

      }



      @Override

      public boolean hasNext() {

        return underlyingIterator.hasNext();

      }



      @Override

      public void remove() {

        underlyingIterator.remove();

      }

    };

  }



  @Override

  public boolean removeAll(Collection<?> elements) {

    return underlyingQueue.removeAll(elements);

  }



  @Override

  public boolean retainAll(Collection<?> elements) {

    return underlyingQueue.retainAll(elements);

  }



  @Override

  public int size() {

    return underlyingQueue.size();

  }



  @Override

  public Object[] toArray() {

    return underlyingQueue.toArray();

  }



  @Override

  public <T> T[] toArray(T[] array) {

    return underlyingQueue.toArray(array);

  }



  @Override

  public boolean contains(Object element) {

    return underlyingQueue.contains(element);

  }



  @Override

  public int remainingCapacity() {

    return underlyingQueue.remainingCapacity();

  }



  @Override

  public boolean remove(Object element) {

    return underlyingQueue.remove(element);

  }

}

hbase thrift 访问队列的更多相关文章

  1. 使用C#通过Thrift访问HBase

    前言 因为项目需要要为客户程序提供C#.Net的HBase访问接口,而HBase并没有提供原生的.Net客户端接口,可以通过启动HBase的Thrift服务来提供多语言支持. Thrift介绍 环境 ...

  2. HQueue:基于HBase的消息队列

    HQueue:基于HBase的消息队列   凌柏   ​1. HQueue简介 HQueue是一淘搜索网页抓取离线系统团队基于HBase开发的一套分布式.持久化消息队列.它利用HTable存储消息数据 ...

  3. 通过Thrift访问HDFS分布式文件系统的性能瓶颈分析

    通过Thrift访问HDFS分布式文件系统的性能瓶颈分析 引言 Hadoop提供的HDFS布式文件存储系统,提供了基于thrift的客户端访问支持,但是因为Thrift自身的访问特点,在高并发的访问情 ...

  4. MinerQueue.java 访问队列

    MinerQueue.java 访问队列 package com.iteye.injavawetrust.miner; import java.util.HashSet; import java.ut ...

  5. hbase thrift 定义

    /*  * Licensed to the Apache Software Foundation (ASF) under one  * or more contributor license agre ...

  6. HBase & thrift & C++编程

    目录 目录 1 1. 前言 1 2. 启动和停止thrift2 1 2.1. 启动thrift2 1 2.2. 停止thrift2 1 2.3. 启动参数 2 3. hbase.thrift 2 3. ...

  7. HBase数据访问的一些常用方式

    类型 特点 场合 优缺点分析 Native Java API 最常规和高效的访问方式 适合MapReduce作业并行批处理HBase表数据 Hbase Shell HBase的命令行工具,最简单的访问 ...

  8. windows通过thrift访问hdfs

    thirift是一个支持跨种语言的远程调用框架,通过thrift远程调用框架,结合hadoop1.x中的thriftfs,编写了一个针对hadoop2.x的thriftfs,供外部程序调用. 1.准备 ...

  9. python Hbase Thrift pycharm 及引入包

    cp -r hbase/ /usr/lib/python2.7/site-packages/ 官方示例子http://code.google.com/p/hbase-thrift/source/bro ...

随机推荐

  1. [nginx] 对UA为空的请求返回403

    nginx blocking blank user agent . sometime apps' backgroud request always visit a url, and these req ...

  2. BeanUtils 读取数据

    前两篇文章都是关于setProperty的,下面来说一个关于getProperty 的小案例.如下: MyClass.java package beanutils; public class MyCl ...

  3. [virtualenv]生产环境中使用virtualenv

    virtualenv 对于python开发和部署都是好工具,可以隔离多个python版本和第三方库的版本,这里作者总结了几个常用python服务怎么样结合virtual部署 原文链接 Python 中 ...

  4. Objc中为何某些类的属性要设置为copy而不是strong?

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 不知道大家是否注意,我们再使用一些第三方类的时候大多数情况下对 ...

  5. there was no endpoint listening at net.pipe://localhost/PreviewProcessingService/ReportProcessing

    当你在开发reporting service报表时,进行报表的preview时报下图中的错误,以下方法可以让你直接跳过这个错误,继续查看报表的运行结果. 直接选择你需要运行查看的报表右击run就可以, ...

  6. 饮一盏Bug留香,唱一曲项目飞扬

    沿途的风景    牵挂的项目    两情迢迢 学生档案管理项目在2月的末尾从稍带寒意的季节里完成了第一次迭代,验收的结果不尽善尽美,演示的功能也惨不忍睹,各种"关爱"的点评充斥耳旁 ...

  7. Android对话框AlertDialog-android学习之旅(四十二)

    对话框简介 android提供了丰富的对话框支持,支持四种如下的对话框. AlertDialog简介 介绍上面六个方法的代码示例 setMessage() <?xml version=" ...

  8. Mybatis执行ReuseExecutor(五)

    ReuseExecutor顾名思义就是重复使用执行,其定义了一个Map<String, Statement>,将执行的sql作为key,将执行的Statement作为value保存,这样执 ...

  9. python类定义

    在我的收藏中有一篇特别详细的类讲解 此处部分内容引自:http://blog.sina.com.cn/s/blog_59b6af690101bfem.html class myclass: 'this ...

  10. Leetcode_7_Reverse Integer

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41578077 Reverse Integer Revers ...