Can I use an existing OTP application inside another application or module?

扶醉桌前 提交于 2019-12-12 19:56:26

问题


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.



来源:https://stackoverflow.com/questions/36251608/can-i-use-an-existing-otp-application-inside-another-application-or-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!