The Failover Transport
The Failover transport layers reconnect logic on top of any of the other transports. The configuration syntax allows you to specify any number of composite URIs. The Failover transport randomly chooses one of the composite URIs and attempts to establish a connection to it. If it does not succeed, or if it subsequently fails, a new connection is established choosing one of the other URIs randomly from the list.
The Failover transport chooses a URI at
random by default. This effectively load-balances clients over multiple brokers. However, to have a client connect to a primary first and only connect to a secondary backup broker when the primary is unavailable, set
randomize=false. For example:
The delay (in ms) before the first reconnect attempt.
The maximum delay (in ms) between the second and subsequent reconnect attempts.
true an exponential back-off is used between reconnect attempts.
The exponent used during exponential back-off attempts.
-1 | 0
From ActiveMQ 5.6: default is
-1, retry forever.
0 means disables re-connection, e.g: just try to connect once.
Before ActiveMQ 5.6: default is
0, retry forever.
All versions: a value
>0 denotes the maximum number of reconnect attempts before an error is sent back to the client.
A value of
-1 denotes that the number of connection attempts at startup should be unlimited.
A value of
>=0 denotes the number of reconnect attempts at startup that will be made after which an error is sent back to the client when the client makes a subsequent reconnect attempt.
Note: once successfully connected the
maxReconnectAttempts option prevails.
true, choose a URI at random from the list to use for reconnect.
Initialize and hold a second transport connection - to enable fast failover.
Set the timeout on send operations (in ms) without interruption of re-connection process. Since ActiveMQ 5.3.
Keep a cache of in-flight messages that will flushed to a broker on reconnect.
Size in bytes for the cache of tracked messages. Applicable only if
Determines whether the client should accept updates from the broker to its list of known URIs. Since ActiveMQ 5.4.
A URL (or path to a local file) to a text file containing a comma separated list of URIs to use for reconnect in the case of failure. Since ActiveMQ 5.4.
Common URI options that will be applied to each URI in the list. Since ActiveMQ 5.9.
>0 specifies the number of reconnect attempts before a warning is logged. A logged warning indicates that there is no current connection but re-connection is being attempted.
A value of
<=0 disables the logging of warnings about reconnect attempts. Since ActiveMQ 5.10.
Determines whether the client should respond to broker
ConnectionControl events with a reconnect (see:
Under the Failover transport send operations will, by default, block indefinitely when the broker becomes unavailable. There are two options available for handling this scenario. First, either set a TransportListener directly on the ActiveMQConnectionFactory, so that it is in place before any request that may require a network hop or second, set the
timeout option. The
timeout option causes the current send operation to fail after the specified timeout. For example:
In this example if the connection isn't established the send operation will timeout after 3 seconds. It is important to note that the connection is not killed when a timeout occurs. It is possible, therefore, to resend the affected message(s) later using the same connection once a broker becomes available.
The Failover transport tracks transactions by default. In-flight transactions are replayed upon re-connection. For simple scenarios this works as expected. However, there is an assumption regarding acknowledged (or consumer) transactions in that the previously received messages will automatically be replayed upon re-connection. This, however, is not always true when there are many connections and consumers, as re-delivery order is not guaranteed as stale outstanding acknowledgements can interfere with newly delivered messages. This can lead to unacknowledged messages.
Starting in version 5.3.1, however, re-delivery order is tracked and a transaction will fail to commit if outstanding messages are not redelivered after failover. A
TransactionRolledBackException is thrown if the commit fails. In doubt transactions will result in a rollback such that they can be replayed by the application. In doubt transactions occur when failover happens when a commit message is in-flight. It is not possible to know the exact point of failure. Did failure happen because the transaction commit message was not delivered or was the commit reply lost? In either case, it becomes necessary to rollback the transaction so that the application can get an indication of the failure and deal with any potential problem.
Broker-side Options for Failover
This is new in version 5.4:
The TransportConnector has options available so that the broker can update clients automatically regarding information of about the presence new brokers available for failover. The options are:
true, pass information to connected clients about changes in the topology of the broker cluster.
true, connected clients will be asked to re-balance across a cluster of brokers when a new broker joins the network of brokers (note:
priorityBackup=true can override).
true, will update clients when a cluster is removed from the network. Having this as separate option enables clients to be updated when new brokers join, but not when brokers leave.
Comma separated list of regular expression filters used to match broker names of brokers to designate as being part of the failover cluster for the clients.
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616" updateClusterClients="true" updateClusterFilter=".*A.*,.*B.*"/>
updateClusterClients=true, then clients only need to be configured with the details of one broker within a cluster to connect to. For example:
when new brokers join the cluster the client is automatically informed of the new broker's URI. The new URI is then available for failover when one of the other known brokers becomes unavailable.
Also check out the following blog entry about using the cluster client updates and re-balancing features titled New Features in ActiveMQ 5.4: Automatic Cluster Update and Rebalance.
If brokers are available in both local and remote networks, it's possible to specify a preference for local brokers over remote brokers using the
priorityURIs options (since ActiveMQ 5.6). Consider the following URL:
Given this URL the client will try to connect and stay connected to the
local broker. If
local broker fails, it will of course fail over to
remote. However, as
priorityBackup parameter is used, the client will constantly try to reconnect to
local. Once the client can do so, the client will re-connect to it without any need for manual intervention.
By default, only the first URI in the list is considered prioritized (local). In most cases this will suffice, but in some cases you can have multiple "local" URIs. The
priorityURIs option can be used to specify which URIs are considered prioritized. For example:
In this case the client will prioritize either
local2 brokers and (re-)connect to them if they are available.
Configuring Nested URI Options.
As of ActiveMQ 5.9 it's possible to specify common URI options by appending them to the query string of failover URI itself. Common URI options must be prefixed with
'nested.'. Note that if the same option is specified as both an individual URI option and a nested option, the nested option definition takes precedence.
For example, instead of doing this:
Any option that can applied to the query string of the individual URIs is a candidate for use with the