activemq 消费者超时设置和失败超时连接测试

本文详细介绍了ActiveMQ消息队列中消费者等待时间和失败重连的配置方法。包括如何设置消费者接收消息的等待时间、如何配置failover以实现故障转移及快速重连等关键参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 一 设置消费者等待产品到来等待时间

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

consumer.receive(6000); ### timeout 6000 表示如果队列中没有元素的时候,再次去队列取元素的时间间隔。默认单位是微秒。 如果是 0 就表示阻塞等待,直到元素到来。

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

二 activemq 失败重连设置

2.1 failover activemq 连接失败重连设置

Configuration Syntax

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

or

failover:uri1,...,uriN

可以通过设置maxReconnectAttempts和maxReconnectDelay

多个参数之间用&

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

Example:

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

2.2 多个连接设置连接优先级

randomize=false

randomize=false 可以设置优先连接放在第一位的 server

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

转载于:https://my.oschina.net/u/2308739/blog/744419

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值