我可以在其他应用程序或模块中使用现有的OTP应用程序吗? [英] Can I use an existing OTP application inside another application or module?

查看:190
本文介绍了我可以在其他应用程序或模块中使用现有的OTP应用程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个需要使用以前构建的OTP应用程序的系统(可以称之为X)。如果我要构建一个新的OTP应用程序/模块,那么我可以如何使用模块中已经存在的应用程序?



我假设我可以调用code> start ,因为它遵循应用程序行为,因此我构建了一个简约应用程序Y,其中包含以下代码:


y.erl:

  -module(y)。 
-behaviour(应用程序)。

start(_StartType,_StartArgs) - >
io:format(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可以成功编译此代码,不生成任何警告。

rel / y / bin / y start 根本没输出(我希望得到至少一个 io:format 的输出)
rel / y / bin / y stop 输出节点没有运行!

解决方案

您需要列出应用程序 x 作为您的应用程序的 .app 资源文件,或者因为你是在 .app.src 文件中使用 rebar

  {application,your_app,
[{description,your application},
{vsn,0.1},
{modules, ]},
{registered,[]},
{mod,{your_app,[]}},
{env,[]},
{ stdlib,x]}]}。

请注意最后一行 x 被列为应用依赖关系。这导致Erlang应用程序控制器确保在开始应用程序之前启动 x 。如果您通过 <$ c交互式启动应用程序在Erlang shell中$ c> application:ensure_all_started / 1,2 此声明将确保您的应用程序启动前首先启动 x 。 p>

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!

解决方案

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.

这篇关于我可以在其他应用程序或模块中使用现有的OTP应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