Aspects of the connections behaviour can be controlled through specifying connection options. For example, connections can be configured to automatically reconnect if the connection to a broker is lost.
Example 2.14. Specifying Connection Options in C++, Python, and .NET
In C++, these options can be set using Connection::setOption()
or by passing in a set of options to the constructor. The options can be passed in as a map or in string form:
Connection connection("localhost:5672", "{reconnect: true}"); try { connection.open(); !!! SNIP !!!
or
Connection connection("localhost:5672"); connection.setOption("reconnect", true); try { connection.open(); !!! SNIP !!!
In Python, these options can be set as attributes of the connection or using named arguments in
the Connection
constructor:
connection = Connection("localhost:5672", reconnect=True) try: connection.open() !!! SNIP !!!
or
connection = Connection("localhost:5672") connection.reconnect = True try: connection.open() !!! SNIP !!!
In .NET, these options can be set using Connection.SetOption()
or by passing in a set of options to the constructor. The options can be passed in as a map or in string form:
Connection connection= new Connection("localhost:5672", "{reconnect: true}"); try { connection.Open(); !!! SNIP !!!
or
Connection connection = new Connection("localhost:5672"); connection.SetOption("reconnect", true); try { connection.Open(); !!! SNIP !!!
See the reference documentation for details in each language.
The following table lists the supported connection options.
Table 2.4. Connection Options
option name | value type | semantics |
---|---|---|
username
| string | The username to use when authenticating to the broker. |
password
| string | The password to use when authenticating to the broker. |
sasl_mechanisms
| string | The specific SASL mechanisms to use with the python client when authenticating to the broker. The value is a space separated list. |
reconnect
| boolean | Transparently reconnect if the connection is lost. |
reconnect_timeout
| integer | Total number of seconds to continue reconnection attempts before giving up and raising an exception. |
reconnect_limit
| integer | Maximum number of reconnection attempts before giving up and raising an exception. |
reconnect_interval_min
| integer representing time in seconds |
Minimum number of seconds between reconnection attempts. The first reconnection attempt is made immediately; if that fails, the first reconnection delay is set to the value of reconnect_interval_min ; if that attempt fails, the reconnect interval increases exponentially until a reconnection attempt succeeds or reconnect_interval_max is reached.
|
reconnect_interval_max
| integer representing time in seconds | Maximum reconnect interval. |
reconnect_interval
| integer representing time in seconds |
Sets both reconnection_interval_min and reconnection_interval_max to the same value.
|
heartbeat
| integer representing time in seconds | Requests that heartbeats be sent every N seconds. If two successive heartbeats are missed the connection is considered to be lost. |
protocol
| string | Sets the underlying protocol used. The default option is 'tcp'. To enable ssl, set to 'ssl'. The C++ client additionally supports 'rdma'. |
tcp-nodelay
| boolean | Set tcp no-delay, i.e. disable Nagle algorithm. [C++ only] |