Packages
diint_utilites_common_app
1.4.21
1.4.23
1.4.22
1.4.21
1.4.20
1.4.19
1.4.18
1.4.17
1.4.16
1.4.15
1.4.14
1.4.12
1.4.11
1.4.10
1.4.9
1.4.8
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.9
1.3.8
1.3.7
1.3.6
1.3.5
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.2.101
1.2.11
1.2.10
1.2.9
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.13
1.0.12
1.0.11
1.0.10
1.0.9
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
библиотеки для работы с ребитом и протоколы
Current section
Files
Jump to
Current section
Files
src/mq/rabbit_rpc2_sender.erl
%%% @author cheese
%%% @copyright (C) 2016, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 19. Apr 2016 11:12
%%%-------------------------------------------------------------------
-module(rabbit_rpc2_sender).
-author("cheese").
-behaviour(gen_server).
%% API
-export([start_link/3]).
-export([get_process_name/2, get_logfile_name/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3, send/5]).
-include("include/types_rabbit.hrl").
-include("_build/default/lib/amqp_client/include/amqp_client.hrl").
-include_lib("kernel/include/logger.hrl").
-record(state, {
connection_config :: #rabbit_connection_config{},
queue,
exchange,
arguments,
log_file,
channel,
connection,
pid
}).
%%%===================================================================
%%% API
%%%===================================================================
start_link(RabbitConnectionConfig, ExchangeName, Queue) ->
RabbitSenderProcessName = get_process_name(RabbitConnectionConfig, Queue),
rabbit_rpc2:set_state_rmq_sender(RabbitConnectionConfig, RabbitSenderProcessName, initialization),
gen_server:start_link({local, RabbitSenderProcessName}, ?MODULE, [RabbitConnectionConfig, ExchangeName, Queue], []).
send(RabbitConnectionConfig, Message, MessageId, RequestQueue, RespQueue) ->
ProcessName = get_process_name(RabbitConnectionConfig, RequestQueue),
?LOG_INFO("Send to ProcessName: ~w~n", [ProcessName]),
gen_server:call(ProcessName, {send, Message, MessageId, RespQueue}).
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
init([RabbitConnectionConfig, ExchangeName, Queue]) ->
erlang:send_after(10, self(), check_connection),
{ok, #state{
arguments = RabbitConnectionConfig#rabbit_connection_config.arguments,
connection_config = RabbitConnectionConfig,
exchange = ExchangeName,
queue = Queue,
pid = self()
}
}.
handle_call(reconnect,_From, #state{connection_config = RabbitConfig, queue = Queue, exchange = Exchange, arguments = Arguments} = State) ->
case connectAndDeclare(RabbitConfig, Exchange, Queue,Arguments) of
{ok, NewChannel, NewConnection} ->
{noreply, State#state{channel = NewChannel, connection = NewConnection}};
{error, Reason} ->
erlang:error(sql_connection_error),
?LOG_INFO("Check rabbit connection error: ~w~n", [Reason]),
{noreply, State}
end;
handle_call(get_queue_name, _From, #state{queue = Queue} = State) ->
{reply, {ok, Queue}, State};
handle_call({send, Message, MessageId, RespQueue}, _From, #state{channel = Channel, exchange = Exchange} = State) ->
?LOG_INFO("Send: ~s~n", [bytes_extension:bin_to_hexstr(Message)]),
case Channel of
undefined ->
?LOG_INFO("Sender channel undefined~n", []),
{reply, {error, no_channel_exist}, State};
_Other ->
Props = #'P_basic'{correlation_id = MessageId, reply_to = RespQueue},
amqp_channel:cast(Channel, #'basic.publish'{exchange = Exchange}, #amqp_msg{payload = Message, props = Props}),
?LOG_INFO("Publish ok~n", []),
{reply, ok, State}
end;
handle_call(_Request, _From, State) ->
{reply, ok, State}.
handle_cast(_Request, State) ->
{noreply, State}.
handle_info(check_connection, #state{connection_config = RabbitConfig, queue = Queue, channel = Channel, exchange = Exchange , arguments = Arguments} = State) ->
% erlang:send_after(10000, self(), check_connection),
case Channel of
undefined ->
case connectAndDeclare(RabbitConfig, Exchange, Queue,Arguments) of
{ok, Connection, NewChannel}->
{noreply, State#state{channel = NewChannel, connection = Connection}};
{error, _Reason} ->
{noreply, State}
end;
_ ->
{noreply, State}
end;
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================
get_process_name(RabbitConnectionConfig, Queue) ->
list_to_atom(atom_to_list(RabbitConnectionConfig#rabbit_connection_config.process_name) ++ "_rpc2_sender_" ++ binary_to_list(Queue)).
get_logfile_name(RabbitConnectionConfig, Queue) ->
"log/" ++ atom_to_list(RabbitConnectionConfig#rabbit_connection_config.process_name) ++ "_rpc2_sender_" ++ binary_to_list(Queue) ++ ".log".
connectAndDeclare(RabbitConfig, Exchange, Queue,Arguments) ->
Process = get_process_name(RabbitConfig, Queue),
?LOG_INFO("Start create sender ~w~n", [Process]),
case rabbit_utilites:connect(Process, RabbitConfig) of
{ok, Connection, NewChannel} = Resp ->
case amqp_channel:call(NewChannel, #'exchange.declare'{exchange = Exchange,durable = true}) of
#'exchange.declare_ok'{} -> ok;
_-> #'exchange.declare_ok'{} = amqp_channel:call(NewChannel, #'exchange.declare'{exchange = Exchange,durable = false})
end,
case amqp_channel:call(NewChannel, #'queue.declare'{queue = Queue,durable = true, auto_delete = false, arguments = Arguments}) of
#'queue.declare_ok'{} -> ok;
_-> #'queue.declare_ok'{} = amqp_channel:call(NewChannel, #'queue.declare'{queue = Queue,durable = false, auto_delete = false, arguments = Arguments})
end,
#'queue.bind_ok'{} = amqp_channel:call(NewChannel, #'queue.bind'{queue = Queue, exchange = Exchange}),
rabbit_rpc2:set_state_rmq_sender(RabbitConfig, Process, connected),
?LOG_INFO("Sender ~w created ok~n", [Process]),
spawn(rabbit_utilites,connectionMonitor,[Connection, Process]),
Resp;
{error, Reason} ->
?LOG_INFO("Sender ~w create error: ~w~n", [Process, Reason]),
spawn(rabbit_utilites,connectionMonitor,[undefined, Process])
end.