JMS QPID 句柄重新连接

JMS QPID handle reconnect

我正在使用带有 QPID 的 JMS,想知道如果无法再访问代理时如何配置重新连接:

try (Connection connection = jmsFactory.createConnection(); Session session = connection.createSession()) {
            Queue queue = session.createQueue("queue:testQueue");
            Topic topic = session.createTopic("topic:testTopic");
            TextMessage message = session.createTextMessage("toQueue");
            TextMessage message2 = session.createTextMessage("toTopic");

            MessageProducer producer = session.createProducer(queue);
            producer.send(message);
            MessageProducer producer2 = session.createProducer(topic);
            producer2.send(message2);
            MessageConsumer consumer = session.createConsumer(queue);
            consumer.setMessageListener(new MessageListener() {

                @Override
                public void onMessage(Message message) {
                    try {
                        String content = extractBody(message);
                        System.out.println("From: " + message.getJMSDestination() + " Content: " + content);
                        message.acknowledge();
                    } catch (JMSException e) {
                        System.out.println("CANNOT GET CONTENT= " + e);
                    }
                }
            });
            connection.start();
            consumer.close();
  }catch ...

如果代理无法访问,我想重新连接直到代理再次备份。

如果您指的是 Qpid JMS then the client provides a failover transport that handles automatic reconnection for you so that your code wouldn't need to do anything. The documentation 解释如何配置它,基本上您修改您的 URI 以使用故障转移标记包装主机信息:

 failover:(amqp://123.0.0.123:5672)