Menu Search

1.10. Connections

Messaging connections are created by specifying a broker or a list of brokers, and an optional set of connection options. The constructor prototypes for Connections are:

      Connection connection();
      Connection connection(const string url);
      Connection connection(const string url, const string& options);
      Connection connection(const string url, const Variant::Map& options);
      

Messaging connection URLs specify only the network host address(es). Connection options are specified separately as an options string or map. This is different from JMS Connection URLs that combine the network address and connection properties in a single string.

1.10.1. Connection URLs

Connection URLs describe the broker or set of brokers to which the connection is to attach. The format of the Connection URL is defined by AMQP 0.10 Domain:connection.amqp-host-url.

	amqp_url = "amqp:" prot_addr_list
	prot_addr_list = [prot_addr ","]* prot_addr
	prot_addr = tcp_prot_addr | tls_prot_addr
	
	tcp_prot_addr = tcp_id tcp_addr
	tcp_id = "tcp:" | ""
	tcp_addr = [host [":" port] ]
	host = <as per http://www.ietf.org/rfc/rfc3986.txt>
	port = number	

Examples of Messaging Connection URLs

	localhost
	localhost:5672
	localhost:9999
	192.168.1.2:5672
	mybroker.example.com:5672
	amqp:tcp:localhost:5672
	tcp:locahost:5672,localhost:5800
	

1.10.2. Connection Options

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 1.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:

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 1.4. Connection Options

option namevalue typesemantics
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.
transport string Sets the underlying transport 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]
protocol string Sets the application protocol used. The default option is 'amqp0-10'. To enable AMQP 1.0, set to 'amqp1.0'.