构建erlang的app
erlang中构建自己的app是非常方便的,可以自己定制app,不过这里只是简单记录下erlang下典型的做法。
即是构建otp application。。
构建定制一个application可以通过xxx.app文件,可以把app文件放到config文件夹里面
eg:config/gs.app
首先来看下app文件:
app 文件全称为 application resource file
用来指定application的用途&&如何启动。。。
- {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函数
- ]
- }
{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文件为:
- {
- 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, []}
- ]
- }.
{
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下面
- %%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").
%%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},
- []
- }}.
%%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文件夹
{ ["src/*"]
, [
{outdir, "./ebin"}
]
}.
ok,基本上了,为了管理需要,可以简单写一个script文件来启动app,在windows下面
可以这样做:
- start.bat
- cd config/
- erl -pa ../ebin/ -name jj@test -setcookie abc -boot start_sasl -s gs_app start
-
- cmd
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()。
- 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>
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。
即是构建otp application。。
构建定制一个application可以通过xxx.app文件,可以把app文件放到config文件夹里面
eg:config/gs.app
首先来看下app文件:
app 文件全称为 application resource file
用来指定application的用途&&如何启动。。。
- {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函数
- ]
- }
{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文件为:
- {
- 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, []}
- ]
- }.
{
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下面
- %%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").
%%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},
- []
- }}.
%%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文件夹
{ ["src/*"]
, [
{outdir, "./ebin"}
]
}.
ok,基本上了,为了管理需要,可以简单写一个script文件来启动app,在windows下面
可以这样做:
- start.bat
- cd config/
- erl -pa ../ebin/ -name jj@test -setcookie abc -boot start_sasl -s gs_app start
- cmd
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()。
- 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>
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。