【发布时间】:2021-03-23 22:32:34
【问题描述】:
我使用 ActiveMQ Artemis 2.17.0,我希望避免故障转移期间生产者中的消息丢失。
在 Artemis 主动到被动切换期间通过捕获 ActiveMQUnBlockedException 并再次发送消息来处理消息发布丢失。
代理配置为主动/被动 HA 共享存储。在host1 中配置的主动节点和在host2 中配置的被动节点。
网址是:
(tcp://host1:61616,tcp://host2:61616)?ha=true&reconnectAttempts=-1&blockOnDurableSend=false
blockOnDurableSend 设置为 false 以获得高吞吐量。
在主动到被动切换发布代码期间会抛出 ActiveMQUnBlockedException,但在被动到主动切换期间不会。
我们使用 Spring 4.2.5 和 CachingConnectionFactory 作为连接工厂。
我正在使用以下代码发送消息:
private void sendMessageInternal(final ConnectionFactory connectionFactory, final Destination queue, final String message)
throws JMSException {
try (final Connection connection = connectionFactory.createConnection();) {
connection.start();
try (final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final MessageProducer producer = session.createProducer(queue);) {
final TextMessage textMessage = session.createTextMessage(message);
producer.send(textMessage);
}
} catch (JMSException thr) {
if (thr.getCause() instanceof ActiveMQUnBlockedException) {
// consider as fail-over disconnection, send message again.
} else {
throw thr;
}
}
}
在 host1 机器中,Artemis 部署为 master - node1。 在 host2 机器中,Artemis 部署为从机 - node2。 按照我为模拟故障转移所做的步骤
- node1 和 node2 已启动
- node1 作为实时服务器启动,node2 作为备份服务器启动
- 杀死 node1,node2 成为活动服务器
- 客户端发布代码抛出
ActiveMQUnBlockedException并处理再次发送消息 - 再次启动 node1。 node1 成为实时服务器,node2 再次成为备份
- 客户端发布代码未抛出
ActiveMQUnBlockedException并丢失消息。
在第 3 步期间获取以下错误堆栈。 (杀死的node1和node2成为Live server)。
javax.jms.JMSException: AMQ219016: Connection failure detected. Unblocking a blocking call that will never get a response
at org.apache.activemq.artemis.core.protocol.core.impl.ChannelImpl.sendBlocking(ChannelImpl.java:540)
at org.apache.activemq.artemis.core.protocol.core.impl.ChannelImpl.sendBlocking(ChannelImpl.java:434)
at org.apache.activemq.artemis.core.protocol.core.impl.ActiveMQSessionContext.sessionStop(ActiveMQSessionContext.java:470)
at org.apache.activemq.artemis.core.client.impl.ClientSessionImpl.stop(ClientSessionImpl.java:1121)
at org.apache.activemq.artemis.core.client.impl.ClientSessionImpl.stop(ClientSessionImpl.java:1110)
at org.apache.activemq.artemis.jms.client.ActiveMQSession.stop(ActiveMQSession.java:1244)
at org.apache.activemq.artemis.jms.client.ActiveMQConnection.stop(ActiveMQConnection.java:339)
at org.springframework.jms.connection.SingleConnectionFactory$SharedConnectionInvocationHandler.localStop(SingleConnectionFactory.java:644)
at org.springframework.jms.connection.SingleConnectionFactory$SharedConnectionInvocationHandler.invoke(SingleConnectionFactory.java:577)
at com.sun.proxy.$Proxy5.close(Unknown Source)
at com.eu.amq.failover.test.ProducerNodeTest.sendMessageInternal(ProducerNodeTest.java:133)
at com.eu.amq.failover.test.ProducerNodeTest.sendMessage(ProducerNodeTest.java:110)
at com.eu.amq.failover.test.ProducerNodeTest.main(ProducerNodeTest.java:90)
【问题讨论】:
-
我所说的“从被动切换到主动切换”是指当我杀死主动的 Artemis 服务器时,被动的 Artemis 服务器将变为主动,模拟故障转移场景。
-
编辑帖子以添加消息发布代码。
ActiveMQUnBlockedException在主动到被动故障转移期间正确抛出,但在反向切换期间不正确。 -
编辑帖子以添加我模拟故障转移的步骤。
-
编辑帖子以添加异常详细信息。由于我在操作之前启动连接,因此即使我设置了
blockOnDurableSend=false,也要关闭/停止调用 sendBlocking。 -
现在我尝试使用
blockOnDurableSend=true和不使用connection.start(),在两种情况下(步骤 #3 和 #5)都得到 `ActiveMQUnBlockedException'。我能够避免消息丢失。设置 'blockOnDurableSend=true' 是在 Artemis 故障转移期间避免消息丢失的唯一方法吗?
标签: activemq-artemis