Erlang应用程序启动/ 2不会执行

gex*_*tra 1 erlang erlang-otp

这是我能提出的最基本的应用程序,我无法理解为什么应用程序模块函数的start/2不会记录消息.这是我做的:

1)app配置文件(test_app.app):

{application,test_app,
             [{description,"Test App"},
              {vsn,0.9},
              {applications,[kernel,stdlib]},
              {modules,[test_app,log_utils]},
              {registered,[test_app]}]}.
Run Code Online (Sandbox Code Playgroud)

2)app模块(test_app.erl):

-module(test_app).
-behaviour(application).
-export([start/2, stop/1]).
-export([test/0]).

start(_Type, _Args) ->
    log_utils:info("here at APP START 1"),
    master_sup:start_link({10,20,30}). 

stop(_State) ->
    ok.

test() ->
    log_utils:info("here at APP START 2"),
    master_sup:start_link({10,20,30}).
Run Code Online (Sandbox Code Playgroud)

然后我编译并测试如下:

1> application:start(test_app).
ok
2> test_app:test().
==INFO REPORT==== 27-Oct-2013::19:53:29 ===
"here at APP START 2"
Run Code Online (Sandbox Code Playgroud)

期望的是应用程序:start(test_app)将执行start/2函数并像test/0函数一样记录消息.

事实上,我有一个更复杂的例子,我启动了一个主管,但同样地,我在app模块中创建的API会导致错误,表明start_link不起作用.如果我调用启动主管的测试功能,那么它可以工作.

rvi*_*ing 7

您需要为该.app文件提供一个额外选项,该选项提供应用程序回调模块并启动args.没有隐式回调模块名称,如果没有给出,则不会启动任何进程.选项是{mod,{CallBackMod,StartArgs}}这样整个.app文件将变为:

{application,test_app,
 [{description,"Test App"},
  {vsn,0.9},
  {applications,[kernel,stdlib]},
  {modules,[test_app,log_utils]},
  {registered,[test_app]},
  {mod,{test_app,[]}}]}.
Run Code Online (Sandbox Code Playgroud)

test_app在您的情况下,第二个元素是应用程序的名称,而不是回调模块; 他们不必是一样的.如果给出了回调,则Mod:start/2在应用程序启动和Mod:stop/1应用程序停止时将调用.

请注意,应用程序在启动时不必运行任何进程,例如stdlib应用程序不会.

您可以在应用程序中找到更好的描述.