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. Java并发框架——AQS之如何使用AQS构建同步器

    AQS的设计思想是通过继承的方式提供一个模板让大家可以很容易根据不同场景实现一个富有个性化的同步器.同步器的核心是要管理一个共享状态,通过对状态的控制即可以实现不同的锁机制.AQS的设计必须考虑把复杂 ...

  2. Compass 更智能的搜索引擎(1)--入门

    学完了前面的Lucene系列教程: 全文检索 Lucene(1)–入门 全文检索 Lucene(2)–进阶 全文检索 Lucene(3)–分页 全文检索 Lucene(4)–高亮 Lucene确实是个 ...

  3. x264源代码简单分析:编码器主干部分-2

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

  4. T-SQL动态查询(2)——关键字查询

    接上文:T-SQL动态查询(1)--简介 前言: 在开发功能的过程中,我们常常会遇到类似以下情景:应用程序有一个查询功能,允许用户在很多查询条件中选择所需条件.这个也是本系列的关注点. 但是有时候你也 ...

  5. Effective C++ ——构造/析构/赋值运算符

    条款五:了解C++默认编写并调用那些函数 是否存在空的类? 假设定义类为class Empty{}:当C++编译器处理过后会变成如下的形式: class Empty{ Empty(){} ~Empty ...

  6. 侧滑面板(对viewGroup的自定义)

    额,好吧,最近一直在做侧滑的事情,到目前为止一共是学了三种方法了,一个是直接加第三方开源框架SlidingMenu,第二给是用DrawerLayout,今天这个是用谷歌官方提供的在新的support- ...

  7. Linux下yum安装MySQL yum安装MySQL指定版本

    yum安装MySQL 1. 查看有没有安装过     yum list installed MySQL* (有存在要卸载yum remove MySQL*)     rpm -qa | grep my ...

  8. Download all Apple open source OS X files at once

    While it is well known that Mac OS X contains open source code, how to access and download that sour ...

  9. Android计时器Chronometer-android学习之旅(二十一)

    Chronometer简介 Chronometer和DigitalColok都继承与TextView,但是Chronometer不是显示的当前时间,而是从某个时间开始又过去了多少时间,是一个时间差. ...

  10. 使用Spring+Junit4.4进行测试

    http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...