6.3. 配置重新连接
重新连接可让客户端从丢失的连接中恢复。它用于确保分布式系统中的组件在临时网络或组件故障后重新建立通信。
AMQ C++ 默认禁用重新连接。要启用它,请将 reconnect connection 选项设置为 reconnect_options 类的实例。
示例:启用重新连接
proton::connection_options opts {};
proton::reconnect_options ropts {};
opts.reconnect(ropts);
container.connect("amqp://example.com", opts);
启用重新连接后,如果连接丢失或连接尝试失败,客户端会在短暂的延迟后重试。每次新尝试都会增加延迟。
要控制连接尝试之间的延迟,请设置 delay、 和 delay _multipliermax_delay 选项。所有持续时间都以毫秒为单位指定。
要限制重新连接尝试次数,请设置 max_attempts 选项。将它设置为 0 可移除任何限制。
示例:配置重新连接
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);