Chapter 6. Network connections
Chapter 6. Network connections 6.1. Connection URLs Connection URLs encode the information used to establish new connections. Connection URL syntax Scheme - The connection transport, either amqp for unencrypted TCP or amqps for TCP with SSL/TLS encryption. Host - The remote network host. The value can be a hostname or a numeric IP address. IPv6 addresses must be enclosed in square brackets. Port - The remote network port. This value is optional. The default value is 5672 for the amqp scheme and 5671 for the amqps scheme. Connection URL examples 6.2. Creating outgoing connections To connect to a remote server, call the container::connect() method with a connection URL . This is typically done inside the messaging_handler::on_container_start() method. Example: Creating outgoing connections class example_handler : public proton::messaging_handler { void on_container_start(proton::container& cont) override { cont.connect("amqp://example.com"); } void on_connection_open(proton::connection& conn) override { std::cout << "The connection is open\n"; } }; See the Chapter 7, Security section for information about creating secure connections. 6.3. Configuring reconnect Reconnect allows a client to recover from lost connections. It is used to ensure that the components in a distributed system reestablish communication after temporary network or component failures. AMQ C++ disables reconnect by default. To enable it, set the reconnect connection option to an instance of the reconnect_options class. Example: Enabling reconnect proton::connection_options opts {}; proton::reconnect_options ropts {}; opts.reconnect(ropts); container.connect("amqp://example.com", opts); With reconnect enabled, if a connection is lost or a connection attempt fails, the client will try again after a brief delay. The delay increases exponentially for each new attempt. To control the delays between connection attempts, set the delay , delay_multiplier , and max_delay options. All durations are specified in milliseconds. To limit the number of reconnect attempts, set the max_attempts option. Setting it to 0 removes any limit. Example: Configuring reconnect proton::connection_options opts {}; proton::reconnect_options ropts {}; ropts.delay(proton::duration(10)); ropts.delay_multiplier(2.0); ropts.max_delay(proton::duration::FOREVER); ropts.max_attempts(0); opts.reconnect(ropts); container.connect("amqp://example.com", opts); 6.4. Configuring failover AMQ C++ allows you to configure multiple connection endpoints. If connecting to one fails, the client attempts to connect to the in the list. If the list is exhausted, the process starts over. To specify alternate connection endpoints, set the failover_urls reconnect option to a list of connection URLs. Example: Configuring failover std::vector<std::string> failover_urls = { "amqp://backup1.example.com", "amqp://backup2.example.com" }; proton::connection_options opts {}; proton::reconnect_options ropts {}; opts.reconnect(ropts); ropts.failover_urls(failover_urls); container.connect("amqp://primary.example.com", opts); 6.5. Accepting incoming connections AMQ C++ can accept inbound network connections, enabling you to build custom messaging servers. To start listening for connections, use the proton::container::listen() method with a URL containing the local host address and port to listen on. Example: Accepting incoming connections class example_handler : public proton::messaging_handler { void on_container_start(proton::container& cont) override { cont.listen("0.0.0.0"); } void on_connection_open(proton::connection& conn) override { std::cout << "New incoming connection\n"; } }; The special IP address 0.0.0.0 listens on all available IPv4 interfaces. To listen on all IPv6 interfaces, use [::0] . For more information, see the server receive.cpp example .
scheme://host[:port] amqps://example.com amqps://example.net:56720 amqp://127.0.0.1 amqp://[::1]:2000 class example_handler : public proton::messaging_handler { void on_container_start(proton::container& cont) override { cont.connect("amqp://example.com"); } void on_connection_open(proton::connection& conn) override { std::cout << "The connection is open\n"; } }; proton::connection_options opts {}; proton::reconnect_options ropts {}; opts.reconnect(ropts); container.connect("amqp://example.com", opts); proton::connection_options opts {}; proton::reconnect_options ropts {}; ropts.delay(proton::duration(10)); ropts.delay_multiplier(2.0); ropts.max_delay(proton::duration::FOREVER); ropts.max_attempts(0); opts.reconnect(ropts); container.connect("amqp://example.com", opts); std::vector<std::string> failover_urls = { "amqp://backup1.example.com", "amqp://backup2.example.com" }; proton::connection_options opts {}; proton::reconnect_options ropts {}; opts.reconnect(ropts); ropts.failover_urls(failover_urls); container.connect("amqp://primary.example.com", opts); class example_handler : public proton::messaging_handler { void on_container_start(proton::container& cont) override { cont.listen("0.0.0.0"); } void on_connection_open(proton::connection& conn) override { std::cout << "New incoming connection\n"; } };