构建erlang的app

来源:互联网 发布:mysql no字段 编辑:程序博客网 时间:2024/05/10 12:54

erlang中构建自己的app是非常方便的,可以自己定制app,不过这里只是简单记录下erlang下典型的做法。
即是构建otp application。。
构建定制一个application可以通过xxx.app文件,可以把app文件放到config文件夹里面
eg:config/gs.app
首先来看下app文件:
app 文件全称为 application resource file

用来指定application的用途&&如何启动。。。

[cpp] view plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  1. { ["src/*"]  
  2.     , [   
  3.         {outdir, "./ebin"}  
  4.       ]  
  5. }.   

ok,基本上了,为了管理需要,可以简单写一个script文件来启动app,在windows下面
可以这样做:
[cpp] view plaincopy
  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()。
[cpp] view plaincopy
  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。
1 0