diff --git a/ebin/amqp_client.app.in b/ebin/amqp_client.app.in index dc688590..2bf0ec9c 100644 --- a/ebin/amqp_client.app.in +++ b/ebin/amqp_client.app.in @@ -13,4 +13,5 @@ ]}, {registered, []}, {env, []}, + {mod, {amqp_client, []}}, {applications, [kernel, stdlib]}]}. diff --git a/src/amqp_client.erl b/src/amqp_client.erl new file mode 100644 index 00000000..ba4ca6b8 --- /dev/null +++ b/src/amqp_client.erl @@ -0,0 +1,48 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is the RabbitMQ Erlang Client. +%% +%% The Initial Developers of the Original Code are LShift Ltd., +%% Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd. +%% +%% Portions created by LShift Ltd., Cohesive Financial +%% Technologies LLC., and Rabbit Technologies Ltd. are Copyright (C) +%% 2007 LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit +%% Technologies Ltd.; +%% +%% All Rights Reserved. +%% +%% Contributor(s): ____________________. + +%% @private +-module(amqp_client). + +-behaviour(application). + +-export([start/0]). +-export([start/2, stop/1]). + +%%--------------------------------------------------------------------------- +%% Interface +%%--------------------------------------------------------------------------- + +start() -> + application:start(amqp_client). + +%%--------------------------------------------------------------------------- +%% application callbacks +%%--------------------------------------------------------------------------- + +start(_StartType, _StartArgs) -> + amqp_sup:start_link(). + +stop(_State) -> + ok. diff --git a/src/amqp_connection.erl b/src/amqp_connection.erl index 9431be8f..5e9f8512 100644 --- a/src/amqp_connection.erl +++ b/src/amqp_connection.erl @@ -99,7 +99,9 @@ start(Type) -> %% a RabbitMQ server, assuming that the server is running in the same process %% space. start(Type, AmqpParams) -> - {ok, _Sup, Connection} = amqp_connection_sup:start_link(Type, AmqpParams), + amqp_client:start(), + {ok, _Sup, Connection} = + amqp_sup:start_connection_sup(Type, AmqpParams), Module = case Type of direct -> amqp_direct_connection; network -> amqp_network_connection end, diff --git a/src/amqp_connection_sup.erl b/src/amqp_connection_sup.erl index a7364f10..9a9940fe 100644 --- a/src/amqp_connection_sup.erl +++ b/src/amqp_connection_sup.erl @@ -38,7 +38,6 @@ start_link(Type, AmqpParams) -> {ok, Sup} = supervisor2:start_link(?MODULE, []), - unlink(Sup), {ok, ChSupSup} = supervisor2:start_child( Sup, {channel_sup_sup, {amqp_channel_sup_sup, start_link, diff --git a/src/amqp_main_reader.erl b/src/amqp_main_reader.erl index 4143a9d0..dd72055b 100644 --- a/src/amqp_main_reader.erl +++ b/src/amqp_main_reader.erl @@ -56,15 +56,8 @@ init([Sock, Connection, ChMgr, Framing0]) -> {ok, #state{sock = Sock, connection = Connection, channels_manager = ChMgr, framing0 = Framing0}}. -terminate(Reason, #state{sock = Sock}) -> - Nice = case Reason of normal -> true; - shutdown -> true; - {shutdown, _} -> true; - _ -> false - end, - ok = case Nice of true -> rabbit_net:close(Sock); - false -> ok - end. +terminate(_Reason, _State) -> + ok. code_change(_OldVsn, State, _Extra) -> State. diff --git a/src/amqp_network_connection.erl b/src/amqp_network_connection.erl index 3f27ad31..4f7ba6dd 100644 --- a/src/amqp_network_connection.erl +++ b/src/amqp_network_connection.erl @@ -151,13 +151,13 @@ handle_method(#'connection.close'{} = Close, none, State) -> #closing{reason = server_initiated_close, close = Close}, State)}; -handle_method(#'connection.close_ok'{}, none, - State = #state{closing = Closing}) -> +handle_method(#'connection.close_ok'{}, none, State = #state{closing = Closing, + sock = Sock}) -> + ok = rabbit_net:close(Sock), #closing{from = From, close = #'connection.close'{reply_code = ReplyCode}} = Closing, - case From of - none -> ok; - _ -> gen_server:reply(From, ok) + case From of none -> ok; + _ -> gen_server:reply(From, ok) end, if ReplyCode =:= 200 -> {stop, normal, State}; true -> {stop, closing_to_reason(Closing), State} @@ -397,10 +397,6 @@ start_ok(#state{params = #amqp_params{username = Username, response = rabbit_binary_generator:generate_table(LoginTable)}. client_properties(UserProperties) -> - %% TODO This eagerly starts the amqp_client application in order to - %% to get the version from the app descriptor, which may be - %% overkill - maybe there is a more suitable point to boot the app - rabbit_misc:start_applications([amqp_client]), {ok, Vsn} = application:get_key(amqp_client, vsn), Default = [{<<"product">>, longstr, <<"RabbitMQ">>}, {<<"version">>, longstr, list_to_binary(Vsn)}, diff --git a/src/amqp_sup.erl b/src/amqp_sup.erl new file mode 100644 index 00000000..871e800d --- /dev/null +++ b/src/amqp_sup.erl @@ -0,0 +1,52 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is the RabbitMQ Erlang Client. +%% +%% The Initial Developers of the Original Code are LShift Ltd., +%% Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd. +%% +%% Portions created by LShift Ltd., Cohesive Financial +%% Technologies LLC., and Rabbit Technologies Ltd. are Copyright (C) +%% 2007 LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit +%% Technologies Ltd.; +%% +%% All Rights Reserved. +%% +%% Contributor(s): ____________________. + +%% @private +-module(amqp_sup). + +-include("amqp_client.hrl"). + +-behaviour(supervisor2). + +-export([start_link/0, start_connection_sup/2]). +-export([init/1]). + +%%--------------------------------------------------------------------------- +%% Interface +%%--------------------------------------------------------------------------- + +start_link() -> + supervisor2:start_link({local, amqp_sup}, ?MODULE, []). + +start_connection_sup(Type, AmqpParams) -> + supervisor2:start_child(amqp_sup, [Type, AmqpParams]). + +%%--------------------------------------------------------------------------- +%% supervisor2 callbacks +%%--------------------------------------------------------------------------- + +init([]) -> + {ok, {{simple_one_for_one_terminate, 0, 1}, + [{connection_sup, {amqp_connection_sup, start_link, []}, + temporary, infinity, supervisor, [amqp_connection_sup]}]}}. diff --git a/test.mk b/test.mk old mode 100755 new mode 100644 index 53e261ef..621f8b2c --- a/test.mk +++ b/test.mk @@ -61,11 +61,12 @@ run_test_broker: start_test_broker_node unboot_broker $$OK start_test_broker_node: boot_broker - $(RABBITMQCTL) delete_user test_user_no_perm 2>/dev/null || true + sleep 1 + - $(RABBITMQCTL) delete_user test_user_no_perm $(RABBITMQCTL) add_user test_user_no_perm test_user_no_perm stop_test_broker_node: - $(RABBITMQCTL) delete_user test_user_no_perm + - $(RABBITMQCTL) delete_user test_user_no_perm $(MAKE) unboot_broker boot_broker: