標籤:
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 [email protected] -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: [email protected]
- Eshell V5.9 (abort with ^G)
- ([email protected])1>
-
- Eshell V5.9 (abort with ^G)
- ([email protected])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"}]
- ([email protected])2>
就這樣,簡單app完成了。。。。
當然,這隻是一個非常非常簡單的app。只實現了parent supervisor。
構建erlang的app