请求提供Red Hat JBoss AMQ Clients故障转移URI格式示例代码
Red Hat AMQ JMS Client Failover Example
Since you're working on a test program with failover functionality for Red Hat AMQ, here's a practical Java example using the exact URI format from the documentation, plus breakdowns of key configurations to help you adapt it to your setup.
Full Working Code
import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.qpid.jms.JmsConnectionFactory; public class AMQFailoverTestApp { public static void main(String[] args) { // Construct the failover URI with multiple brokers and retry configurations String failoverUri = "failover:(amqp://host1:5672,amqp://host2:5672,amqp://host3:5672)?" + "initialReconnectDelay=500&" + "maxReconnectAttempts=-1&" + "reconnectDelayMultiplier=1.5&" + "useExponentialBackOff=true"; // Initialize the connection factory with the failover URI ConnectionFactory factory = new JmsConnectionFactory(failoverUri); try (Connection connection = factory.createConnection("your-username", "your-password"); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)) { // Start the connection (triggers failover logic if initial broker is down) connection.start(); // Create a target queue (adjust to your topic/queue name) Destination testQueue = session.createQueue("test.failover.queue"); // Example: Send a test message to verify connectivity MessageProducer producer = session.createProducer(testQueue); TextMessage message = session.createTextMessage("Test message for failover cluster"); producer.send(message); System.out.println("Sent test message successfully"); // Add your test logic here (e.g., simulate broker failure, check reconnection) } catch (JMSException e) { System.err.println("Messaging error encountered: " + e.getMessage()); e.printStackTrace(); } } }
URI Configuration Breakdown
Let’s unpack the failover URI used above to clarify each part:
failover:(amqp://host1:5672,amqp://host2:5672): The core failover wrapper that lists all your AMQ broker endpoints. The client will automatically attempt to connect to the first available broker, and seamlessly fail over to others if the active connection drops.initialReconnectDelay=500: The first delay (in milliseconds) before trying to reconnect after a connection failure.maxReconnectAttempts=-1: Sets infinite reconnect attempts (use a positive number like10to limit retries).reconnectDelayMultiplier=1.5+useExponentialBackOff=true: Enables exponential backoff for reconnect delays (each subsequent retry waits 1.5x longer than the last, preventing overwhelming the cluster).
Quick Tips for Your Test Program
- Ensure you have the Red Hat AMQ JMS client dependency in your project (for Maven, use the
rh-amq-clientartifact from Red Hat's repositories). - To test failover behavior, manually stop one broker while the program is running—you should see the client automatically reconnect to another broker without crashing.
- Add connection listeners if you want to log failover events explicitly (use
connection.setExceptionListener(...)to capture connection failure/reconnection events).
内容的提问来源于stack exchange,提问作者greja
相关产品推荐
相关产品推荐

