%% @author Rolong<rolong@vip.qq.com> -module(bank_sup). -behaviour(supervisor). -export([start_link/0]). -export([init/1]). %% 定义一个启动本监督树的API start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []). init([]) -> %% 启动2个子进程 BankCenterSpec = { center %% 指定本进程(在子进程中唯一)的名称 ,{bank_center, start_link, []} %% 进程启动函数:{M,F,A} ,transient %% 重启策略:permanent | transient | temporary ,5000 %% 关闭方式:brutal_kill | int()>0 | infinity ,worker %% 进程类型:worker 或 supervisor ,[bank_center] %% 回调模块名称:[Module] | dynamic }, BankCenterSpec2 = { center2 ,{bank_center2, start_link, []} ,transient ,5000 ,worker ,[bank_center2] }, %% {ok, {{RestartStrategy, MaxR, MaxT}, [ChildSpec]}} {ok, {{one_for_one, 5, 30}, [BankCenterSpec, BankCenterSpec2]}}.
handle_info(error, Money) -> %% Error Return ... {ok, Money}; handle_info(force_stop, Money) -> {stop, "Force STOP", Money}; handle_info(normal_stop, Money) -> {stop, normal, Money}; handle_info(shutdown, Money) -> {stop, shutdown, Money}; handle_info({shutdown,Term}, Money) -> {stop, {shutdown,Term}, Money};
Eshell V5.10.4 (abort with ^G) 1> appmon:start(). {ok,<0.40.0>} 2> bank_center ! normal_stop. ##[<0.38.0>bank_center:59] terminate: normal normal_stop ##[<0.51.0>bank_center:23] Start bank_center ... 3> bank_center ! normal_stop. ##[<0.51.0>bank_center:59] terminate: normal normal_stop ##[<0.53.0>bank_center:23] Start bank_center ... 4> bank_center ! normal_stop. ##[<0.53.0>bank_center:59] terminate: normal normal_stop ##[<0.55.0>bank_center:23] Start bank_center ... 5> bank_center2 ! normal_stop. ##[<0.39.0>bank_center2:59] terminate: normal normal_stop ##[<0.57.0>bank_center2:23] Start bank_center2 ... 6> bank_center2 ! normal_stop. ##[<0.57.0>bank_center2:59] terminate: normal normal_stop ##[<0.59.0>bank_center2:23] Start bank_center2 ... 7> bank_center2 ! normal_stop. ##[<0.59.0>bank_center2:59] terminate: normal ##[<0.55.0>bank_center:59] terminate: shutdown normal_stop 8> ##[<0.34.0>bank_app:16] Stop bank_app! =INFO REPORT==== 17-Jan-2015::16:19:15 === application: bank exited: shutdown type: temporary
Eshell V5.10.4 (abort with ^G) 1> bank_center ! error. ##[<0.38.0>bank_center:59] terminate: {bad_return_value,{ok,0}} error ##[<0.40.0>bank_center:23] Start bank_center ... 2> =ERROR REPORT==== 17-Jan-2015::17:09:15 === ** Generic server bank_center terminating ** Last message in was error ** When Server state == 0 ** Reason for termination == ** {bad_return_value,{ok,0}} 2> bank_center ! force_stop. ##[<0.40.0>bank_center:59] terminate: "Force STOP" =ERROR REPORT==== 17-Jan-2015::17:09:28 === ** Generic server bank_center terminating ** Last message in was force_stop ** When Server state == 0 ** Reason for termination == ** "Force STOP" force_stop ##[<0.42.0>bank_center:23] Start bank_center ... 3> 3> bank_center ! normal_stop. ##[<0.42.0>bank_center:59] terminate: normal normal_stop 4> 4> whereis(bank_center). undefined 5> supervisor:restart_child(bank_sup, center). ##[<0.46.0>bank_center:23] Start bank_center ... {ok,<0.46.0>} 6> whereis(bank_center). <0.46.0> 7> bank_center ! shutdown. ##[<0.46.0>bank_center:59] terminate: shutdown shutdown 8> whereis(bank_center). undefined 9> supervisor:restart_child(bank_sup, center). ##[<0.51.0>bank_center:23] Start bank_center ... {ok,<0.51.0>} 10> whereis(bank_center). <0.51.0> 11> bank_center ! {shutdown, test}. ##[<0.51.0>bank_center:59] terminate: {shutdown,test} {shutdown,test} 12> whereis(bank_center). undefined
由上可见,进程退出的Reason为normal或shutdown或{shutdown, Term}时,bank_center不会被重启,其他Reason时会被重启。
4、完整演示代码下载
地址:http://download.csdn.net/detail/u011471961/8371361
Erlang入门:构建application练习5(监督树)
原文:http://blog.csdn.net/zhongruixian/article/details/42806659