Current section

Files

Jump to
diint_utilites_common_app src mq rabbit_utilites.erl
Raw

src/mq/rabbit_utilites.erl

%%%-------------------------------------------------------------------
%%% @author cheese
%%% @copyright (C) 2015, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 09. Jan 2015 8:56
%%%-------------------------------------------------------------------
-module(rabbit_utilites).
-author("cheese").
-include("_build/default/lib/amqp_client/include/amqp_client.hrl").
-include("include/types_rabbit.hrl").
%% API
-export([close_rpc_channel/4, convert_array_to_rabbit_connection_config/1]).
-export([connect/3]).
-export([internal_convert_array_to_rabbit_connection_config/2]).
convert_array_to_rabbit_connection_config(Arr) ->
internal_convert_array_to_rabbit_connection_config(Arr, #rabbit_connection_config{}).
internal_convert_array_to_rabbit_connection_config([], Info) ->
Info;
internal_convert_array_to_rabbit_connection_config([{Key, Value} | Tail], Info) ->
case Key of
process_name ->
internal_convert_array_to_rabbit_connection_config(Tail, Info#rabbit_connection_config{process_name = Value});
host ->
internal_convert_array_to_rabbit_connection_config(Tail, Info#rabbit_connection_config{host = Value});
password ->
internal_convert_array_to_rabbit_connection_config(Tail, Info#rabbit_connection_config{password = Value});
username ->
internal_convert_array_to_rabbit_connection_config(Tail, Info#rabbit_connection_config{username = Value});
virtual_host ->
internal_convert_array_to_rabbit_connection_config(Tail, Info#rabbit_connection_config{ virtual_host = Value});
queue_response ->
internal_convert_array_to_rabbit_connection_config(Tail, Info#rabbit_connection_config{ queue_response = Value});
exchange_response ->
internal_convert_array_to_rabbit_connection_config(Tail, Info#rabbit_connection_config{ exchange_response = Value});
arguments ->
internal_convert_array_to_rabbit_connection_config(Tail, Info#rabbit_connection_config{ arguments = Value});
_Other ->
internal_convert_array_to_rabbit_connection_config(Tail, Info)
end.
close_rpc_channel(ProcessName, Connection, Channel, LogFile) ->
logger_Cheese:info("Try to close ~w: ~w-~w~n", [ProcessName, Connection, Channel], LogFile),
try amqp_channel:close(Channel) of
ResCloseChannel ->
logger_Cheese:info("Close ~w chanell ~w: ~w~n", [ProcessName, Channel, ResCloseChannel], LogFile),
try amqp_connection:close(Connection) of
ResCloseConnection ->
logger_Cheese:info("Close ~w connection ~w: ~w~n", [ProcessName, Connection, ResCloseConnection], LogFile)
catch
ErrCloseConnection ->
logger_Cheese:info("Close ~w connection ~w error: ~w~n", [ProcessName, Connection, ErrCloseConnection], LogFile)
end
catch
ErrCloseChannel ->
logger_Cheese:info("Close ~w channel ~w error: ~w~n", [ProcessName, Channel, ErrCloseChannel], LogFile)
end.
connect(Process, #rabbit_connection_config{ host=Host, username=Username, password=Password, virtual_host=VirtHost}, LogFile) ->
logger_Cheese:info("Start create connection. Host: ~s, User: ~s, VirtHost: ~s~n", [Host, Username, VirtHost], LogFile),
case amqp_connection:start(#amqp_params_network{host = Host, password = Password, username = Username, heartbeat = 10, frame_max = 8388608, virtual_host = VirtHost}) of
{ok, Connection} ->
logger_Cheese:info("Create ~w connection: ~p~n", [Process, Connection], LogFile),
case amqp_connection:open_channel(Connection) of
{ok, Channel} ->
logger_Cheese:info("Create ~w channel: ~p~n", [Process, Channel], LogFile),
{ok, Connection, Channel};
ErrorChannel ->
logger_Cheese:info("Create ~w connection error: ~p~n", [Process, ErrorChannel], LogFile),
{error, ErrorChannel}
end;
ErrorConnection ->
logger_Cheese:info("Create ~w connection error: ~p~n", [Process, ErrorConnection], LogFile),
{error, ErrorConnection}
end.