構建erlang的app

來源:互聯網
上載者:User

標籤:

erlang中構建自己的app是非常方便的,可以自己定製app,不過這裡只是簡單記錄下erlang下典型的做法。
即是構建otp application。。
構建定製一個application可以通過xxx.app檔案,可以把app檔案放到config檔案夾裡面
eg:config/gs.app
首先來看下app檔案:
app 檔案全稱為 application resource file

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

 

[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檔案為:

 

[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下面

 

[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檔案夾

 

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


ok,基本上了,為了管理需要,可以簡單寫一個script檔案來啟動app,在windows下面
可以這樣做:

 

 

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


最後執行bat檔案。。。
app運行了,可以通過application:loaded_applications()。

 

 

[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: [email protected]  
  7. Eshell V5.9  (abort with ^G)  
  8. ([email protected])1>  
  9.   
  10. Eshell V5.9  (abort with ^G)  
  11. ([email protected])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. ([email protected])2>  



 



就這樣,簡單app完成了。。。。
當然,這隻是一個非常非常簡單的app。只實現了parent supervisor。

 

構建erlang的app

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.