向 MQ Topic 发布消息时是否可以使用内置关联?

发布于 2024-12-14 18:56:42 字数 2397 浏览 3 评论 0原文

我只是对 WebSphere MQ 7 中主题的使用有点迷失。假设简单的场景:

  • 我有一个向主题发布消息的流程。
  • 我有零个或多个订阅者接收该消息。
  • 任何订阅者都可以决定对收到的消息做出反应=可选的“回复”。 (旁注:最初的决定是也使用主题进行回复,但如有必要,我会将其更改为队列)已发布的消息可以配置 ReplyToQueueName ,也可以是客户端上的系统级配置。
  • 响应必须与原始发布的“请求”相关(因为可能有多个发布的请求)。

现在该怎么做呢?我尝试使用 MQ 队列中的常见方法,其中发起者选择:

  • 将报告设置为 MQRO_COPY_MSG_ID_TO_CORREL_ID。接收方将从请求中获取消息 ID,并将其作为响应中的相关 ID 传递。
  • 将报告设置为 MQRO_PASS_CORREL_ID + 在请求消息中设置相关 ID。接收方将从请求中获取相关 ID,并将其设置为响应消息中的相关 ID。

在这两种情况下,发送方都知道传递给请求的消息 ID 或关联 ID,因此它可以正确关联消息,但这不适用于 MQ 主题。

如果我尝试遵循上述方法,则在发布请求时会立即遇到问题 - 消息 ID 未在发布者端设置(它为空),因此第一种方法是不可能的。如果我尝试在发布者端设置关联 ID,我将在订阅者端得到不同的关联 ID,因此我无法将其用于消息关联(= 第二种方法也不可能)。

以下是使用主题时发生的一些示例(ID 采用 Base64):

使用 MQRO_COPY_MSG_ID_TO_CORREL_ID 进行测试:

Send request: // After sending message publisher doesn't know its ID!
Message Id:     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Correlation Id: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

Received request: // Subscriber gets some ID and correlation ID (but publisher didn't set any)
Message Id:     QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYH
Correlation Id: QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYF

Send response:  // Subscriber correctly uses message ID of incoming request
Message Id:     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Correlation Id: QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYH

Received response: // Publisher gets in response completely different IDs!
Message Id:     QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYP
Correlation Id: QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYN

使用 MQRO_PASS_CORREL_ID 进行测试:

Send request: // Publisher doesn't know its ID but it generated some random correlation ID.
Message Id:     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Correlation Id: Chhv8KT+zhTjfAEdzeiB3cizw9vRy9Pf

Received request: // Subscriber got ANOTHER correlation ID!
Message Id:     QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYY
Correlation Id: QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYW

Send response:  // Subscriber is using incorrect correlation ID.
Message Id:     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Correlation Id: QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYW

Received response: // Publisher gets in response completely different IDs!
Message Id:     QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYg
Correlation Id: QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYe

如何强制 MQ 不更改我的 ID?我用 Java 和 .NET 编写了测试应用程序,两者的行为方式相同。或者什么是适合我的用例的正确方法?

I'm just little bit lost in usage of topics in WebSphere MQ 7. Suppose simple scenario:

  • I have a process publishing messages to a topic.
  • I have zero or more subscribers receiving the message.
  • Any subscriber can decide to react on received message = optional "reply". (Side note: Initial decision was to use topics for replies as well but I will change it to queue if necessary) The published message can have either ReplyToQueueName configured or it can be system level configuration on client.
  • The response must be correlated to original published "request" (because there can be more then one published request).

Now how to do this? I tried to use common approach from MQ queues where the initiator choose either:

  • Set report to MQRO_COPY_MSG_ID_TO_CORREL_ID. Receiver will take message ID from request and pass it as Correlation ID in response.
  • Set report to MQRO_PASS_CORREL_ID + set Correlation ID in the request message. The receiver will take Correlation ID from request and set it as Correlation ID in response message.

In both cases the sender is aware of message ID or correlation ID passed to request so it can correctly correlate messages but this doesn't work with MQ topics.

If I try to follow the mentioned approaches I have a problem immediately when publishing the request - the message ID is not set on publisher side (it is empty) so the first approach is not possible. If I try to set correlation ID on publisher side I will get different one on subscriber side so I cannot use it for message correlation (= second approach is not possible as well).

Here is some example what happens when using topics (IDs are in Base64):

Test using MQRO_COPY_MSG_ID_TO_CORREL_ID:

Send request: // After sending message publisher doesn't know its ID!
Message Id:     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Correlation Id: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

