The Failover Transport

The Failover transport layers reconnect logic on top of any of the other transports. (We used to call this transport the Reliable transport in ActiveMQ 3).

The Failover configuration syntax allows you to specify any number of composite uris. The Failover transport randomly chooses one of the composite URI and attempts to establish a connection to it. If it does not succeed or if it subsequently fails, a new connection is established to one of the other uris in the list.

Configuration Syntax

failover:(uri1,...,uriN)?transportOptions
or
failover:uri1,...,uriN

The failover transport uses random by default which lets you to load balance clients over a number of brokers.

If you would rather connect to a primary first and only connect to a secondary backup broker if the primary is unavailable, turn off randomizing using something like

failover:(tcp://primary:61616,tcp://secondary:61616)?randomize=false
Transport Options
Option Name Default Value Description
initialReconnectDelay 10 How long to wait before the first reconnect attempt (in ms)
maxReconnectDelay 30000 The maximum amount of time we ever wait between reconnect attempts (in ms)
useExponentialBackOff true Should an exponential backoff be used btween reconnect attempts
backOffMultiplier 2 The exponent used in the exponential backoff attempts
maxReconnectAttempts 0 If not 0, then this is the maximum number of reconnect attempts before an error is sent back to the client
randomize true use a random algorithm to choose the the URI to use for reconnect from the list provided
backup false initialize and hold a second transport connection - to enable fast failover
timeout -1 Enables timeout on send operations (in miliseconds) without interruption of reconnection process
Example URI
failover:(tcp://localhost:61616,tcp://remotehost:61616)?initialReconnectDelay=100

If the above gives errors try it this way (this way works in ActiveMQ 4.1.1 the one above does not)

failover://(tcp://localhost:61616,tcp://remotehost:61616)?initialReconnectDelay=100
Notes

If you use failover, and a broker dies at some point, your sends will block by default. Using TransportListener can help with this regard. It is best to set the Listener directly on the ActiveMQConnectionFactory so that it is in place before any request that may require an network hop.
Additionally you can use timeout option which will cause your current send to fail after specified timeout. The following URL, for example

failover:(tcp://primary:61616)?timeout=3000

will cause send to fail after 3 seconds if the connection isn't established. The connection will not be killed, so you can try sending messages later at some point using the same connection (presumably some of your brokers will be available again). Timeouts on the failover transport are available since 5.3 version.

Graphic Design By Hiram