erlang - ejabberd - module gen_mod & gen_server with RabbitMq -
i'm developing ejabberd module can send queue directly rabbitmq using ejabberd hook gen_mod
behaviour. queue can sent without issue. however, connection rabbitmq server not persistent.
sample working code per following :
-module(mod_rab). -author('author@domain.com'). -behaviour(gen_mod). -export([start/2, init/2, stop/1, send_available_notice/4]). -define(procname, ?module). -include("ejabberd.hrl"). -include("jlib.hrl"). -include("logger.hrl"). -include("amqp_client.hrl"). start(host, opts) -> ?info_msg("starting mod_rab testing", [] ), register(?procname,spawn(?module, init, [host, opts])), ok. init(host, _opts) -> inets:start(), ssl:start(), ejabberd_hooks:add(set_presence_hook, host, ?module, send_available_notice, 10), ok. stop(host) -> ?info_msg("stopping mod_rab testing", [] ), ejabberd_hooks:delete(set_presence_hook, host, ?module, send_available_notice, 10), ok. send_available_notice(user, server, _resource, _packet) -> {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.
i've tried use gen_server
create persistent connection , should able use when hook triggered. if understand gen_server
correctly, keep connection in state , use during hook callback.
broken code i've tried before :
-module(mod_rab). -author('author@domain.com'). -behaviour(gen_mod). -behaviour(gen_server). -export([start/2, init/2, stop/1, send_available_notice/4]). -export([start_link/1]). -export([handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -define(procname, ?module). -record(state, {channel, connection}). -include("ejabberd.hrl"). -include("jlib.hrl"). -include("logger.hrl"). -include("amqp_client.hrl"). start_link(opts) -> gen_server:start_link({local, ?module}, ?module, opts, []). start(host, opts) -> ?info_msg("starting mod_rab testing", [] ), inets:start(), ssl:start(), ejabberd_hooks:add(set_presence_hook, host, ?module, send_available_notice, 10), {ok, connection} = amqp_connection:start(#amqp_params_network{ host = "localhost"), {ok, channel} = amqp_connection:open_channel(connection), ?info_msg("channel ~s", [channel] ), {ok, #state{ connection = connection, channel = channel}}, %% register(?procname,spawn(?module, init, [host, opts])), ok. init(host, _opts) -> ok. stop(host) -> ?info_msg("stopping mod_rab testing", [] ), ejabberd_hooks:delete(set_presence_hook, host, ?module, send_available_notice, 10), ok. send_available_notice(user, server,_resource, _packet) -> ?info_msg("hook", [] ), amqp_channel:call(#state.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"), ?info_msg("sent", [] ), %% ok = amqp_channel:close(channel), %% ok = amqp_connection:close(connection), ok. handle_call(_request, _from, state) -> reply = ok, {reply, reply, state}. handle_cast(send_available_notice, state) -> {noreply, state}. handle_info(_info, state) -> {noreply, state}. terminate(_reason, _state) -> ok. code_change(_oldvsn, state, _extra) -> {ok, state}.
thanks help
Comments
Post a Comment