简体   繁体   English

按标签从 MSMQ 读取消息

[英]Read message from MSMQ by Label

I need to create a C# based MSMQ environment where I need to be able to read messages from the queue based on some unique value which has been supplied by an external process.我需要创建一个基于 C# 的 MSMQ 环境,我需要能够根据外部进程提供的一些唯一值从队列中读取消息。 ie Ideally something like ReceiveByLabel("1234").即理想情况下类似于 ReceiveByLabel("1234")。 I can arrange things such that the label is unique but there does not seem to be easy way to do this.我可以安排一些事情,使标签独一无二,但似乎没有简单的方法可以做到这一点。 I could use he GetAllMessage enumerator but as here may be 30,000 messages on the queue this is probably going to to be very slow.我可以使用他的 GetAllMessage 枚举器,但由于队列中可能有 30,000 条消息,这可能会非常慢。

Any ideas suggestions would be much appreciated.任何想法建议将不胜感激。

I would suggest to use correlation id.我建议使用相关ID。 1. Need to build correlation id (38-42 chars length). 1.需要建立关联id(38-42个字符长度)。 Can be build in any manner, as example build it by GUID可以以任何方式构建,例如通过 GUID 构建它

    /// <summary>
    /// Generate random correlation ID(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\xxxxx)
    /// </summary>
    /// <returns>string</returns>
    /// <param name="id">if want specific id in end of generated id, else it will be random forom 1- 10000</param>
    public string GenerateId(int id = 0)
    {
        if (id == 0)
        {
            Random r = new Random();
            id = r.Next(1, 10000);
            id = r.Next(1, 10000);
        }
        return $"{Guid.NewGuid().ToString()}\\{id.ToString()}";
    }
  1. Specify message with generated correlation id.使用生成的相关 ID 指定消息。

     Message message = new Message(); string messageUniqueID = GenerateId(); message.CorrelationId = messageUniqueID;
  2. Receive it by this correlation id.通过此关联 ID 接收它。

     string yourQueuePath= "..."; MessageQueue queue = new MessageQueue(yourQueuePath); Message myMessage = queue.ReceiveByCorrelationId(messageUniqueID);

Id's can be specified and generated in code or in any other manner(save in DB etc.)可以在代码中或以任何其他方式(保存在 DB 等中)指定和生成 ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM