将在本文中列出gen_serverand
、
supervisor
和
application
的完整模板清单。这些模板内建于Emacs
模式,这样便于直接使用和学习。
一:通用服务器模版
gen_server_template.full
%%% gen_server_template.full
%%%-----------------------------------------------------------------------
%%% @作者XXX<me@hostname.1ocal>
%%% @版权所有 (C) 20xx, XXX
%%% @doc
%%%
%%% @end
%%% 创建于:20xx年x月xx日 作者XXX <me@hostname.1ocal>
%%%-----------------------------------------------------------------------
-module().
-behaviour(gen_server).
%% API
-export([start_link/0]).
%% gen_server回调函数
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
-record(state, {}).
%%%=========================================================================
%%% API
%%%=========================================================================
%%--------------------------------------------------------------------------
%% @doc
%% 启动服务器
%%
%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
%% @end
%%--------------------------------------------------------------------------
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
%%%=========================================================================
%%% gen_server回调函数
%%%=========================================================================
%%--------------------------------------------------------------------------
%% @private
%% @doc
%% 初始化服务器
%%
%% @spec init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% @end
%%-------------------------------------------------------------------------
init([]) ->
{ok, #state{}}.
%%-------------------------------------------------------------------------
%% @private
%% @doc
%% 处理调用消息
%%
%% @spec handle_call(Request, From, State) ->
%% {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% 处理播发消息
%%
%% @spec handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% 处理所有非调用/播发的消息
%%
%% @spec handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_info(_Info, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% 这个函数是在某个gen_server即将终止时调用的。
%% 它应当是Module:init/1的逆操作,并进行必要的清理。
%% 当它返回时,gen_server终止并生成原因Reason。它的返回值会被忽略
%%
%% @spec terminate(Reason, State) -> void()
%% @end
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
ok.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% 在代码更改时转换进程状态
%%
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%% @end
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%===================================================================
%%% 内部函数
%%%===================================================================
二:监控器模版:
supervisor_template.full
%% supervisor_template.full
%%%-------------------------------------------------------------------
%%% @作者XXX <me@hostname.local>
%%% @版权所有 (C) 20xx, XXX
%%% @doc
%%%
%%% @end
%%% 创建于:20xx年x月xx日 作者XXX <me@hostname.local>
%%%-------------------------------------------------------------------
-module().
-behaviour(supervisor).
%% API
-export([start_link/0]).
%% 监控器回调函数
-export([init/1]).
-define(SERVER, ?MODULE).
%%%===================================================================
%%% API函数
%%%===================================================================
%%--------------------------------------------------------------------
%% @doc
%% 启动监控器
%%
%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
%% @end
%%--------------------------------------------------------------------
start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
%%%===================================================================
%%% 监控器回调函数
%%%===================================================================
%%--------------------------------------------------------------------
%% @private
%% @doc
%% 每当supervisor:start_link/[2,3]启动一个监控器时,新进程就会调用这个函数来确定重启策略、
%% 最大重启频率和子进程规范
%% specifications.
%%
%% @spec init(Args) -> {ok, {SupFlags, [ChildSpec]}} |
%% ignore |
%% {error, Reason}
%% @end
%%--------------------------------------------------------------------
init([]) ->
RestartStrategy = one_for_one,
MaxRestarts = 1000,
MaxSecondsBetweenRestarts = 3600,
SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts},
Restart = permanent,
Shutdown = 2000,
Type = worker,
AChild = {'AName', {'AModule', start_link, []},
Restart, Shutdown, Type, ['AModule']},
{ok, {SupFlags, [AChild]}}.
%%%===================================================================
%%% 内部函数
%%%===================================================================
三:应用程序模版:
application_template.full
%% application_template.full
%%%-------------------------------------------------------------------
%%% @作者XXX <me@hostname.local>
%%% @版权所有 (C) 20xx, XXX
%%% @doc
%%%
%%% @end
%%% 创建于:20xx年x月xx日 作者XXX <me@hostname.local>
%%%-------------------------------------------------------------------
-module().
-behaviour(application).
%% 应用程序回调函数
-export([start/2, stop/1]).
%%%===================================================================
%%% 应用程序回调函数
%%%===================================================================
%%--------------------------------------------------------------------
%% @private
%% @doc
%% 用application:start/[1,2]启动一个应用程序时会调用这个函数。
%% 它应当启动该应用程序的各个进程。
%% 如果应用程序的结构遵循OTP的监控树设计原则,此函数就会启动该树的顶级监控进程。
%%
%% @spec start(StartType, StartArgs) -> {ok, Pid} |
%% {ok, Pid, State} |
%% {error, Reason}
%% StartType = normal | {takeover, Node} | {failover, Node}
%% StartArgs = term()
%% @end
%%--------------------------------------------------------------------
start(_StartType, _StartArgs) ->
case 'TopSupervisor':start_link() of
{ok, Pid} ->
{ok, Pid};
Error ->
Error
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% 这个函数是在应用程序停止时调用的。
%% 它应当是Module:start/2的逆操作,并进行必要的清理。它的返回值会被忽略。
%%
%% @spec stop(State) -> void()
%% @end
%%--------------------------------------------------------------------
stop(_State) ->
ok.
%%%===================================================================
%%% 内部函数
%%%===================================================================