1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
%% 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 RabbitMQ.
%%
%% The Initial Developer of the Original Code is VMware, Inc.
%% Copyright (c) 2007-2012 VMware, Inc. All rights reserved.
%%
%% @type close_reason(Type) = {shutdown, amqp_reason(Type)}.
%% @type amqp_reason(Type) = {Type, Code, Text}
%% Code = non_neg_integer()
%% Text = binary().
%% @doc This module is responsible for maintaining a connection to an AMQP
%% broker and manages channels within the connection. This module is used to
%% open and close connections to the broker as well as creating new channels
%% within a connection.<br/>
%% The connections and channels created by this module are supervised under
%% amqp_client's supervision tree. Please note that connections and channels
%% do not get restarted automatically by the supervision tree in the case of a
%% failure. If you need robust connections and channels, we recommend you use
%% Erlang monitors on the returned connection and channel PIDs.<br/>
%% <br/>
%% In case of a failure or an AMQP error, the connection process exits with a
%% meaningful exit reason:<br/>
%% <br/>
%% <table>
%% <tr>
%% <td><strong>Cause</strong></td>
%% <td><strong>Exit reason</strong></td>
%% </tr>
%% <tr>
%% <td>Any reason, where Code would have been 200 otherwise</td>
%% <td>```normal'''</td>
%% </tr>
%% <tr>
%% <td>User application calls amqp_connection:close/3</td>
%% <td>```close_reason(app_initiated_close)'''</td>
%% </tr>
%% <tr>
%% <td>Server closes connection (hard error)</td>
%% <td>```close_reason(server_initiated_close)'''</td>
%% </tr>
%% <tr>
%% <td>Server misbehaved (did not follow protocol)</td>
%% <td>```close_reason(server_misbehaved)'''</td>
%% </tr>
%% <tr>
%% <td>AMQP client internal error - usually caused by a channel exiting
%% with an unusual reason. This is usually accompanied by a more
%% detailed error log from the channel</td>
%% <td>```close_reason(internal_error)'''</td>
%% </tr>
%% <tr>
%% <td>Other error</td>
%% <td>(various error reasons, causing more detailed logging)</td>
%% </tr>
%% </table>
%% <br/>
%% See type definitions below.
-module(amqp_connection).
-include("amqp_client.hrl").
-export([open_channel/1, open_channel/2, open_channel/3]).
-export([start/1]).
-export([close/1, close/3]).
-export([info/2, info_keys/1, info_keys/0]).
%%---------------------------------------------------------------------------
%% Type Definitions
%%---------------------------------------------------------------------------
%% @type adapter_info() = #adapter_info{}.
%% @type amqp_params_direct() = #amqp_params_direct{}.
%% As defined in amqp_client.hrl. It contains the following fields:
%% <ul>
%% <li>username :: binary() - The name of a user registered with the broker,
%% defaults to <<guest">></li>
%% <li>virtual_host :: binary() - The name of a virtual host in the broker,
%% defaults to <<"/">></li>
%% <li>node :: atom() - The node the broker runs on (direct only)</li>
%% <li>adapter_info :: adapter_info() - Extra management information for if
%% this connection represents a non-AMQP network connection.</li>
%% <li>client_properties :: [{binary(), atom(), binary()}] - A list of extra
%% client properties to be sent to the server, defaults to []</li>
%% </ul>
%%
%% @type amqp_params_network() = #amqp_params_network{}.
%% As defined in amqp_client.hrl. It contains the following fields:
%% <ul>
%% <li>username :: binary() - The name of a user registered with the broker,
%% defaults to <<guest">></li>
%% <li>password :: binary() - The user's password, defaults to
%% <<"guest">></li>
%% <li>virtual_host :: binary() - The name of a virtual host in the broker,
%% defaults to <<"/">></li>
%% <li>host :: string() - The hostname of the broker,
%% defaults to "localhost" (network only)</li>
%% <li>port :: integer() - The port the broker is listening on,
%% defaults to 5672 (network only)</li>
%% <li>channel_max :: non_neg_integer() - The channel_max handshake parameter,
%% defaults to 0</li>
%% <li>frame_max :: non_neg_integer() - The frame_max handshake parameter,
%% defaults to 0 (network only)</li>
%% <li>heartbeat :: non_neg_integer() - The hearbeat interval in seconds,
%% defaults to 0 (turned off) (network only)</li>
%% <li>connection_timeout :: non_neg_integer() | 'infinity'
%% - The connection timeout in milliseconds,
%% defaults to 'infinity' (network only)</li>
%% <li>ssl_options :: term() - The second parameter to be used with the
%% ssl:connect/2 function, defaults to 'none' (network only)</li>
%% <li>client_properties :: [{binary(), atom(), binary()}] - A list of extra
%% client properties to be sent to the server, defaults to []</li>
%% <li>socket_options :: [any()] - Extra socket options. These are
%% appended to the default options. See
%% <a href="http://www.erlang.org/doc/man/inet.html#setopts-2">inet:setopts/2</a>
%% and <a href="http://www.erlang.org/doc/man/gen_tcp.html#connect-4">
%% gen_tcp:connect/4</a> for descriptions of the available options.</li>
%% </ul>
%%---------------------------------------------------------------------------
%% Starting a connection
%%---------------------------------------------------------------------------
%% @spec (Params) -> {ok, Connection} | {error, Error}
%% where
%% Params = amqp_params_network() | amqp_params_direct()
%% Connection = pid()
%% @doc Starts a connection to an AMQP server. Use network params to
%% connect to a remote AMQP server or direct params for a direct
%% connection to a RabbitMQ server, assuming that the server is
%% running in the same process space. If the port is set to 'undefined',
%% the default ports will be selected depending on whether this is a
%% normal or an SSL connection.
start(AmqpParams) ->
case amqp_client:start() of
ok -> ok;
{error, {already_started, amqp_client}} -> ok;
{error, _} = E -> throw(E)
end,
AmqpParams1 =
case AmqpParams of
#amqp_params_network{port = undefined, ssl_options = none} ->
AmqpParams#amqp_params_network{port = ?PROTOCOL_PORT};
#amqp_params_network{port = undefined, ssl_options = _} ->
AmqpParams#amqp_params_network{port = ?PROTOCOL_SSL_PORT};
_ ->
AmqpParams
end,
{ok, _Sup, Connection} = amqp_sup:start_connection_sup(AmqpParams1),
amqp_gen_connection:connect(Connection).
%%---------------------------------------------------------------------------
%% Commands
%%---------------------------------------------------------------------------
%% @doc Invokes open_channel(ConnectionPid, none, ?DEFAULT_CONSUMER).
%% Opens a channel without having to specify a channel number. This uses the
%% default consumer implementation.
open_channel(ConnectionPid) ->
open_channel(ConnectionPid, none, ?DEFAULT_CONSUMER).
%% @doc Invokes open_channel(ConnectionPid, none, Consumer).
%% Opens a channel without having to specify a channel number.
open_channel(ConnectionPid, {_, _} = Consumer) ->
open_channel(ConnectionPid, none, Consumer);
%% @doc Invokes open_channel(ConnectionPid, ChannelNumber, ?DEFAULT_CONSUMER).
%% Opens a channel, using the default consumer implementation.
open_channel(ConnectionPid, ChannelNumber)
when is_number(ChannelNumber) orelse ChannelNumber =:= none ->
open_channel(ConnectionPid, ChannelNumber, ?DEFAULT_CONSUMER).
%% @spec (ConnectionPid, ChannelNumber, Consumer) -> Result
%% where
%% ConnectionPid = pid()
%% ChannelNumber = pos_integer() | 'none'
%% Consumer = {ConsumerModule, ConsumerArgs}
%% ConsumerModule = atom()
%% ConsumerArgs = [any()]
%% Result = {ok, ChannelPid} | {error, Error}
%% ChannelPid = pid()
%% @doc Opens an AMQP channel.<br/>
%% Opens a channel, using a proposed channel number and a specific consumer
%% implementation.<br/>
%% ConsumerModule must implement the amqp_gen_consumer behaviour. ConsumerArgs
%% is passed as parameter to ConsumerModule:init/1.<br/>
%% This function assumes that an AMQP connection (networked or direct)
%% has already been successfully established.<br/>
%% ChannelNumber must be less than or equal to the negotiated max_channel value,
%% or less than or equal to ?MAX_CHANNEL_NUMBER if the negotiated max_channel
%% value is 0.<br/>
%% In the direct connection, max_channel is always 0.
open_channel(ConnectionPid, ChannelNumber,
{_ConsumerModule, _ConsumerArgs} = Consumer) ->
amqp_gen_connection:open_channel(ConnectionPid, ChannelNumber, Consumer).
%% @spec (ConnectionPid) -> ok | Error
%% where
%% ConnectionPid = pid()
%% @doc Closes the channel, invokes
%% close(Channel, 200, <<"Goodbye">>).
close(ConnectionPid) ->
close(ConnectionPid, 200, <<"Goodbye">>).
%% @spec (ConnectionPid, Code, Text) -> ok | closing
%% where
%% ConnectionPid = pid()
%% Code = integer()
%% Text = binary()
%% @doc Closes the AMQP connection, allowing the caller to set the reply
%% code and text.
close(ConnectionPid, Code, Text) ->
Close = #'connection.close'{reply_text = Text,
reply_code = Code,
class_id = 0,
method_id = 0},
amqp_gen_connection:close(ConnectionPid, Close).
%%---------------------------------------------------------------------------
%% Other functions
%%---------------------------------------------------------------------------
%% @spec (ConnectionPid, Items) -> ResultList
%% where
%% ConnectionPid = pid()
%% Items = [Item]
%% ResultList = [{Item, Result}]
%% Item = atom()
%% Result = term()
%% @doc Returns information about the connection, as specified by the Items
%% list. Item may be any atom returned by info_keys/1:
%%<ul>
%%<li>type - returns the type of the connection (network or direct)</li>
%%<li>server_properties - returns the server_properties fields sent by the
%% server while establishing the connection</li>
%%<li>is_closing - returns true if the connection is in the process of closing
%% and false otherwise</li>
%%<li>amqp_params - returns the #amqp_params{} structure used to start the
%% connection</li>
%%<li>num_channels - returns the number of channels currently open under the
%% connection (excluding channel 0)</li>
%%<li>channel_max - returns the channel_max value negotiated with the
%% server</li>
%%<li>heartbeat - returns the heartbeat value negotiated with the server
%% (only for the network connection)</li>
%%<li>frame_max - returns the frame_max value negotiated with the
%% server (only for the network connection)</li>
%%<li>sock - returns the socket for the network connection (for use with
%% e.g. inet:sockname/1) (only for the network connection)</li>
%%<li>any other value - throws an exception</li>
%%</ul>
info(ConnectionPid, Items) ->
amqp_gen_connection:info(ConnectionPid, Items).
%% @spec (ConnectionPid) -> Items
%% where
%% ConnectionPid = pid()
%% Items = [Item]
%% Item = atom()
%% @doc Returns a list of atoms that can be used in conjunction with info/2.
%% Note that the list differs from a type of connection to another (network vs.
%% direct). Use info_keys/0 to get a list of info keys that can be used for
%% any connection.
info_keys(ConnectionPid) ->
amqp_gen_connection:info_keys(ConnectionPid).
%% @spec () -> Items
%% where
%% Items = [Item]
%% Item = atom()
%% @doc Returns a list of atoms that can be used in conjunction with info/2.
%% These are general info keys, which can be used in any type of connection.
%% Other info keys may exist for a specific type. To get the full list of
%% atoms that can be used for a certain connection, use info_keys/1.
info_keys() ->
amqp_gen_connection:info_keys().
|