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.
Initialize and hold a second transport connection - to enable fast failover.
The delay (in ms) before the first reconnect attempt.
Size in bytes for the cache of tracked messages.
Applicable only if
-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 ActiveMQ versions: a value
>0 denotes the maximum number of reconnect attempts before an error is sent back to the client.
The maximum delay (in ms) between the second and subsequent reconnect attempts.
From ActiveMQ 5.9: common URI options that will be applied to each URI in the list.
true, choose a URI at random from the list to use for reconnect.
The exponent used during exponential back-off attempts.
Determines whether the client should respond to broker
ConnectionControl events with a reconnect (see:
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.
From ActiveMQ 5.3: set the timeout on send operations (in ms) without interruption of re-connection process.
Keep a cache of in-flight messages that will flushed to a broker on reconnect.
From ActiveMQ 5.4: determines whether the client should accept updates from the broker to its list of known URIs.
From 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.
true an exponential back-off is used between reconnect attempts.
From ActiveMQ 5.10: a value
>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.
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
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.
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.
From ActiveMQ 5.3.1: 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
From ActiveMQ 5.4: the
TransportConnector has options available so that the broker can update clients automatically with information regarding the presence of new brokers that are available (or are no longer 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 clients need only be configured with the details of one broker in a cluster in order to connect.
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.
From ActiveMQ 5.6: 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
Consider the following URL:
Given this URL a 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. However, in some cases it may be necessary to have multiple "local" URIs. The
priorityURIs option can be used to specify which URIs are considered prioritized.
In this case the client will prioritize either
local2 brokers and (re-)connect to them if they are available.
Configuring Nested URI Options.
From ActiveMQ 5.9: common URI options can be configured by appending them to the query string of the failover URI where each common URI option has the prefix:
Example - instead of doing this:
Any option that can applied to the query string of an individual URI is a candidate for use with the
If the same option is specified as both an individual URI option and as a nested option, the nested option definition will take precedence.