RabbitMQ erlang客户端,来自C system()调用内的escript

时间:2012-12-11 18:58:17

标签: erlang rabbitmq

我怎样才能做到如下:

system.c

#include<stdio.h>

int main()

{
        system("/home/noob/send.erl");

        return(0);
}

使用erlang客户端rabbitmq-tutorials/erlang/

从RabbitMQ教程中

send.erl

#!/usr/bin/env escript
%%! -pz ./deps/amqp_client ./deps/rabbit_common ./deps/amqp_client/ebin ./deps/rabbit_common/ebin

-include_lib("deps/amqp_client/include/amqp_client.hrl").

main(_) ->
    {ok, Connection} = 
    amqp_connection:start(#amqp_params_network{host = "localhost"}),
    {ok, Channel} = amqp_connection:open_channel(Connection),

    amqp_channel:call(Channel, #'queue.declare'{queue = <<"hello">>}),
    amqp_channel:cast(Channel, 
             #'basic.publish'{
               exchange = <<"">>,
               routing_key = <<"hello">>}, 
              #amqp_msg{payload = <<"Hello World!">>}),
    io:format("[x] Sent 'Hello World!'~n"),
    ok = amqp_channel:close(Channel),
    ok = amqp_connection:close(Connection),
    ok.

然后运行:

gcc system.c -o system

noob$./system

输出:

send.erl:20: can't find include lib "rabbit_common/include/rabbit.hrl"
send.erl:21: can't find include lib "rabbit_common/include/rabbit_framing.hrl"
escript: There were compilation errors.

所以我在amqp_client.hrl

上执行此操作
-include_lib("../../rabbit_common/include/rabbit.hrl").
-include_lib("../../rabbit_common/include/rabbit_framing.hrl").

然后运行noob$./system

和Boom:

escript: exception error: undefined function amqp_connection:start/1
  in function  erl_eval:do_apply/6 (erl_eval.erl, line 572)
  in call from erl_eval:expr/5 (erl_eval.erl, line 367)
  in call from escript:eval_exprs/5 (escript.erl, line 836)
  in call from erl_eval:local_func/5 (erl_eval.erl, line 470)
  in call from escript:interpret/4 (escript.erl, line 754)
  in call from escript:start/1 (escript.erl, line 277)
  in call from init:start_it/1

因此,似乎escript在编译时遇到PATHS问题,并且在C的system()调用中调用它。

我知道如何才能实现这一目标?

TIA

1 个答案:

答案 0 :(得分:0)

感谢question

我能够在dinamically添加额外的PATH目录,以便escript可以找到运行脚本所需的erlang客户端代码。

true = code:add_pathz(filename:dirname(escript:script_name()) ++ "/deps/amqp_client/ebin"),
true = code:add_pathz(filename:dirname(escript:script_name()) ++ "/deps/rabbit_common/ebin"),

我仍然不太明白为什么在shell中使用-pz r​​elative paths选项调用escript并且在C的system()中无法工作。