我面临超时故障转移选项的问题。基本上我想要一个不断向代理发送消息的客户端,如果代理关闭,它将尝试重新连接,直到代理再次启动。同时,发送将超时,因此它不会永远挂起。
但是,经过一些测试,超时选项似乎无法正常工作。有时,它确实超时,但有时,它只是挂在那里。我知道另一个选项是 maxReconnectAttempt。但问题是我希望它尝试永远重新连接。
以下是我正在使用的网址:
failover:(tcp://10.5.0.198:61616)?timeout=1000
我有一个两服务器系统。一个充当经纪人,一个充当客户。当我将代理切换为启动/停止时,我收到以下日志消息:
We sent a Message!
2016-02-24 20:29:06,967 [198:61616@17075] - WARN FailoverTransport - Transport (tcp://10.5.0.198:61616) failed, reason: java.io.EOFException, attempting to automatically reconnect
Failover timeout of 1000 ms reached.
Failover timeout of 1000 ms reached.
Failover timeout of 1000 ms reached.
Failover timeout of 1000 ms reached.
Failover timeout of 1000 ms reached.
Failover timeout of 1000 ms reached.
We sent a Message!
We sent a Message!
We sent a Message!
We sent a Message!
We sent a Message!
2016-02-24 20:29:48,688 [198:61616@17099] - WARN FailoverTransport - Transport (tcp://10.5.0.198:61616) failed, reason: java.io.EOFException, attempting to automatically reconnect
We sent a Message!
We sent a Message!
We sent a Message!
We sent a Message!
We sent a Message!
2016-02-24 21:57:50,777 [198:61616@18542] - WARN FailoverTransport - Transport (tcp://10.5.0.198:61616) failed, reason: java.io.EOFException, attempting to automatically reconnect
Failover timeout of 1000 ms reached.
Failover timeout of 1000 ms reached.
Failover timeout of 1000 ms reached.
如您所见,第二次代理宕机时,它没有超时超过一小时。
下面是我正在使用的代码:
private final String connectionUri = "failover:(tcp://10.5.0.198:61616)?timeout=1000";
private ActiveMQConnectionFactory connectionFactory;
private Connection connection;
private Session session;
private Destination destination;
public void before() throws Exception {
connectionFactory = new ActiveMQConnectionFactory(connectionUri);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("MyQueue");
}
public void after() throws Exception {
if (connection != null) {
connection.close();
}
}
public void run() throws Exception {
MessageProducer producer = session.createProducer(destination);
try {
TextMessage message = session.createTextMessage();
message.setText("We sent a Message!");
producer.send(message);
} finally {
producer.close();
}
MessageConsumer consumer = session.createConsumer(destination);
try {
TextMessage message = (TextMessage) consumer.receive();
System.out.println(message.getText());
} finally {
consumer.close();
}
}
public static void main(String[] args) {
SimpleJMS example = new SimpleJMS();
System.out.print("\n\n\n");
System.out.println("Starting SimpleJMS example now...");
try {
example.before();
for(int i =0;i<1000;i++){
Thread.sleep(1000);
try{
example.run();
} catch (Exception e){
System.out.println(e.getMessage());
}
}
example.after();
} catch (Exception e) {
System.out.println("Caught an exception during the example: " + e.getMessage());
}
System.out.println("Finished running the SimpleJMS example.");
System.out.print("\n\n\n");
}