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 ...
随机推荐
- Mysql增加主键或者更改表的列为主键的sql语句
...
- 删除织梦所有待审核稿件sql语句
先提醒一下 archives是dedecms主表addonarticle 新闻信息表 在dede后台"系统->SQL命令行工具"运行下以命令即可(注意,运行后未审核的数据全被 ...
- 第五十四篇、OC利用AFN上传上传语音
如果不是NSData就要想办法把语音文件转化为NSData,然后才能上传服务器. 首先我们拿到语音文件对应的NSData对象 NSData *voiceData = [message valueFor ...
- PHP学习笔记 - 入门篇(2)
PHP入门篇(2) 什么是变量 变量是用于存储值的容器,如下 $var = @"6666" 如何定义变量 定义变量就是像服务器的内存申请空间,用来存储数据,eg: <?php ...
- java SimpleDateFormat非线程安全测试
public class MyThread extends Thread { private SimpleDateFormat sdf; private String dateString; publ ...
- 【转】简单理解socket
题外话 前几天和朋友聊天,朋友问我怎么最近不写博客了,一个是因为最近在忙着公司使用的一些控件的开发,浏览器兼容性搞死人:但主要是因为这段时间一直在看html5的东西,看到web socket时觉得很有 ...
- 引用、return
C语言中没有引用,引用(reference)是c++对c语言的重要扩充.通俗点说,引用就是“起别名”.比如变量data,和它的引用 RefData.虽然名字不同,但是操作他们的时候,都操作的是相同的内 ...
- 使用python读写windows剪切板
import win32clipboard as w import win32con base_addr = 0x8e00000 buffer_len = 0x123 def getText(): w ...
- c#怎么获取当前页面的url
Request.ApplicationPath: /testwebRequest.CurrentExecutionFilePath: /testweb/default.aspxRequest.File ...
- 【CLR VIA C#】读书笔记
工作几年了才看,记录下笔记备忘. 章节 笔记 1.CLR的执行模型 公共语言运行时(Common Language Runtime,CLR) 源代码-->编译器检查语法和分析源代码-->托 ...