Erlang/OTP 构建 Application

在Erlang/OTP ,Application表示作为一个单元,可以启动和停止,执行一些特定功能​​的组件,并可以在其它系统中重新使用。Application控制器的模块接口,是在每一个Erlang运行时系统启动的进程,并包含用于控制Application(例如启动和停止Application),以及访问Application的信息(例如配置参数)的功能。

Erlang/OTP Application基本结构:

一个 Application 至少包含了3部分的内容:应用模块、监督者模块、资源文件。

应用模块(test_app.erl) :

[plain]  view plain  copy
  1. -module(test_app).  
  2.   
  3. -behaviour(application).  
  4.   
  5. -export([start/2, stop/1]).  
  6.   
  7. start(_Type, StartArgs) ->  
  8.     io:format("test app start~n"),  
  9.     case test_sup:start_link(StartArgs) of  
  10.         {ok, Pid} ->  
  11.             {ok, Pid};  
  12.         Error ->  
  13.             Error  
  14.     end.  
  15.   
  16. stop(_State) ->  
  17.     ok.  

监督者模块(test_sup.erl):

[plain]  view plain  copy
  1. -module(test_sup).  
  2.   
  3. -behaviour(supervisor).  
  4.   
  5. -export([start_link/1, init/1]).  
  6.   
  7. start_link(_) ->  
  8.     io:format("test sup start link~n"),  
  9.     supervisor:start_link({local, ?MODULE}, ?MODULE, []).  
  10.   
  11.   
  12. init([]) ->  
  13.     io:format("test sup init~n"),  
  14.     {ok,{  
  15.         {one_for_one, 1, 60},  
  16.         []}  
  17.     }.  

资源文件(test.app) :

[plain]  view plain  copy
  1. {application,test,  
  2.    [{description,"Test application"},  
  3.     {vsn,"1.0.0"},  
  4.     {modules,[test_app,test_sup]},  
  5.     {registered,[test_app]},  
  6.     {mod,{test_app,[]}},  
  7.     {env,[]},  
  8.     {applications,[kernel,stdlib]}]}.  

erl编译以上代码后,执行命令启动这个Application

[plain]  view plain  copy
  1. 1> application:start(test).  
  2. test app start  
  3. test sup start link  
  4. test sup init  
  5. ok  
查看这个Application有没有加载
[plain]  view plain  copy
  1. 2> application:loaded_applications().   
  2. [{kernel,"ERTS  CXC 138 10","2.16.2"},   
  3. {stdlib,"ERTS  CXC 138 10","1.19.2"},   
  4. {test,"Test application","1.0.0"}]  

Erlang/OTP Application启动流程:

1、当erlang 执行application:start(test) 时,erlang会查找工作目录有没有 test.app 这个资源文件,没找到就报错,如果找到,就按照 test.app 这个文件的指示,启动 test 应用。

[plain]  view plain  copy
  1. %% 比较完整的资源文件:  
  2. {application,test,                      % 名称  
  3.    [{description,"Test application"},   % 描述  
  4.     {vsn, "1.0.0"},                     % 版本  
  5.     {id, Id},                           % id 同 erl -id ID  
  6.     {modules, [test_app,test_sup]},     % 所有模块,systools用来生成script/tar文件  
  7.     {maxP, Num},                        % 最大进程数  
  8.     {maxT, Time},                       % 运行时间 单位毫秒  
  9.     {registered, [test_app]},           % 指定名称,systools用来解决名字冲突  
  10.     {included_applictions, []},         % 指定子app,加载但不启动  
  11.     {mod, {test_app,[]}},               % 启动模块,[]为参数  
  12.     {env, []},                          % 配置env,可以使用application:get_env获取  
  13.     {applications,[kernel,stdlib]}]}.   % 依赖项,启动app前,必须有启动的app  

2、这里重点看 {mod, {test_app,[]}} 参数,意思是告诉 erlang 要调用应用模块(test_app)的start/2 函数。

[plain]  view plain  copy
  1. -module(test_app).  
  2.   
  3. start(_Type, StartArgs) ->    
  4.     io:format("test app start~n"),    
  5.     case test_sup:start_link(StartArgs) of    
  6.         {ok, Pid} ->    
  7.             {ok, Pid};    
  8.         Error ->    
  9.             Error    
  10.     end.    

3、紧接着,到了监督者模块(test_sup),监督者模块启动了一个监督者进程,该进程会回调监督者模块的 init/1 函数,根据这个函数的返回值来启动子进程。

[plain]  view plain  copy
  1. %% 比较完整的监督者模块 init/1 函数,仅参考,下篇再介绍参数意义  
  2. init([]) ->  
  3.   {ok,  
  4.     {{one_for_one, 1, 60},         % Strategy = {How, Max, Within}  
  5.        [{ test_handler_worker,       % Id       = internal id  
  6.           {test_server, start, []},  % StartFun = {M, F, A}  
  7.           permanent,                 % Restart  = permanent | transient | temporary  
  8.           2000,                      % Shutdown = brutal_kill | int() >= 0 | infinity  
  9.           worker,                    % Type     = worker | supervisor  
  10.           [test_server]              % Modules  = [Module] | dynamic  
  11.           }]  
  12.      }  
  13.    }.  

文章完整例子下载 http://download.csdn.net/detail/cwqcwk1/6398337

猜你喜欢

转载自blog.csdn.net/boiled_water123/article/details/80643584