您的位置:首页 > 移动开发

构建erlang的app

2017-10-09 10:49 295 查看
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

{application,"app名字",  

[  

{description,"app描述"},  

{vsn ,"版本号"},  

{id ,Id},%%app id 同 erl -id ID  

{modules,[Modules]},%%app包含的模块,systools模块使用它来生成script、tar文件  

{maxP,Num},%%进程最大值  

{maxT,Time},%%app运行时间 单位毫秒  

{registered,[mod]},%%指定app 名字模块,systools用来解决名字冲突  

{included_applictions ,[XX]},%%指定子 app,只加载,但是不启动  

{applictions,[xxxx]},%%启动自己的app前,将会首先启动此列表的app  

{env,[xxxx]},%%配置app的env,可以使用application:get_env获取  

{mod,{xxx,args}},%%指定app启动模块,参数,对应自己app的application behavior  

{start_phases,[{xxx,xxx}]]%%指定启动阶段一些操作,对应otp application  start_phase函数  

]  

}  

根据自己的需要定制app文件,这里我的app文件为:

[cpp] view
plain copy

{  

    application, gs,  

    [  

        {description, "just gs."},  

        {vsn, "1.0a"},  

        {modules, [gs_app,gs_sup]},  

        {registered, [gs_sup]},  

        {mod, {gs_app, []}},  

    {applictions,[kernel,stdlib,sasl]},  

        {env,[{author,"jj"}]},  

        {start_phases, []}  

    ]  

}.  

ok,接下来定制otp application:

并且把代码文件放到src下面

[cpp] view
plain copy

%%src/gs_app.erl  

-module(gs_app).  

-behaviour(application).  

-export([start/2,start/0, stop/1]).  

  

start() ->  

    application:start(gs).  

  

start(_, []) ->  

    io:format("gs start.... ~n"),  

    {ok, Pid} = gs_sup:start_link(),  

    io:format("gs Main Pid is ~p ~n",[Pid]),      

    {ok, Pid}.  

    

stop(_State) ->  

    io:format("gs stop..... ~n").  

其中这里的gs_sup是在app registered模块,典型otp中的supervisor,当然也可以自己随便实现一个模块。。。

[cpp] view
plain copy

%%src/gs_sup.erl  

-module(gs_sup).  

-behaviour(supervisor).  

-export([start_link/0,init/1]).  

  

start_link() ->  

    supervisor:start_link({local,?MODULE}, ?MODULE, []).  

  

  

init([]) ->   

    {ok, {     

            {one_for_one, 3, 10},     

            []           

    }}.   

为此,还可以简单写个Emakefile,来实现erl -make,并且把beam文件

输出到ebin文件夹

[cpp] view
plain copy

{ ["src/*"]  

    , [   

        {outdir, "./ebin"}  

      ]  

}.   

ok,基本上了,为了管理需要,可以简单写一个script文件来启动app,在windows下面
可以这样做:

[cpp] view
plain copy

start.bat  

cd config/  

erl  -pa ../ebin/ -name jj@test -setcookie abc -boot start_sasl -s gs_app start   

  

cmd  

最后执行bat文件。。。
app运行了,可以通过application:loaded_applications()。

[cpp] view
plain copy

gs start....  

gs Main Pid is <0.48.0>  

  

=PROGRESS REPORT==== 28-Dec-2012::15:51:46 ===  

         application: gs  

          started_at: jj@test  

Eshell V5.9  (abort with ^G)  

(jj@test)1>  

  

Eshell V5.9  (abort with ^G)  

(jj@test)1> application:loaded_applications().  

[{kernel,"ERTS  CXC 138 10","2.15"},  

 {sasl,"SASL  CXC 138 11","2.2"},  

 {gs,"just gs.","1.0a"},  

 {stdlib,"ERTS  CXC 138 10","1.18"}]  

(jj@test)2>  

就这样,简单app完成了。。。。

当然,这只是一个非常非常简单的app。只实现了parent supervisor。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: