構建erlang的app

erlang中構建本身的app是很是方便的,能夠本身定製app,不過這裏只是簡單記錄下erlang下典型的作法。
便是構建otp application。。
構建定製一個application能夠經過xxx.app文件,能夠把app文件放到config文件夾裏面
eg:config/gs.app
首先來看下app文件:
app 文件全稱爲 application resource fileshell

用來指定application的用途&&如何啓動。。。windows

 

[cpp]  view plain  copy
 
  1. {application,"app名字",  
  2. [  
  3. {description,"app描述"},  
  4. {vsn ,"版本號"},  
  5. {id ,Id},%%app id 同 erl -id ID  
  6. {modules,[Modules]},%%app包含的模塊,systools模塊使用它來生成script、tar文件  
  7. {maxP,Num},%%進程最大值  
  8. {maxT,Time},%%app運行時間 單位毫秒  
  9. {registered,[mod]},%%指定app 名字模塊,systools用來解決名字衝突  
  10. {included_applictions ,[XX]},%%指定子 app,只加載,可是不啓動  
  11. {applictions,[xxxx]},%%啓動本身的app前,將會首先啓動此列表的app  
  12. {env,[xxxx]},%%配置app的env,能夠使用application:get_env獲取  
  13. {mod,{xxx,args}},%%指定app啓動模塊,參數,對應本身app的application behavior  
  14. {start_phases,[{xxx,xxx}]]%%指定啓動階段一些操做,對應otp application  start_phase函數  
  15. ]  
  16. }  



 

根據本身的須要定製app文件,這裏個人app文件爲:cookie

 

[cpp]  view plain  copy
 
  1. {  
  2.     application, gs,  
  3.     [  
  4.         {description, "just gs."},  
  5.         {vsn, "1.0a"},  
  6.         {modules, [gs_app,gs_sup]},  
  7.         {registered, [gs_sup]},  
  8.         {mod, {gs_app, []}},  
  9.     {applictions,[kernel,stdlib,sasl]},  
  10.         {env,[{author,"jj"}]},  
  11.         {start_phases, []}  
  12.     ]  
  13. }.  



 


ok,接下來定製otp application:
而且把代碼文件放到src下面app

 

[cpp]  view plain  copy
 
  1. %%src/gs_app.erl  
  2. -module(gs_app).  
  3. -behaviour(application).  
  4. -export([start/2,start/0, stop/1]).  
  5.   
  6. start() ->  
  7.     application:start(gs).  
  8.   
  9. start(_, []) ->  
  10.     io:format("gs start.... ~n"),  
  11.     {ok, Pid} = gs_sup:start_link(),  
  12.     io:format("gs Main Pid is ~p ~n",[Pid]),      
  13.     {ok, Pid}.  
  14.     
  15. stop(_State) ->  
  16.     io:format("gs stop..... ~n").  



 

其中這裏的gs_sup是在app registered模塊,典型otp中的supervisor,固然也能夠本身隨便實現一個模塊。。。函數

 

[cpp]  view plain  copy
 
  1. %%src/gs_sup.erl  
  2. -module(gs_sup).  
  3. -behaviour(supervisor).  
  4. -export([start_link/0,init/1]).  
  5.   
  6. start_link() ->  
  7.     supervisor:start_link({local,?MODULE}, ?MODULE, []).  
  8.   
  9.   
  10. init([]) ->   
  11.     {ok, {     
  12.             {one_for_one, 3, 10},     
  13.             []           
  14.     }}.   



 

爲此,還能夠簡單寫個Emakefile,來實現erl -make,而且把beam文件
輸出到ebin文件夾spa

 

[cpp]  view plain  copy
 
  1. { ["src/*"]  
  2.     , [   
  3.         {outdir, "./ebin"}  
  4.       ]  
  5. }.   


ok,基本上了,爲了管理須要,能夠簡單寫一個script文件來啓動app,在windows下面
能夠這樣作:.net

 

 

[cpp]  view plain  copy
 
  1. start.bat  
  2. cd config/  
  3. erl  -pa ../ebin/ -name jj@test -setcookie abc -boot start_sasl -s gs_app start   
  4.   
  5. cmd  


最後執行bat文件。。。
app運行了,能夠經過application:loaded_applications()。orm

 

 

[cpp]  view plain  copy
 
  1. gs start....  
  2. gs Main Pid is <0.48.0>  
  3.   
  4. =PROGRESS REPORT==== 28-Dec-2012::15:51:46 ===  
  5.          application: gs  
  6.           started_at: jj@test  
  7. Eshell V5.9  (abort with ^G)  
  8. (jj@test)1>  
  9.   
  10. Eshell V5.9  (abort with ^G)  
  11. (jj@test)1> application:loaded_applications().  
  12. [{kernel,"ERTS  CXC 138 10","2.15"},  
  13.  {sasl,"SASL  CXC 138 11","2.2"},  
  14.  {gs,"just gs.","1.0a"},  
  15.  {stdlib,"ERTS  CXC 138 10","1.18"}]  
  16. (jj@test)2>  



 



就這樣,簡單app完成了。。。。
固然,這只是一個很是很是簡單的app。只實現了parent supervisor。blog

相關文章
相關標籤/搜索