Managing Disconnection
The HAClient
class, included with the AMPS C++ client, contains a
disconnect handler and other features for building highly-available
applications. The HAClient
includes features for managing a list of
failover servers, resuming subscriptions, republishing in-flight
messages, and other functionality that is commonly needed for high
availability. 60East recommends using the HAClient
for automatic
reconnection wherever possible, as the HAClient disconnect handler has
been carefully crafted to handle a wide variety of edge cases and
potential failures.
If an application needs to reconnect or fail over, use an
HAClient
, and the AMPS client library will automatically
handle failover and reconnection. You control which servers
the client fails over to using an implementation of the
ServerChooser
interface, and control the timing of
the failover using an implementation of the ReconnectDelayStrategy
interface.
For most applications, the combination of the HAClient
disconnect handler and a ConnectionStateListener
gives
you the ability to monitor disconnections and add custom
behavior at the appropriate point in the reconnection
process.
If you need to add custom behavior to the failover (such as logging,
resetting an internal cache, refreshing credentials and so on), the
ConnectionStateListener
class allows your application to
be notified and take action when disconnection is detected and at
each stage of the reconnection process.
To extend the behavior of the AMPS client during reconnection, implement
a ConnectionStateListener
and add
it to the set of active listeners
using the addConnectionStateListener
function.