Storm-源码分析-EventManager (backtype.storm.event)
大体结构,
定义protocol EventManager, 其实就是定义interface
函数event-manager, 主要做2件事
1. 启动event queue的处理线程, 不断从queue中取出event-fn并执行
2. 返回实现EventManager的匿名record(reify部分, 实现protocol)
这里使用了reify的close over特性, reify会将用到的局部变量打包到闭包内, 包含queue, runner
(ns backtype.storm.event
(:use [backtype.storm log util])
(:import [backtype.storm.utils Time Utils])
(:import [java.util.concurrent LinkedBlockingQueue TimeUnit])
) (defprotocol EventManager
(add [this event-fn])
(waiting? [this])
(shutdown [this])) (defn event-manager
"Creates a thread to respond to events. Any error will cause process to halt"
[daemon?]
(let [added (atom 0)
processed (atom 0)
^LinkedBlockingQueue queue (LinkedBlockingQueue.)
running (atom true)
runner (Thread.
(fn []
(try-cause
(while @running
(let [r (.take queue)]
(r)
(swap! processed inc)))
(catch InterruptedException t
(log-message "Event manager interrupted"))
(catch Throwable t
(log-error t "Error when processing event")
(halt-process! 20 "Error when processing an event"))
)))]
(.setDaemon runner daemon?)
(.start runner)
(reify
EventManager
(add [this event-fn]
;; should keep track of total added and processed to know if this is finished yet
(when-not @running
(throw (RuntimeException. "Cannot add events to a shutdown event manager")))
(swap! added inc)
(.put queue event-fn)
)
(waiting? [this]
(or (Time/isThreadWaiting runner)
(= @processed @added)
))
(shutdown [this]
(reset! running false)
(.interrupt runner)
(.join runner)
)
)))
使用的时候很简单, 如下
let [event-manager processes-event-manager :as managers] [(event/event-manager false) (event/event-manager false)]
(.add processes-event-manager sync-processes)
可以直接调用add或其他的function
相当于给event-manager增加EventManager protocol, 反过来说, 给add或其他接口functions增加对event-manager record的support, 因为protocol函数的第一个参数总是类型
比较神奇的是, 闭包产生的效果, 可以在完全没有queue, runner定义或声明的情况下, 方便的操作他们
Storm-源码分析-EventManager (backtype.storm.event)的更多相关文章
- Storm源码分析--Nimbus-data
nimbus-datastorm-core/backtype/storm/nimbus.clj (defn nimbus-data [conf inimbus] (let [forced-schedu ...
- JStorm与Storm源码分析(四)--均衡调度器,EvenScheduler
EvenScheduler同DefaultScheduler一样,同样实现了IScheduler接口, 由下面代码可以看出: (ns backtype.storm.scheduler.EvenSche ...
- JStorm与Storm源码分析(一)--nimbus-data
Nimbus里定义了一些共享数据结构,比如nimbus-data. nimbus-data结构里定义了很多公用的数据,请看下面代码: (defn nimbus-data [conf inimbus] ...
- JStorm与Storm源码分析(三)--Scheduler,调度器
Scheduler作为Storm的调度器,负责为Topology分配可用资源. Storm提供了IScheduler接口,用户可以通过实现该接口来自定义Scheduler. 其定义如下: public ...
- JStorm与Storm源码分析(二)--任务分配,assignment
mk-assignments主要功能就是产生Executor与节点+端口的对应关系,将Executor分配到某个节点的某个端口上,以及进行相应的调度处理.代码注释如下: ;;参数nimbus为nimb ...
- storm源码分析之任务分配--task assignment
在"storm源码分析之topology提交过程"一文最后,submitTopologyWithOpts函数调用了mk-assignments函数.该函数的主要功能就是进行topo ...
- 一个普通的 Zepto 源码分析(三) - event 模块
一个普通的 Zepto 源码分析(三) - event 模块 普通的路人,普通地瞧.分析时使用的是目前最新 1.2.0 版本. Zepto 可以由许多模块组成,默认包含的模块有 zepto 核心模块, ...
- storm源码分析之topology提交过程
storm集群上运行的是一个个topology,一个topology是spouts和bolts组成的图.当我们开发完topology程序后将其打成jar包,然后在shell中执行storm jar x ...
- Nimbus<三>Storm源码分析--Nimbus启动过程
Nimbus server, 首先从启动命令开始, 同样是使用storm命令"storm nimbus”来启动看下源码, 此处和上面client不同, jvmtype="-serv ...
随机推荐
- -[__NSArrayI removeAllObjects]: unrecognized selector sent to instance 0x7fa8dc830110
问题 今天做项目,遇到了这个问题 -[__NSArrayI removeAllObjects]: unrecognized selector sent to instance 0x7fa8dc8301 ...
- 华中农业大学校赛--c The Same Color
Problem C: The Same Color Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 993 Solved: 595[Submit][St ...
- hdu3948(后缀数组)
题意:给一串字符,需要你求不相同的回文子串个数....... 同ural1297,链接:http://www.cnblogs.com/ziyi--caolu/archive/2013/06/09/31 ...
- 【Code::Blocks】windows 环境下编译 Code::Blocks(已修正)
Code::Blocks 在2012-11-25发布了最新的12.11版本,相比上一个版本(10.05),Code::Blocks 进行了许多改进和更新(Change log). 引用 Wikiped ...
- array2json
原文:jQuery方法扩展:type, toJSON, evalJSON. http://zhkac.iteye.com/blog/499330 .2013-05-19 (function($) { ...
- Eclipse报Caused by: java.lang.OutOfMemoryError: PermGen space解决思路
一.修改tomcat/bin目录下的catalina.bat 在“rem ----- Execute The Requested Command ----------------------”下加入 ...
- Odoo 8.0 new API 概述
相对于7来说,8的api改进了不少,用官方的话来说就是更加面向对象了. 下面探究一下具体的改动. 准备知识:python装饰器的使用 http://blog.csdn.net/thy38/articl ...
- Spring MVC之@RequestParam @RequestBody @RequestHeader 等详解<转>
简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...
- 介绍编译的less的几种IDE工具
介绍编译的less的两种IDE工具 现在css预编译越来越普及了,著名的有less.sass.stylus等等等等.功能上基本上都是大同小异.这些个玩意儿主要表达的意思就是:“像编程一样的编写你的cs ...
- jQuery无刷新分页完整实例代码
在线演示地址如下: http://demo.jb51.net/js/2015/jquery-wsx-page-style-demo/ <!DOCTYPE html> <head> ...