从rabbit模块启动 看rabbit.erl的 start()-> 函数
start() ->
try
ok = prepare(),
ok = rabbit_misc:start_applications(?APPS)
after
%%give the error loggers some time to catch up
timer:sleep(100)
end.
prepare()准备环境 然后调用rabbit_misc:start_applications(?APPS)
-define(APPS, [os_mon, mnesia, rabbit]).
看看rabbit_misc:start_applications
start_applications(Apps) ->
manage_applications(fun lists:foldl/3,
fun application:start/1,
fun application:stop/1,
already_started,
cannot_start_application,
Apps).
start_applications调用的是manage_applications 传递的参数是
fun lists:foldl/3,
fun application:start/1,
fun application:stop/1,
already_started,
cannot_start_application,
[os_mon, mnesia, rabbit]
这个比较精妙,仔细看看!分别对应到Iterate, Do, Undo, SkipError, ErrorTag, Apps,如下
manage_applications(Iterate, Do, Undo, SkipError, ErrorTag, Apps) ->
Iterate(fun (App, Acc) ->
case Do(App) of
ok -> [App | Acc];
{error, {SkipError, _}} -> Acc;
{error, Reason} ->
lists:foreach(Undo, Acc),
throw({error, {ErrorTag, App, Reason}})
end
end, [], Apps),
ok.
实际如下
manage_applications(lists:foldl/3, application:start/1, application:stop, already_started, cannot_start_application, [os_mon, mnesiarabbit]) ->
lists:foldl(fun (os_mon, Acc) ->
case application:start(os_mon) of
ok -> [os_mon | Acc];
{error, {already_started, _}} -> Acc;
{error, Reason} ->
lists:foreach(application:stop, Acc),
throw({error, {cannot_start_application, os_mon, Reason}})
end
end, [], [os_mon, mnesia, rabbit]),
ok. 依次启动os_mon, mnesia, rabbit
使用application:start(rabbit) 会调用rabbit:start()/2
start() ->
try
ok = prepare(),
ok = rabbit_misc:start_applications(?APPS)
after
%%give the error loggers some time to catch up
timer:sleep(100)
end.
prepare()准备环境 然后调用rabbit_misc:start_applications(?APPS)
-define(APPS, [os_mon, mnesia, rabbit]).
看看rabbit_misc:start_applications
start_applications(Apps) ->
manage_applications(fun lists:foldl/3,
fun application:start/1,
fun application:stop/1,
already_started,
cannot_start_application,
Apps).
start_applications调用的是manage_applications 传递的参数是
fun lists:foldl/3,
fun application:start/1,
fun application:stop/1,
already_started,
cannot_start_application,
[os_mon, mnesia, rabbit]
这个比较精妙,仔细看看!分别对应到Iterate, Do, Undo, SkipError, ErrorTag, Apps,如下
manage_applications(Iterate, Do, Undo, SkipError, ErrorTag, Apps) ->
Iterate(fun (App, Acc) ->
case Do(App) of
ok -> [App | Acc];
{error, {SkipError, _}} -> Acc;
{error, Reason} ->
lists:foreach(Undo, Acc),
throw({error, {ErrorTag, App, Reason}})
end
end, [], Apps),
ok.
实际如下
manage_applications(lists:foldl/3, application:start/1, application:stop, already_started, cannot_start_application, [os_mon, mnesiarabbit]) ->
lists:foldl(fun (os_mon, Acc) ->
case application:start(os_mon) of
ok -> [os_mon | Acc];
{error, {already_started, _}} -> Acc;
{error, Reason} ->
lists:foreach(application:stop, Acc),
throw({error, {cannot_start_application, os_mon, Reason}})
end
end, [], [os_mon, mnesia, rabbit]),
ok. 依次启动os_mon, mnesia, rabbit
使用application:start(rabbit) 会调用rabbit:start()/2