【问题标题】:Impossible to read queue message with Masstransit无法使用 Masstransit 读取队列消息
【发布时间】:2021-09-19 20:05:34
【问题描述】:

我有一个队列,其中包含一些消息(使用 masstransit 创建)。

我尝试了这段代码来获取消息(见下文)。

我希望在 Console.Out 行上收到消息,但我从未点击过此行,并且消息仍在队列中。我没有收到任何错误。

有什么想法吗?

 class Program
    {
        static void Main(string[] args)
        {
            var bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
            {
    
                cfg.Host("localhost", "/", h =>
                {
                    h.Username("guest");
                    h.Password("guest");
                });
    
                cfg.ReceiveEndpoint("myQueue", e =>
                {
                    e.Handler<ProcessingQueue>(context =>
                    {
                        return Console.Out.WriteLineAsync($"{context.Message.Id}");
                    });
    
                });
    
            });
        }
    }
    
    public class ProcessingQueue
    {
        public int Id { get; set; }
    
        public string Name { get; set; }
    }

谢谢,

我尝试添加:

bus.Start();
Console.WriteLine("Receive listening for messages");
Console.ReadLine();
bus.Stop();

但是当我这样做时,会创建一个新队列myQueue_skipped,其中包含我的消息。

【问题讨论】:

    标签: c# .net rabbitmq message-queue masstransit


    【解决方案1】:

    如果消息被移动到 _skipped 队列,则表明这些消息未被配置在该接收端点上的任何消费者使用。最常见的错误 as highlighted at the top of the message documentation 是命名空间不匹配。

    类似答案:here

    【讨论】:

    • 谢谢克里斯,这很好。额外问题:我有一个包含 100 条消息的队列。在某些情况下,我想发送一条消息,但首先应该优先处理此消息,在现有的 100 条消息之前。有没有办法做到这一点?
    • 欢迎您提出这个新问题。
    【解决方案2】:

    尝试将此代码用于 ReceiveEndpoint

    cfg.ReceiveEndpoint("myQueue", e =>
    {
        e.Consumer<MessagesConsumer>();
    });
    

    “MessagesConsumer”必须继承自 IConsumer

    public class MessagesConsumer: IConsumer<ProcessingQueue>      
    {   public async Task Consume(ConsumeContext<ProcessingQueue> context)
            {
                 //access to the properties
                 var name=context.Message.Name;
                 var id=context.Message.Id;
            }
    }
    

    在 Consume 方法中,您将收到“ProcessingQueue”类型的消息。您可以在此处访问属性..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-09
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      • 1970-01-01
      • 2020-05-02
      相关资源
      最近更新 更多