erlang中构建本身的app是很是方便的,能够本身定制app,不过这里只是简单记录下erlang下典型的作法。
便是构建otp application。。
构建定制一个application能够经过xxx.app文件,能够把app文件放到config文件夹里面
eg:config/gs.app
首先来看下app文件:
app 文件全称为 application resource fileshell
用来指定application的用途&&如何启动。。。windows
- {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文件为:cookie
- {
- 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下面app
- %%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,固然也能够本身随便实现一个模块。。。函数
- %%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文件夹spa
- { ["src/*"]
- , [
- {outdir, "./ebin"}
- ]
- }.
ok,基本上了,为了管理须要,能够简单写一个script文件来启动app,在windows下面
能够这样作:.net
- 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()。orm
- 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。blog