[Erlang 0095] 善用 Erlang module_info

简介:
在.net里面我们可以使用Attribute和反射在运行时完成对程序集元数据的解析; 下面是C#写得一个简单的例子:
 
复制代码
 Worker1 worker1 = new Worker1 ();
  var attribute = worker1.GetType().GetCustomAttribute(typeof( ProcessOrderAttribute)) as ProcessOrderAttribute ;
  Console.WriteLine("Description {0}  Order {1}" , attribute.Descrption, attribute.Order);
  Console.ReadLine();

[ProcessOrder( " first step", 1)]
 public class Worker1
 {
 }

class ProcessOrderAttribute : Attribute
   {
       public string Descrption { get; set; }
       public int Order { get; set; }

       public ProcessOrderAttribute(string description, int order)
       {
           Descrption = description;
           Order = order;
       }
   }
复制代码

 

   

  类似的在Erlang中,也可以做类似的事情,我们可以通过module_info获取模块的元数据,比如:
 
复制代码
(rabbit@nimbus)4> test:module_info().
[{exports,[{module_info,0},{module_info,1}]},
{imports,[]},
{attributes,[{vsn,[64335248162526234078446821625873662118]},
              {tag,[generated_by_tool]}]},
{compile,[{options,[{d,use_specs},
                     {outdir,"/data/rabbitmq-server-3.0.0/ebin"},
                     {i,"/data/rabbitmq-server-3.0.0/include"},
                     debug_info]},
           {version,"4.8"},
           {time,{2012,12,12,14,39,42}},
           {source,"/data/rabbitmq-server-3.0.0/src/test.erl"}]}]
(rabbit@nimbus)5>
复制代码

 

  在RabbitMQ中就使用元数据 rabbit_boot_step来控制启动的步骤,比如在下面是rabbit.erl中的代码片段,每一个rabbit_boot_step都定义了启动的mfa,前置条件,描述信息等等,在rabbit启动的时候,会根据模块的元素信息按顺序完成启动.
 
复制代码
-rabbit_boot_step({rabbit_log,
                   [{description, "logging server"},
                    {mfa,         {rabbit_sup, start_restartable_child,
                                   [rabbit_log]}},
                    {requires,    external_infrastructure},
                    {enables,     kernel_ready}]}).

-rabbit_boot_step({rabbit_event,
                   [{description, "statistics event manager"},
                    {mfa,         {rabbit_sup, start_restartable_child,
                                   [rabbit_event]}},
                    {requires,    external_infrastructure},
                    {enables,     kernel_ready}]}).

-rabbit_boot_step({kernel_ready,
                   [{description, "kernel ready"},
                    {requires,    external_infrastructure}]}).
复制代码

 

  在rabbit.erl模块可以看到rabbitmq是如何通过解析模块元数据并完成.

复制代码
start(normal, []) ->
    case erts_version_check() of
        ok ->
            {ok, Vsn} = application:get_key(rabbit, vsn),
            error_logger:info_msg("Starting RabbitMQ ~s on Erlang ~s~n",
                                  [Vsn, erlang:system_info(otp_release)]),
            {ok, SupPid} = rabbit_sup:start_link(),
            true = register(rabbit, self()),
            print_banner(),
            [ok = run_boot_step(Step) || Step <- boot_steps()],
            io:format("~nbroker running~n"),
            {ok, SupPid};
        Error ->
            Error
    end.
 
run_boot_step({StepName, Attributes}) ->
    Description = case lists:keysearch(description, 1, Attributes) of
                      {value, {_, D}} -> D;
                      false           -> StepName
                  end,
    case [MFA || {mfa, MFA} <- Attributes] of
        [] ->
            io:format("-- ~s~n", [Description]);
        MFAs ->
            io:format("starting ~-60s ...", [Description]),
            [try
                 apply(M,F,A)
             catch
                 _:Reason -> boot_error(Reason, erlang:get_stacktrace())
             end || {M,F,A} <- MFAs],
            io:format("done~n"),
            ok
    end.

boot_steps() ->
    sort_boot_steps(rabbit_misc:all_module_attributes(rabbit_boot_step)).
复制代码

 

  至于rabbit_misc:all_module_attributes的实现,就简单了,遍历加载所有模块的元数据即可:
  
复制代码
\rabbitmq-server-3.0.0\src\rabbit_misc.erl
 
all_module_attributes(Name) ->
    Modules =
        lists:usort(
          lists:append(
            [Modules || {App, _, _}   <- application:loaded_applications(),
                        {ok, Modules} <- [application:get_key(App, modules)]])),
    lists:foldl(
      fun (Module, Acc) ->
              case lists:append([Atts || {N, Atts} <- module_attributes(Module),
                                         N =:= Name]) of
                  []   -> Acc;
                  Atts -> [{Module, Atts} | Acc]
              end
      end, [], Modules).
 
module_attributes(Module) ->
    case catch Module:module_info(attributes) of
        {'EXIT', {undef, [{Module, module_info, _} | _]}} ->
            io:format("WARNING: module ~p not found, so not scanned for boot steps.~n",
                      [Module]),
            [];
        {'EXIT', Reason} ->
            exit(Reason);
        V ->
            V
    end.
复制代码

 

  调用的结果样例:

复制代码
(rabbit@nimbus)5> rabbit_misc:all_module_attributes(rabbit_boot_step).
[{rabbit_policy,[{rabbit_policy,[{description,"policy parameters"},
                                 {mfa,{rabbit_policy,register,[]}},
                                 {requires,rabbit_registry},
                                 {enables,recovery}]}]},
{rabbit_mirror_queue_misc,[{rabbit_mirror_queue_misc,[{description,"HA policy validation"},
                                                       {mfa,{rabbit_registry,register,
                                                                             [policy_validator,<<"ha-mode">>,rabbit_mirror_queue_misc]}},
                                                       {mfa,{rabbit_registry,register,
                                                                             [policy_validator,<<"ha-params">>,
                                                                              rabbit_mirror_queue_misc]}},
                                                       {requires,rabbit_registry},
                                                       {enables,recovery}]}]},
{rabbit_exchange_type_topic,[{rabbit_exchange_type_topic,[{description,"exchange type topic"},
                                                           {mfa,{rabbit_registry,register,
                                                                                 [exchange,<<"topic">>,rabbit_exchange_type_topic]}},
                                                           {requires,rabbit_registry},
........ 
复制代码

 

 

关乎扩展

    如果我们需要编写RabbitMQ的扩展,要让知道rabbit知道我们新定义的模块就需要进行注册,而这个注册就是在模块中添加rabbit_boot_step,在Rabbit启动的适当阶段加入我们的模块,完成注册后在ETS的rabbit_registry表可以看到我们新注册的模块,比如rabbit_exchange_type_recent_history就是我们增加的新exchange.
  
复制代码
  (rabbit@nimbus)4> ets:i(rabbit_registry). 
<1   > {{exchange,'x-recent-history'},rabbit_exchange_type_recent_history}
<2   > {{policy_validator,'ha-mode'},rabbit_mirror_queue_misc}
<3   > {{policy_validator,'ha-params'},rabbit_mirror_queue_misc}
<4   > {{auth_mechanism,'AMQPLAIN'},rabbit_auth_mechanism_amqplain}
<5   > {{exchange,topic},rabbit_exchange_type_topic}
<6   > {{runtime_parameter,policy},rabbit_policy}
<7   > {{auth_mechanism,'PLAIN'},rabbit_auth_mechanism_plain}
<8   > {{exchange,headers},rabbit_exchange_type_headers}
<9   > {{auth_mechanism,'RABBIT-CR-DEMO'},rabbit_auth_mechanism_cr_demo}
<10  > {{exchange,direct},rabbit_exchange_type_direct}
<11  > {{exchange,fanout},rabbit_exchange_type_fanout}
EOT  (q)uit (p)Digits (k)ill /
复制代码
 
  又跟RabbitMQ学了一招,心情大好.
 
 
  今天北京大雪,不禁想起"六出飞花入户时,坐看青竹变琼枝"的佳句,小图一张(小时候有一件一模一样的棉服):
 

 
相关实践学习
消息队列RocketMQ版:基础消息收发功能体验
本实验场景介绍消息队列RocketMQ版的基础消息收发功能,涵盖实例创建、Topic、Group资源创建以及消息收发体验等基础功能模块。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
目录
相关文章
|
缓存 编译器 Go
Golang 语言 vendor 在 GOPATH 和 Modules 中的区别
Golang 语言 vendor 在 GOPATH 和 Modules 中的区别
62 0
|
Linux Python
【python】解决 python 项目移动linux上报 No module named 'xxxx' 错误
【python】解决 python 项目移动linux上报 No module named 'xxxx' 错误
281 0
|
Go
make和new对比|Go主题月
make只可以用来创建map,slice,chan这三个类型,而且它是直接返回类型本身。
164 0
|
存储 算法 Go
一文读懂Go Modules原理
一文读懂Go Modules原理
关于 Mac OSX下运行Python程序调用tools库的问题,ModuleNotFoundError: No module named ‘Tools‘
关于 Mac OSX下运行Python程序调用tools库的问题,ModuleNotFoundError: No module named ‘Tools‘
用go-module作为包管理器搭建go的web服务器
本篇博客主要介绍了如何从零开始,使用Go Module作为依赖管理,基于Gin来一步一步搭建Go的Web服务器。并使用Endless来使服务器平滑重启,使用Swagger来自动生成Api文档。
|
Shell 自然语言处理 网络协议