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.
Configuration Syntax
failover:(uri1,...,uriN)?transportOptions&nestedURIOptions
or
failover:uri1,...,uriN
Using Randomize
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:
failover:(tcp://primary:61616,tcp://secondary:61616)?randomize=false
Transport Options
Option Name | Default Value | Description |
---|---|---|
initialReconnectDelay | 10 | The delay (in ms) before the first reconnect attempt. |
maxReconnectDelay | 30000 | The maximum delay (in ms) between the second and subsequent reconnect attempts. |
useExponentialBackOff | true | If true an exponential back-off is used between reconnect attempts. |
reconnectDelayExponent | 2.0 | The exponent used during exponential back-off attempts. |
maxReconnectAttempts | -1 | 0 | From ActiveMQ 5.6: default is -1, retry forever. 0 means disables re-connection, e.g: just try to connect once. |
startupMaxReconnectAttempts | -1 | 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. |
randomize | true | If true, choose a URI at random from the list to use for reconnect. |
backup | false | Initialize and hold a second transport connection - to enable fast failover. |
timeout | -1 | Set the timeout on send operations (in ms) without interruption of re-connection process. Since ActiveMQ 5.3. |
trackMessages | false | Keep a cache of in-flight messages that will flushed to a broker on reconnect. |
maxCacheSize | 131072 | Size in bytes for the cache of tracked messages. Applicable only if trackMessages is true. |
updateURIsSupported | true | Determines whether the client should accept updates from the broker to its list of known URIs. Since ActiveMQ 5.4. |
updateURIsURL | null | 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. |
nested.* | null | Common URI options that will be applied to each URI in the list. Since ActiveMQ 5.9. |
warnAfterReconnectAttempts | 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. Since ActiveMQ 5.10. |
reconnectSupported | true | Determines whether the client should respond to broker ConnectionControl events with a reconnect (see: rebalanceClusterClients). |
Example URI
failover:(tcp://localhost:61616,tcp://remotehost:61616)?initialReconnectDelay=100
Notes
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 TransportListenerdirectly 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:
failover:(tcp://primary:61616)?timeout=3000
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.
Transactions
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 javax.jms.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:
Option Name | Default Value | Description |
---|---|---|
updateClusterClients | false | If true, pass information to connected clients about changes in the topology of the broker cluster. |
rebalanceClusterClients | false | If 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). |
updateClusterClientsOnRemove | false | If 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 notwhen brokers leave. |
updateClusterFilter | null | 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. |
For example:
<broker>
...
<transportConnectors>
<transportConnectorname="openwire"uri="
tcp://0.0.0.0:61616"updateClusterClients="true"updateClusterFilter=".*A.*,.*B.*"/>
</<transportConnectors>
...
</broker>
|
If updateClusterClients=true, then clients only need to be configured with the details of one broker within a cluster to connect to. For example:
failover:(tcp://primary:61616)
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.
More Information
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.
Priority Backup
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 priorityBackup and priorityURIs options (since ActiveMQ 5.6). Consider the following URL:
failover:(tcp://local:61616,tcp://remote:61616)?randomize=false&priorityBackup=true
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:
failover:(tcp://local1:61616,tcp://local2:61616,tcp://remote:61616)?randomize=false&priorityBackup=true&priorityURIs=tcp://local1:61616,tcp://local2:61616
In this case the client will prioritize either local1 or 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:
failover:(tcp://broker1:61616?wireFormat.maxInactivityDuration=1000,tcp://broker2:61616?wireFormat.maxInactivityDuration=1000,tcp://broker3:61616?wireFormat.maxInactivityDuration=1000)
do this:
failover:(tcp://broker1:61616,tcp://broker2:61616,tcp://broker3:61616)?nested.wireFormat.maxInactivityDuration=1000
Any option that can applied to the query string of the individual URIs is a candidate for use with the nested option.