ActiveMQ故障转移超时随机工作

时间:2016-02-24 23:22:22

标签: failover amq

我遇到了超时故障转移选项的问题。 基本上我想要一个不断向代理发送消息的客户端,如果代理关闭,它将尝试重新连接,直到代理再次启动。同时,发送将超时,因此它不会永远等待。

但是,经过一些测试后,似乎超时选项无法正常工作。 有时,它确实超时,但有时,它只是挂在那里。 我知道另一种选择是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");
}

2 个答案:

答案 0 :(得分:0)

我无法解决这个问题。经过一些研究,这似乎是AMQ的一个问题。

因此,为了不阻止整个事情,我切换到提供本地缓冲区的网络代理,因此当它进行重试时,发送操作将继续。

这是我现在能找到的最佳解决方案。

答案 1 :(得分:0)

我面临着同样的挑战,似乎添加了maxReconnectAttempts = 2&maxReconnectDelay = 5000帮助