Flume学习——BasicTransactionSemantics
An implementation of basic
Transactionsemantics designed to work in concert withBasicChannelSemanticsto simplify creation of robustChannelimplementations. This class ensures that each transaction implementation method is called only while the transaction is in the correct state for that method, and only by the thread that created the transaction. Nested calls tobegin()andclose()are supported as long as they are balanced.Subclasses need only implement
doPut,doTake,doCommit, anddoRollback, and the developer can rest assured that those methods are called only after transaction state preconditions have been properly met.doBeginanddoClosemay also be implemented if there is work to be done at those points.All InterruptedException exceptions thrown from the implementations of the
doXXXmethods are automatically wrapped to become ChannelExceptions, but only after restoring the interrupted status of the thread so that any subsequent blocking method calls will themselves throw InterruptedException rather than blocking. The exception to this rule isdoTake, which simply returns null instead of wrapping and propagating the InterruptedException, though it still first restores the interrupted status of the thread.
BasicTransactionSemantics实现了Transaction的基本语法,与BasicChannelSemantics一起工作,来使得创建健壮的Channel实现更加简单。这个类确保了一个事务中的操作(如take(),put())只有在当前的事务处于对应的状态时才被调用,并且只有创建了当前的事务的线程才能调用这些事务中的操作。对begin()和close()的嵌套调用是支持的,只要对它们的调用保持平衡(这句不怎么明白,对同一个transaction对象嵌套调用这两个方法明显不行,应该是不同的transaction对象的close和begin方法可以嵌套)。
它的子类可以只实现doPut, doTake, doCommit和doCommit、doRollback方法,而不必再设法确保这些方法只有在正确的事务状态下才被调用。当doBegin和doClose也有特殊的操作时,也可以实现这两个方法。
在doXXX方法中抛出的InterruptedException都被包装进ChannelException中,但这是在恢复了当前线程的interrupted状态之后,这样接下的blocking method call就会抛出InterruptedException,而不是进入阻塞状态。这个规则的的例外是doTake,当只是返回null,而不是包装、传播InterruptedException,但是doTake仍然先恢复了线程的interrupted状态。
BasicTransactionSemantics实现了跟事务范围内各个操作有关方法。
Method Summary voidbegin()
Starts a transaction boundary for the current channel operation.voidclose()
Ends a transaction boundary for the current channel operation.voidcommit()
Indicates that the transaction can be successfully committed.protected voiddoBegin()
protected voiddoClose()
protected abstract voiddoCommit()
protected abstract voiddoPut(Event event)
protected abstract voiddoRollback()
protected abstract EventdoTake()
protected BasicTransactionSemantics.StategetState()
protected voidput(Event event)
The method to whichBasicChannelSemanticsdelegates calls toput.voidrollback()
Indicates that the transaction can must be aborted.protected Eventtake()
The method to whichBasicChannelSemanticsdelegates calls totake.StringtoString()
其中put take begin close commit rollback 的结构都很相似。主要结构都是确保这些操作时Transaction在正确的对应状态,然后调用doXXX方法。如果当前的线程不拥有当前的事务或者事务的状态不对,就抛出异常。如果doXXX方法抛出InterruptedException就通过Thread.currentThread.interrupt()方法恢复当前线程的interrupted状态,然后将捕获的InterruptedException包装成一个ChannelException,抛出。
@Override
public void rollback() {
Preconditions.checkState(Thread.currentThread().getId() == initialThreadId,
"rollback() called from different thread than getTransaction()!");
Preconditions.checkState(state.equals(State.OPEN),
"rollback() called when transaction is %s!", state); state = State.COMPLETED;
try {
doRollback();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new ChannelException(e.toString(), e);
}
}
可见BasicTransactionSemantic类确保了事务中各个操作的执行顺序。并且对执行各个操作中可能抛出的InterruptedException进行了一次封装。它的各个子类只需要实现具体的操作。
以BasicChannelSemantic类的代码可以看出,它对Channel接口定义的put(event)和take()的实现是把这两个方法代理给了自己线程中的BasicTransactionSemantic的put和get方法。而BasicTransactionSemantic中这两个方法
protected Event take() {
Preconditions.checkState(Thread.currentThread().getId() == initialThreadId,
"take() called from different thread than getTransaction()!");
Preconditions.checkState(state.equals(State.OPEN),
"take() called when transaction is %s!", state);
try {
return doTake();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return null;
}
}
是交给了子类要实现的doTake和doPut方法。因此一个Channel的具体实现,其消息的处理逻辑是由自己的getTransaction()方法返回的Trasaction对象来实现的。正如MemoryChannel的内部类MemoryTransaction所做的一样。
Flume学习——BasicTransactionSemantics的更多相关文章
- Flume学习总结
Flume学习总结 flume是一个用来采集数据的软件,它可以从数据源采集数据到一个集中存放的地方. 最常用flume的数据采集场景是对日志的采集,不过,lume也可以用来采集其他的各种各样的数据,因 ...
- flume学习(三):flume将log4j日志数据写入到hdfs(转)
原文链接:flume学习(三):flume将log4j日志数据写入到hdfs 在第一篇文章中我们是将log4j的日志输出到了agent的日志文件当中.配置文件如下: tier1.sources=sou ...
- Flume学习应用:Java写日志数据到MongoDB
概述 Windows平台:Java写日志到Flume,Flume最终把日志写到MongoDB. 系统环境 操作系统:win7 64 JDK:1.6.0_43 资源下载 Maven:3.3.3下载.安装 ...
- Flume学习 & Kafka & Storm 等 & Log4J 配置
正在学习这篇文章: http://blog.csdn.net/ymh198816/article/details/51998085 和工作中接触的电商.订单.分析,可以结合起来. 开宗明义,这幅图片: ...
- Flume学习——Flume中事务的定义
首先要搞清楚的问题是:Flume中的事务用来干嘛? Flume中的事务用来保证消息的可靠传递. 当使用继承自BasicChannelSemantics的Channel时,Flume强制在操作Chann ...
- flume学习安装
近期项目组有需求点击流日志须要自己收集,学习了一下flume而且成功安装了.相关信息记录一下. 1)下载flume1.5版本号 wget http://www.apache.org/dyn/clos ...
- flume学习以及ganglia(若是要监控hive日志,hive存放在/tmp/hadoop/hive.log里,只要运行过hive就会有)
python3.6hdfs的使用 https://blog.csdn.net/qq_29863961/article/details/80291654 https://pypi.org/ 官网直接搜 ...
- flume学习
下载 自定义sink(mysql) 1.ide打开下载后的源码 2.代码如下: /** * Licensed to the Apache Software Foundation (ASF) under ...
- Flume学习——BasicChannelSemantics
public class MemoryChannel extends BasicChannelSemantics public abstract class BasicChannelSemantics ...
随机推荐
- Android之所有权限
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" >& ...
- JSP之初识
JSP是“java server pages”的缩写,java是一种编程语言,jsp只是相当于java里面的servlet部分,所以JSP技术是以Java语言作为脚本语言的. JSP这门技术的最大的特 ...
- SQL Server的三种物理连接之Hash Join(三)
简介 在 SQL Server 2012 在一些特殊的例子下会看到下面的图标: Hash Join分为两个阶段,分别为生成和探测阶段. 首先是生成阶段,将输入源中的每一个条目经过散列函数的计算都放到不 ...
- ###《VIM实用技巧》
###<VIM实用技巧> #@author: gr #@date: 2015-11-20 #@email: forgerui@gmail.com <VIM实用技巧>阅读笔记. ...
- Cocos2d-x数据持久化-修改数据
修改数据时,涉及的SQL语句有insert.update和delete语句,这3个SQL语句都可以带参数.修改数据的具体步骤如下所示.(1) 使用sqlite3_open函数打开数据库.(2) 使用s ...
- Objective-c中的对象间的消息传递以及消息路由
刚开始使用Objective-C时,总是习惯将对象间发送消息之间称呼为方法调用.心想,这和c#不是一回事吗?不就是调用实例方法吗,还搞个消息发送作甚,最后还不是要转化为方法的调用?通过一段时间的理解学 ...
- 添加点标注IMarkerElement
private void AddPointElement(IPoint pPoint) { if (pPoint != null) { IElement pElement = null; IRgbCo ...
- bzoj3405:[Usaco2009 Open]Grazing2 移动牛棚
思路:首先因为要让距离尽量大,所以奶牛1一定在1号牛棚,奶牛n一定在s号牛棚,然后考虑dp. 因为总距离为s-1,然后要使长度为d的段数尽量多,那么剩下的一定就是d+1的段数,也就是s-(n-1)*d ...
- ios版弹珠游戏源码
这个是我们比较喜欢玩的一直小游戏的,ios版弹珠游戏源码,该游戏源码来着IOS教程网其他网友提供上传的,大家可以了解一下吧. nore_js_op> <ignore_js_op&g ...
- PHP 魔术方法 __isset __unset (三)
慢慢长寻夜,明月高空挂 __isset() - 在对类中属性或者非类中属性使用isset()方法的时候如果没有或者非公有属性,则自动执行__isset()的方法 __unset() - 在对类中属性 ...