标签:
ejabberd_iq_sup作为supervisor启动,子程序启动的模块为gen_iq_handler
gen_iq_handler:
根据配置参数iqdisc,可选项为no_queue, one_queue, {quques, N}和parallel
add_iq_handler(Component, Host, NS, Module, Function, Type) -> case Type of no_queue -> Component:register_iq_handler(Host, NS, Module, Function, no_queue); one_queue -> {ok, Pid} = supervisor:start_child(ejabberd_iq_sup, [Host, Module, Function]), Component:register_iq_handler(Host, NS, Module, Function, {one_queue, Pid}); N when is_integer(N) -> Pids = lists:map(fun (_) -> {ok, Pid} = supervisor:start_child(ejabberd_iq_sup, [Host, Module, Function]), Pid end, lists:seq(1, N)), Component:register_iq_handler(Host, NS, Module, Function, {queues, Pids}); parallel -> Component:register_iq_handler(Host, NS, Module, Function, parallel) end.
add_iq_handler根据选项可分为四种启动方式,这是根据IQ的业务性质决定的
因为IQ业务的处理时间不同,有的耗时,有的不耗时,所以有的直接执行,有的另起进程
具体操作为handle函数
handle(Host, Module, Function, Opts, From, To, IQ) -> case Opts of no_queue -> process_iq(Host, Module, Function, From, To, IQ); {one_queue, Pid} -> Pid ! {process_iq, From, To, IQ}; {queues, Pids} -> Pid = lists:nth(erlang:phash(now(), length(Pids)), Pids), Pid ! {process_iq, From, To, IQ}; parallel -> spawn(?MODULE, process_iq, [Host, Module, Function, From, To, IQ]); _ -> todo end.
gen_iq_handler的域分为 ejabberd_sm | ejabberd_local
-type component() :: ejabberd_sm | ejabberd_local.
意思为只向ejabberd_sm 、ejabberd_local注册
这样则主业务逻辑和模块分开,主业务逻辑只需调用gen_iq_handler提供的接口,所有的操作细节则有gen_iq_handler处理。
所以gen_iq_handler屏蔽了各个模块的差异
标签:
原文地址:http://www.cnblogs.com/lawen/p/5057556.html