如何读取主题的保留消息

发布于 2025-01-11 09:43:10 字数 1064 浏览 0 评论 0原文

我有一个 Web 应用程序,它向一个主题发布消息,然后有多个订阅这些主题的 Windows 服务,其中一些服务具有多个实例。如果发布消息时服务正在运行,则一切正常,但如果不是,则消息将保留在订阅该主题的队列中,但在服务启动备份时不会被读取。

期望的行为 -

  1. 当消息发布到主题字符串 MyTopic 时,它会被读取 仅从 MyTopicQueue 中一次。我使用一些通配符主题,因此每条消息都会发送到多个队列,但服务的多个实例订阅同一主题字符串,并且每条消息只能由这些实例读取

  2. 如果 MyTopic 主题的订阅者不是在线发布消息后,消息将保留在 MyTopicQueue 中。

  3. 当Windows服务订阅时 到一个特定的主题返回线上每条保留的消息是 仅由单个订阅者从 MyTopicQueue 读取。

我发现了一些[通常针对 IBM]关于 MQSUBRQ 和 MQSO_PUBLICATIONS_ON_REQUEST 选项的参差不齐的文档,但我不确定应该如何设置它们。有人可以帮助弄清楚我需要做什么才能获得我想要的行为吗? [除了切换回 RabbitMQ,尽管我更喜欢它,但我不能这么做。]

我的选项:

private readonly int _openOptions = MQC.MQSO_CREATE | MQC.MQSO_FAIL_IF_QUIESCING | MQC.MQSO_MANAGED;
private readonly MQGetMessageOptions _messageOptions = new MQGetMessageOptions()

打开主题的代码:

_topic = _queueManager.AccessTopic(_settings.TopicString, null, 
                    MQC.MQTOPIC_OPEN_AS_SUBSCRIPTION, _openOptions);

从主题读取的代码行(取自循环):

  _topic.Get(mqMessage, _messageOptions);

I have a web application that publishes messages to a topic then several Windows services that subscribe to those topics, some with multiple instances. If the services are running when the messages are published everything works correctly but if they are not then the messages are retained on the queue(s) subscribing to that topic but aren't read when the services start back up.

The desired behavior-

  1. When a message is published to the topic string MyTopic, it is read
    from the MyTopicQueue only once. I use some wildcard topics so each message is sent to multiple queues, but multiple instances of a services subscribe to the same topic string and each message should be read by only of those instances

  2. If the subscribers to the MyTopic topic aren't online when the message is published then the messages are retained on MyTopicQueue.

  3. When the Windows services subscribing
    to a particularly topic come back on line each retained message is
    read from MyTopicQueue by only a single subscriber.

I've found some [typically for IBM] spotty documentation about the MQSUBRQ and MQSO_PUBLICATIONS_ON_REQUEST options but I'm not sure how I should set them. Can someone please help figure out what I need to do to get my desired behavior? [Other than switching back to RabbitMQ which I can't do though I'd prefer it.]

My options:

private readonly int _openOptions = MQC.MQSO_CREATE | MQC.MQSO_FAIL_IF_QUIESCING | MQC.MQSO_MANAGED;
private readonly MQGetMessageOptions _messageOptions = new MQGetMessageOptions()

Code to open the Topic:

_topic = _queueManager.AccessTopic(_settings.TopicString, null, 
                    MQC.MQTOPIC_OPEN_AS_SUBSCRIPTION, _openOptions);

The line of code that reads from the topic (taken from a loop):

  _topic.Get(mqMessage, _messageOptions);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与风相奔跑 2025-01-18 09:43:10

如果您希望在未连接时累积消息,则需要通过添加 MQC.MQSO_DURABLE 来使订阅持久化。为了能够恢复现有订阅,除了 MQC.MQSO_CREATE 之外,还添加 MQC.MQSO_RESUME


请注意术语,您所描述的保留消息是持久订阅。

保留发布是另一回事,MQ 可以保留每个主题上最近发布的一条消息,并且默认情况下新订阅者将检索此消息,除非他们使用 MQSO_NEW_PUBLICATIONS_ONLY 跳过接收保留发布。

MQSO_PUBLICATIONS_ON_REQUEST 允许订阅者仅根据请求接收保留的发布,而不会接收非保留的发布。


如果您希望多个使用者在单个订阅上协同工作,您有两个选择:

  1. 查看 XMS.NET 中的共享订阅者,查看 CLONESUPP 属性。
  2. 创建对您想要消费的主题的队列的一次性持久订阅,然后让您的消费者直接从队列而不是主题消费。

If you want the messages to accumulate while you are not connected you need to make the subscription durable by adding MQC.MQSO_DURABLE. In order to be able to resume an existing subscription add MQC.MQSO_RESUME in addition to MQC.MQSO_CREATE.


Be careful with terminology, what you are describing as retained messages is a durable subscription.

Retained publications are something else were MQ can retain one most recently published message on each topic and this message will be retrieved by new subscribers by default unless they use MQSO_NEW_PUBLICATIONS_ONLY to skip receiving the retained publication.

MQSO_PUBLICATIONS_ON_REQUEST allows a subscriber to only receive retained publications on request, it will not receive non-retained publications.


If you want multiple consumers to work together on a single subscription you have two options:

  1. Look at shared subscribers in XMS.NET, look at the CLONESUPP property.
  2. Create a one time durable subscription to a queue on the topics you want consumed, then have your consumers directly consume from the queue not a topic.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文