Can I use an existing OTP application inside anoth

2019-07-19 03:12发布

问题:

I'm building a system that needs to use a previously built OTP application (lets call it X). If I want to build a new OTP application / module, how can I use the application that already exists from a module, for instance?

I assumed I could call start, since it follows the application behaviour, and so I built a minimalistic application Y that has the following code:

y.erl:

-module(y).
-behaviour(application).

start(_StartType, _StartArgs) ->
  io:format("going to call x_app~n"),
  {ok, _} = x_app:start([]),
  io:format("called x_app~n"),
  y:start_link().

stop(_State) ->
  ok = x_app:stop([]),
  ok.

Rebar compiles this code successfully and generates no warnings.
rel/y/bin/y start outputs nothing at all (I hoped to get the output of at least one io:format) rel/y/bin/y stop outputs Node is not running!

回答1:

You need to list application x as a dependent application in your application's .app resource file, or since you're using rebar, in your .app.src file:

{application, your_app,
 [{description,"your application"},
  {vsn, "0.1"},
  {modules,[]},
  {registered, []},
  {mod,{your_app,[]}},
  {env, []},
  {applications,[kernel, stdlib, x]}]}.

Note in the very last line that x is listed as an application dependency. This results in the Erlang application controller ensuring that x is started before it starts your application. And if you're starting your application interactively in an Erlang shell via application:ensure_all_started/1,2 this declaration will ensure that x is started first before your app starts.