我正在将JMS与QPID一起使用,想知道如果无法再访问该代理时如何配置重新连接:
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 ...
如果代理不再可用,我想重新连接直到代理再次备份。
答案 0 :(得分:1)
如果您引用的是Qpid JMS,则客户端会提供故障转移传输,该传输将为您处理自动重新连接,因此您的代码无需执行任何操作。 documentation解释了如何配置它,基本上,您可以修改URI以使用故障转移标签包装主机信息:
failover:(amqp://123.0.0.123:5672)