Received request: // Subscriber gets some ID and correlation ID (but publisher didn't set any)
Message Id:     QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYH
Correlation Id: QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYF

Send response:  // Subscriber correctly uses message ID of incoming request
Message Id:     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Correlation Id: QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYH

Received response: // Publisher gets in response completely different IDs!
Message Id:     QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYP
Correlation Id: QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYN

Test using MQRO_PASS_CORREL_ID:

Send request: // Publisher doesn't know its ID but it generated some random correlation ID.
Message Id:     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Correlation Id: Chhv8KT+zhTjfAEdzeiB3cizw9vRy9Pf

Received request: // Subscriber got ANOTHER correlation ID!
Message Id:     QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYY
Correlation Id: QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYW

Send response:  // Subscriber is using incorrect correlation ID.
Message Id:     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Correlation Id: QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYW

Received response: // Publisher gets in response completely different IDs!
Message Id:     QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYg
Correlation Id: QU1RIExNLnF1ZXVlLm1hbp2OiU4g6AYe

How to force MQ to not alter my IDs? I wrote test applications in both Java and .NET and both behaves in the same way. Or what is correct approach for my use case?

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

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

发布评论

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

评论(2

笑咖 2024-12-21 18:56:43

您是否考虑过使用 JMS(MQ 作为实现)而不是直接使用 MQ api 进行编码?

JMS 已经有了 replyTo< 的概念/a> 在发布的消息中已经有了 a 关联 ID 将消息链接在一起。对于replyTo,发布者可以指定队列或主题,因此您在接收端不会受到任何限制。

Have you considered coding it with JMS (MQ as the implementation) instead of the MQ api directly?

JMS already has the concept of a replyTo in the published message and already has the concept of a correlation id to link messages together. For the replyTo, either a queue or topic can be specified by the publisher, so you are not tied to either on the receiving end.

樱花落人离去 2024-12-21 18:56:43

消息Id在消息系统中是唯一的。在发布/订阅模式下,消息传递引擎将发布者发送的消息的副本发布给多个(和未知)订阅者。每个消息副本都会有一个不同的消息 ID,因为消息 ID 必须是唯一的。

编辑:

再次通读信息中心,这是我发现的。

您可以使用 MQSO_SET_CORREL_ID 选项强制队列管理器保留应用程序设置的 correlId。

int subOptions = MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_INPUT_SHARED | MQC.MQSO_SET_CORREL_ID;
MQTopic sub = qm.AccessTopic(recvQ, "/TEST/CORRELID", null, subOptions);

必须注意的是,如果您在创建订阅时使用 MQSO_MANAGED 选项,则无法使用 MQSO_SET_CORREL_ID

发布时将 correlID 设置为

putMsg.CorrelationId = Encoding.ASCII.GetBytes("THISISTHECORRELID");
topicCorrelId.Put(putMsg);

接收发布

gmo.MatchOptions = MQC.MQMO_MATCH_CORREL_ID;
fwdMsg.CorrelationId = Encoding.ASCII.GetBytes("THISISTHECORRELID"); 

sub.Get(fwdMsg, gmo);

时,仅当您使用队列进行多个订阅并且只想获取特定主题的发布时,您才可以使用匹配相关 ID 选项,例如使用 MQMO_MATCH_CORREL_ID

Message Id is unique in a messaging system. In publish/subscribe mode, the messaging engine publishes a copy of the message sent by the publisher to multiple(and unknown) subscribers. Each of copy the message will have a different message id as message id has to be unique.

Edit:

Read through the InfoCenter again and here is what I found.

You can use MQSO_SET_CORREL_ID option to force a queue manager to retain the correlId set by the application.

int subOptions = MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_INPUT_SHARED | MQC.MQSO_SET_CORREL_ID;
MQTopic sub = qm.AccessTopic(recvQ, "/TEST/CORRELID", null, subOptions);

It must be noted that MQSO_SET_CORREL_ID can not be used if you are using MQSO_MANAGED option when creating a subscription.

When publishing set the correlID as

putMsg.CorrelationId = Encoding.ASCII.GetBytes("THISISTHECORRELID");
topicCorrelId.Put(putMsg);

When receiving publication you can use match correl id option like

gmo.MatchOptions = MQC.MQMO_MATCH_CORREL_ID;
fwdMsg.CorrelationId = Encoding.ASCII.GetBytes("THISISTHECORRELID"); 

sub.Get(fwdMsg, gmo);

Use MQMO_MATCH_CORREL_IDonly if you are using a queue for multiple subscription and you want to get only publications for a specific topic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文