【问题标题】:ActiveMQ Artemis publish message loss during HA fail-over在 HA 故障转移期间 ActiveMQ Artemis 发布消息丢失
【发布时间】: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。 按照我为模拟故障转移所做的步骤

  1. node1 和 node2 已启动
  2. node1 作为实时服务器启动,node2 作为备份服务器启动
  3. 杀死 node1,node2 成为活动服务器
  4. 客户端发布代码抛出ActiveMQUnBlockedException并处理再次发送消息
  5. 再次启动 node1。 node1 成为实时服务器,node2 再次成为备份
  6. 客户端发布代码未抛出 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


【解决方案1】:

您得到的ActiveMQUnBlockedException 来自Spring 对javax.jms.Connection#stop 的调用。它与发送消息无关。当您收到此特定异常时重新发送消息可能会导致重复消息。

最终您的问题与设置blockOnDurableSend=false 直接相关。这告诉客户“解雇并忘记”。换句话说,客户端不会等待代理的响应来确保消息实际成功发送。无需等待会提高吞吐量,但会降低可靠性。

如果您真的想减少潜在的消息丢失,您有两个主要选择。

设置blockOnDurableSend=true。这会降低消息吞吐量,但这是保证消息成功到达代理的最简单方法。

使用CompletionListener。这将允许您保留blockOnDurableSend=false,但如果发送消息时出现问题,应用程序仍会收到通知,尽管信息将异步提供。此功能是在 JMS 2 中专门针对这种场景添加的。有关详细信息,请参阅 JavaDoc。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-01
    • 2021-02-15
    • 2021-10-13
    • 2020-06-09
    • 2016-05-19
    • 2012-01-17
    • 2022-01-04
    • 1970-01-01
    相关资源
    最近更新 更多