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. html5全解析

    htm是软件开发中非常基础的知识,也是很重要的知识,在web中是很重要的知识点,在此梳理一下主要内容: 1.HTML是什么? 全称为HyperText Markup Language,超文本标记语言, ...

  2. cuda网格的限制

    限制于计算能力有关. 详情 http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#compute-capabilities 只 ...

  3. frameset 与frame 设置的技巧

    今天来写点不一样的.如下图: 实现的效果就是原生的类似于导航形式的frameset. frameset 注意: 包含frameset的网页应该只是作为框架而存在,所以不能有body标签. 这个标签可以 ...

  4. Android之自定义AlertDialog和PopupWindow实现(仿微信Dialog)

    我们知道,在很多时候,我们都不用Android内置的一些控件,而是自己自定义一些自己想要的控件,这样显得界面更美观. 今天主要是讲自定义AlertDialog和popupWindow的使用,在很多需求 ...

  5. vs 删除行尾空格

    vs 删除行尾空格 vs2010:Enter: Ctrl+H Find what: :b*$ Replace with: [Empty] Look in: Current Document Find ...

  6. UE4实现描边效果

    描边效果属于常见常用的功能,现VR项目中,也需要射线选中一个物体,使物体高亮. 于是在网上找了部分资料,同时也感谢群里的一位大神的提点,总算将描边的功能实现了,这里也写一个简单的示例步骤. 1.我并不 ...

  7. (八十六)使用系统自带的分享框架Social.framework

    使用Social.framework十分简单,能够便捷的分享到主流的社交框架. ①导入主头文件 #import <Social/Social.h> ②以新浪微博为例,首先判断服务是否可用, ...

  8. 精通CSS+DIV网页样式与布局--页面背景

    上篇博客,我们主要简单的总结了CSS的图片效果,我们这回来讲讲CSS如何对网页的背景进行设置,网页的背景是整个网页的重要组成部分,她直接决定了整个网页的风格和色调.这篇博客简单的总结一下如何用CSS来 ...

  9. java开源项目之IQQ学习记录之项目环境搭建与启动

    本文链接地址:http://blog.csdn.net/sushengmiyan/article/details/18779727 作者:sushengmiyan 现在就码字说说今天晚上搞定的一个项目 ...

  10. ROC曲线的AUC(以及其他评价指标的简介)知识整理

    相关评价指标在这片文章里有很好介绍 信息检索(IR)的评价指标介绍 - 准确率.召回率.F1.mAP.ROC.AUC:http://blog.csdn.net/marising/article/det ...