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. Spark技术内幕: Shuffle详解(一)

    通过上面一系列文章,我们知道在集群启动时,在Standalone模式下,Worker会向Master注册,使得Master可以感知进而管理整个集群:Master通过借助ZK,可以简单的实现HA:而应用 ...

  2. Dynamics CRM 为Visual Studio 2015安装CRM Developer Toolkit

    从CRM2015的SDK以后Tools的文件夹里就没有了DeveloperToolkit,而DeveloperToolkit还是停留在VS2012版本,这对于我们这种用新版本的童鞋来说比较头疼,我本地 ...

  3. 5.1、Android Studio用Logcat编写和查看日志

    Android Studio在Android Monitor中包含了一个logcat的tab,可以打印系统事件,比如垃圾回收发生时,实时打印应用消息. 为了显示需要的信息,你可以创建过滤器,更改需要显 ...

  4. 剑指Offer——二叉树

    剑指Offer--二叉树 前言 数据结构通常是编程面试中考察的重点.在参加面试之前,应聘者需要熟练掌握链表.树.栈.队列和哈希表等数据结构,以及它们的操作.本片博文主要讲解二叉树操作的相关知识,主要包 ...

  5. Android开发优化之——使用软引用和弱引用

    Java从JDK1.2版本开始,就把对象的引用分为四种级别,从而使程序能更加灵活的控制对象的生命周期.这四种级别由高到低依次为:强引用.软引用.弱引用和虚引用. 这里重点介绍一下软引用和弱引用. 如果 ...

  6. 1073. Scientific Notation (20)

    题目如下: Scientific notation is the way that scientists easily handle very large numbers or very small ...

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

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

  8. 软考下午题详解---uml图

    在上篇博客中,小编主要简单的对软考下午题当中的数据流图设计进行了一系列总结,今天我们继续来看软考下午题当中大题部分,uml图的相关知识,在我们学习的过程中,我们也已经接触过,西安交大刘惠老师讲解过um ...

  9. 【Unity Shaders】使用Unity Render Textures实现画面特效——建立画面特效脚本系统

    本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...

  10. Swift基础之实现一个镂空图片的小Demo

    前两天看了别人的文章,涉及到了镂空的展示,所以我在这里把实现的内容写成Swift语言的小Demo,供大家欣赏 首先,需要创建导航视图,然后创建两种展示方式的按钮 let vc = ViewContro ...