activemq 消費者超時設置和失敗超時鏈接測試

 一 設置消費者等待產品到來等待時間

// Consume the message...
        MessageConsumer consumer = session.createConsumer(queue);
        Message msg = consumer.receive(60000);
        assertNotNull(msg);
        session.close();

consumer.receive(6000); ### timeout 6000 表示若是隊列中沒有元素的時候,再次去隊列取元素的時間間隔。默認單位是微秒。 若是是 0 就表示阻塞等待,直到元素到來。html

https://github.com/apache/activemq/blob/master/activemq-unit-tests/src/test/java/org/apache/activemq/ConsumerReceiveWithTimeoutTest.javajava

二 activemq 失敗重連設置

2.1 failover activemq 鏈接失敗重連設置

Configuration Syntaxgit

failover:(uri1,...,uriN)?transportOptions&nestedURIOptionsgithub

orapache

failover:uri1,...,uriNsession

能夠經過設置maxReconnectAttempts和maxReconnectDelaydom

多個參數之間用&tcp

failover:(tcp://local:61616,tcp://remote:61616)?randomize=false&priorityBackup=true

Example:spa

failover:(tcp://localhost:61616,tcp://remotehost:61616)?initialReconnectDelay=100

2.2 多個鏈接設置鏈接優先級

randomize=false

randomize=false 能夠設置優先鏈接放在第一位的 servercode

http://activemq.apache.org/failover-transport-reference.html

2.3 最有效的快速失敗重連配置是

下面配置是親自實踐過,當一個MQ有問題後,可以在大約10秒鐘左右完成快速切換到有效的 MQ server上面的配置。

failover://(tcp://XXXX:61616,tcp://XXXXX:61616)?nested.wireFormat.maxInactivityDuration=3000&n
ested.connectionTimeout=2000&maxReconnectAttempts=2&timeout=2000&initialReconnectDelay=50&startupMaxReconnectAttempts=2&maxRe
connectDelay=100

發現activemq說明文檔不是很清楚,能夠參考redhat 的說明:

https://access.redhat.com/documentation/en-US/Fuse_ESB_Enterprise/7.1/html/Fault_Tolerant_Messaging/files/FMQFaultTolStaticFailover.html

2.4 快速切換是如何完成的呢?maxInactivityDuration=3000 這個參數是如何工做的?

如何完成快速切換呢?

Connections are monitored by:

  • Ensuring data is read from the connection during the specified time period (Max Inactivity Duration).
  • Writing a KeepAliveInfo message to the connection if no normal activemq traffic is sent across the connection during the specified time period.

原來每一個鏈接都是有兩個監控的monitor,當maxInanctivityDuration時間裏面沒有消息的時候,就會發送keepAliveInfo心跳信息,不然sever就會認爲這個鏈接已經失效了,能夠關閉了。

http://activemq.apache.org/activemq-inactivitymonitor.html