hbase thrift 访问队列
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 访问队列的更多相关文章
- 使用C#通过Thrift访问HBase
前言 因为项目需要要为客户程序提供C#.Net的HBase访问接口,而HBase并没有提供原生的.Net客户端接口,可以通过启动HBase的Thrift服务来提供多语言支持. Thrift介绍 环境 ...
- HQueue:基于HBase的消息队列
HQueue:基于HBase的消息队列 凌柏 1. HQueue简介 HQueue是一淘搜索网页抓取离线系统团队基于HBase开发的一套分布式.持久化消息队列.它利用HTable存储消息数据 ...
- 通过Thrift访问HDFS分布式文件系统的性能瓶颈分析
通过Thrift访问HDFS分布式文件系统的性能瓶颈分析 引言 Hadoop提供的HDFS布式文件存储系统,提供了基于thrift的客户端访问支持,但是因为Thrift自身的访问特点,在高并发的访问情 ...
- MinerQueue.java 访问队列
MinerQueue.java 访问队列 package com.iteye.injavawetrust.miner; import java.util.HashSet; import java.ut ...
- hbase thrift 定义
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agre ...
- 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. ...
- HBase数据访问的一些常用方式
类型 特点 场合 优缺点分析 Native Java API 最常规和高效的访问方式 适合MapReduce作业并行批处理HBase表数据 Hbase Shell HBase的命令行工具,最简单的访问 ...
- windows通过thrift访问hdfs
thirift是一个支持跨种语言的远程调用框架,通过thrift远程调用框架,结合hadoop1.x中的thriftfs,编写了一个针对hadoop2.x的thriftfs,供外部程序调用. 1.准备 ...
- python Hbase Thrift pycharm 及引入包
cp -r hbase/ /usr/lib/python2.7/site-packages/ 官方示例子http://code.google.com/p/hbase-thrift/source/bro ...
随机推荐
- PLSQL实现分页查询
--集合实现游标查询 CREATE OR REPLACE PACKAGE emppkg IS TYPE t_record IS RECORD( rn INT, empno emp.empno%TYPE ...
- RxJava(九)zip操作符在Android中的实际使用场景
欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/51614927 本文出自:[余志强的博客] 一.zip操作符概述 官方 ...
- Java提升篇之反射的原理(二)
Java提升篇之通过反射越过泛型检查 /* *问题:在一个ArrayList<Integer>对象中,在这个集合中添加一个字符串. */ 在我们还没有学反射前,遇到这个问题都是无法实现的, ...
- Storm 0.9安装指南
Storm 0.9.2安装指南 0 Storm0.9的亮点 引用网上的描述: "Storm 0.9.0.1版本的第一亮点是引入了netty transport.Storm网络传输机制实现可插 ...
- API创建员工
DECLARE lc_employee_number PER_ALL_PEOPLE_F.EMPLOYEE_NUMBER%TYPE := 'PRAJ_01'; ln_person_id PER_ALL_ ...
- kindeditor用法简单介绍
最近做毕业设计用了一个叫做kindeditor的文本编辑工具,相信很多人都用过,这货和fckeditor差不多,个人感觉这个的皮肤更好看,而且对中文的支持更好,没那么容易出现中文乱码问题.下次记录一下 ...
- Android初级教程理论知识(第九章多媒体编程)
多媒体概念 文字.图片.音频.视频 计算机图片大小的计算 图片大小 = 图片的总像素 * 每个像素占用的大小 单色图:每个像素占用1/8个字节 16色图:每个像素占用1/2个字节 256色图:每个像素 ...
- iOS开发中 常用枚举和常用的一些运算符(易错总结)
1.色值的随机值: #define kColorValue arc4random_uniform(256)/255.0 // arc4random_uniform(256)/255.0; 求出0.0~ ...
- Unity StrangeIoC HelloWorld
unity有一个叫StrangeIoC的框架插件,这里写了一个使用StrangeIoC的HelloWorld,比他自带的demo更为简单,方便理解 1.插件下载,在Asset Store直接搜索Str ...
- rhel6.4 安装 mysql-5.6
rhel6.4 安装 mysql-5.6 下载(临时地址, 如不可用,请到oracle官网下载) 采用rpm安装. mysql服务端要安装: ftp://pepstack.com/pub/rpm/My ...