我是erlang的新手,正在开发基于erlang的重大项目。
我的用例是将确认消息,已发布消息和已发送消息从MQTT代理推送到Rabbitmq服务器。
我在我的erlang文件中定义了三个函数,每个函数都想与Rabbitmq服务器建立连接。每个函数都能够与Rabbitmq创建一个具有较少命中数的连接,但是当大量命中达到这三个函数时,Rabbitmq由于与Rabbitmq的大量连接而停止响应。在回复中,我收到了超时错误。
我想要的是想要创建Rabbitmq的全局连接,并在所有三个函数中使用相同的连接对象。
以下显示三个功能: -
on_message_publish(Message, _Env) ->
%% Getting a Raw Message
io:format("PUBLISHING"),
Val=element(12,Message),
%% Making a Connection with RabbitMQ
{ok, Connection} = amqp_connection:start(#'amqp_params_network'{}),
%% Creating Channel
{ok, Channel} = amqp_connection:open_channel(Connection),
%% Declaring Queue
% Declare = #'queue.declare'{queue = <<"erlang_message">>,durable = true},
% #'queue.declare_ok'{queue = Queue} = amqp_channel:call(Channel, Declare),
%% Creating Payload Message
Payload = list_to_binary(["published|$&|",Val]),
%% Published a Message
Publish = #'basic.publish'{exchange = <<"celery">>},
amqp_channel:cast(Channel, Publish, #amqp_msg{payload = Payload}),
%% Closing Channel
amqp_channel:close(Channel),
%% Closing Connection
amqp_connection:close(Connection),
{ok, Message}.
on_message_delivered(ClientId, Message, _Env) ->
Val=element(12,Message),
%% Making a Connection with RabbitMQ
{ok, Connection} = amqp_connection:start(#'amqp_params_network'{}),
%% Creating Channel
{ok, Channel} = amqp_connection:open_channel(Connection),
%% Declaring Queue
% Declare = #'queue.declare'{queue = <<"erlang_message">>,durable = true},
% #'queue.declare_ok'{queue = Queue} = amqp_channel:call(Channel, Declare),
%% Creating Payload Message
Payload = list_to_binary(["published|$&|",Val]),
%% Published a Message
Publish = #'basic.publish'{exchange = <<"celery">>},
amqp_channel:cast(Channel, Publish, #amqp_msg{payload = Payload}),
%% Closing Channel
amqp_channel:close(Channel),
%% Closing Connection
amqp_connection:close(Connection),
{ok, Message}.
on_message_acked(ClientId, Message, _Env) ->
%% Getting a Raw Message
Val=element(12,Message),
%% Making a Connection with RabbitMQ
{ok, Connection} = amqp_connection:start(#'amqp_params_network'{}),
%% Creating Channel
{ok, Channel} = amqp_connection:open_channel(Connection),
%% Declaring Queue
% Declare = #'queue.declare'{queue = <<"erlang_message">>,durable = true},
% #'queue.declare_ok'{queue = Queue} = amqp_channel:call(Channel, Declare),
%% Creating Payload Message
Payload = list_to_binary(["published|$&|",Val]),
%% Published a Message
Publish = #'basic.publish'{exchange = <<"celery">>},
amqp_channel:cast(Channel, Publish, #amqp_msg{payload = Payload}),
%% Closing Channel
amqp_channel:close(Channel),
%% Closing Connection
amqp_connection:close(Connection),
{ok, Message}.