GenEvent 是事件处理的通用部分的抽象。
经过 GenEvent ,咱们给已有的服务 动态 的添加 事件处理。html
以前已经介绍了 GenServer ,GenServer 和 GenEvent 的主要区别在于:ios
基于上述的区别,GenEvent 和 GenServer 的应用场景也不一样。服务器
经过 GenEvent 建立一个事件管理器,将此事件管理器添加到现有进程中,现有进程就有了处理相应事件的能力。异步
简单示例以下:测试
defmodule HelloEvent do use GenEvent def handle_event(event, parent) do case event do :hello -> send parent, :world :world -> send parent, :hello _ -> send parent, "error msg" end {:ok, parent} end end
测试过程:spa
# 启动一个空的事件管理器 iex(1)> {:ok, manager} = GenEvent.start_link {:ok, #PID<0.87.0>} # 发送 :hello 消息 iex(2)> GenEvent.sync_notify(manager, :hello) :ok # 没有任何反应,由于事件管理器中没有任何 handle 来处理消息 iex(3)> flush :ok # 给事件管理器增长一个handle,同时将当前进程PID做为事件处理的状态 iex(4)> GenEvent.add_handler(manager, HelloEvent, self()) :ok # 发送 :hello 消息 iex(5)> GenEvent.sync_notify(manager, :hello) :ok # 事件管理器处理了 :hello 消息,并返回 :world 结果 iex(6)> flush :world :ok # 发送 :world 消息 iex(7)> GenEvent.sync_notify(manager, :world) :ok # 事件管理器处理了 :world 消息,并返回 :hello 结果 iex(8)> flush :hello :ok # 发送 :test 消息 iex(9)> GenEvent.sync_notify(manager, :test) :ok # 事件管理器对于 :hello 和 :world 之外的消息都返回 "error msg" iex(10)> flush "error msg" :ok
上面测试中用的发送消息的方法都是同步方式 sync_notify ,经过异步方式 notify 发送消息也是同样的, GenEvent 的 handle_event 接收同步和异步的消息。code
事件流就是将 GenEvent 的事件转入到流中,这样,就能够经过处理流的方式来处理事件。server
好比上面的例子,经过 GenEvent 的 stream ,能够不定义 defmodule HelloEvent 也实现上面的功能。htm
上述测试过程能够改成以下:blog
iex(1)> {:ok, manager} = GenEvent.start_link {:ok, #PID<0.59.0>} iex(2)> stream = GenEvent.stream(manager) %GenEvent.Stream{manager: #PID<0.59.0>, timeout: :infinity} iex(3)> nil iex(4)> spawn_link fn -> ...(4)> for x <- stream do ...(4)> case x do ...(4)> :hello -> IO.inspect :world ...(4)> :world -> IO.inspect :hello ...(4)> _ -> IO.inspect "error msg" ...(4)> end ...(4)> end ...(4)> end #PID<0.71.0> iex(5)> GenEvent.sync_notify(manager, :hello) :world :ok iex(6)> GenEvent.sync_notify(manager, :world) :hello :ok iex(7)> GenEvent.sync_notify(manager, :test) "error msg" :ok
能够看出,咱们没有给 GenEvent 绑定任何的 handler,而是在 GenEvent 的事件流中对全部消息进行了处理。
GenEvent 中事件流的特性是 erlang 中所没有的。
除了上面用的 handle_event 和 stream, GenEvent 中还有其余的实用的 Functios 和 Callbacks
具体参见:http://elixir-lang.org/docs/stable/elixir/GenEvent.html