在两个 ActiveMQ 代理之间转发消息时维护消息顺序

Maintain order of messages while forwarding messages between two ActiveMQ brokers

我有一个 ActiveMQ 设置,其中位于一个数据中心的源代理将所有到达特定主题的消息转发到另一个数据中心的目标代理。消费者应用程序仅使用来自目标代理的消息。 (这个设置主要是为了保证两个数据中心之间消息的快速高效转发。)

转发配置如下所示:

<networkConnectors>
  <networkConnector name="Q:DontForwardQueueMessages"
    uri="static:(tcp://destination-broker.example.com:61616)"
    duplex="false" decreaseNetworkConsumerPriority="true" networkTTL="2"
    dynamicOnly="true">
    <excludedDestinations>
      <queue physicalName=">" />
    </excludedDestinations>
  </networkConnector>
  <networkConnector name="T:ForwardSampleMessages"
    uri="static:(tcp://destination-broker.example.com:61616)"
    duplex="false" decreaseNetworkConsumerPriority="true" networkTTL="2"
    dynamicOnly="true">
    <excludedDestinations>
      <topic physicalName=">" />
    </excludedDestinations>
    <staticallyIncludedDestinations>
      <topic physicalName="SampleTopic1" />
      <topic physicalName="SampleTopic2" />
      <topic physicalName="SampleTopic3" />
      <topic physicalName="SampleTopic4" />
    </staticallyIncludedDestinations>
  </networkConnector>
</networkConnectors>

我们的应用程序需要维护消息顺序。但是,当目标代理出现故障时,我们正在丢失消息。到达源代理的消息堆积在主题中,但在重新建立与目标代理的连接时不会被转发。但是,重新连接后到达的消息将照常转发。

我正在寻找一种配置设置的方法,以便:

从主题转发消息似乎是一个糟糕的设计选择。根据 ActiveMQ documentation:

Only subscribers who had an active subscription at the time the broker receives the message will get a copy of the message.

目标代理充当源主题的订户,从中转发消息。因此,当消息在没有订阅者(目标断开连接)的情况下到达源主题时,任何人都无法使用它们。

作为解决方案,我更改了设计:

  • 删除目标代理中的虚拟目标配置
  • 在源代理中添加相同的虚拟目标配置(因此现在消息在此处分发到各自的队列中)
  • 向源代理添加 networkConnector 规则以将这些队列中的消息转发到目标代理上的相应队列。

现在,由于源端的消息在队列中,它们将按照接收到的顺序使用,并且不会丢失任何消息,即使代理彼此断开连接也是如此。