简体   繁体   English

一个事件多个消费者(只有一个消费者工作)Rabbitmq

[英]One event multiple consumers (only one consumer working) Rabbitmq

MyProject.Core
MyProject.Services.DataImporter
Myproject.Services.Cars
Myproject.Services.Cars2

Both DataImporter and Cars projects are referencing MyProject.Core project. DataImporterCars项目都引用了 MyProject.Core 项目。 I have an event ( DataImportFinishedEvent ) that is emitted by the DataImporter service.我有一个由DataImporter服务发出的事件 ( DataImportFinishedEvent )。 Both Services.Cars and Services.Cars2 are subscribed to this event. Services.CarsServices.Cars2都订阅了此事件。

When the event is fired I am able to fetch this event in Services.Cars and after I process the event I cannot fetch it in Services.Cars2 .触发事件后,我可以在Services.Cars中获取此事件,而在处理该事件后,我无法在Services.Cars2中获取它。 If I disable Services.Cars then I'm able to fetch the event on Services.Cars2 successfully.如果我禁用Services.Cars ,那么我可以成功获取Services.Cars2上的事件。

In short, I cannot have multiple subscribers for one event, as soon as the event is processed it's like no longer exists for other consumers.简而言之,我不能为一个事件拥有多个订阅者,一旦处理了该事件,其他消费者就不再存在了。

Not sure what code should I share but here how's my event and event handler looks like.不知道我应该分享什么代码,但这里我的事件和事件处理程序是什么样子的。

public class DataImportFinishedEvent: Event
{
   public string Data { get; set; }
}

public class DataImportFinishedEventHandler : IEventHandler<DataImportFinishedEvent> {
   public Task Handle(DataImportFinishedEvent @event)
   {
        return Task.FromResult(true);
   }
}

In case you need more info I'm injecting RabbitMQBus implementation in both services (.net core)如果您需要更多信息,我将在两个服务(.net 核心)中注入 RabbitMQBus 实现

 services.AddSingleton<IEventBus, RabbitMQBus>(sp =>
 {
      var scopeFactory = sp.GetRequiredService<IServiceScopeFactory>();
      return new RabbitMQBus(sp.GetService<IMediator>(), scopeFactory);
 });

Please let me know if you need more info from the RabbitMQBus implementation.如果您需要RabbitMQBus实现的更多信息,请告诉我。

When you upload a message in a queue in RabbitMQ that message will be consumed only once .当您在 RabbitMQ 的队列中上传消息时,该消息将仅被使用一次

In order to achive the same message to be consumed by several consumers you have to use a fan-out exchange .为了让多个消费者使用相同的消息,您必须使用扇出交换 In the link attached you can find a very descriptive post of how this concept works.在附加的链接中,您可以找到有关此概念如何工作的非常描述性的帖子。

What is happening in the background is that when you uploada message in the exchange, your message is placed in several queues.后台发生的事情是,当您在交换中上传消息时,您的消息被放置在几个队列中。 And then each service can consume it's own message.然后每个服务都可以使用它自己的消息。

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

相关问题 只有一个消费者从 RabbitMQ 上的队列接收消息 - only one consumer receives messages from the queue on RabbitMQ 当我有两个消费者线程运行时,为什么只有一个 RabbitMQ 消费者接收所有消息? - Why is only one RabbitMQ consumer receiving all the messages when I have two consumer threads going? Rabbitmq C#:一个使用者应该只接受一种类型的消息,还是我们可以使用NameValueCollection发送不同类型的消息? - Rabbitmq c#: Should one consumer accept only one type of message or we can send different type of message using NameValueCollection? 多个 RabbitMQ 消费者 DotNet Core - Multiple RabbitMQ Consumers DotNet Core 与一个生产者和多个消费者一起使用Queue是否安全? - Is it safe to use Queue with one producer and multiple consumers 一个事件处理程序中的多个按钮不起作用 - Multiple buttons in a one event handler not working Windows 服务上的 RabbitMQ 多个唯一消费者 - RabbitMQ Multiple Unique Consumers on Windows Service 使用 RabbitMQ 消费者线程处理事件 - Working on events with threads RabbitMQ consumer 一个接收端点上有多个相同消息类型的消费者 - Multiple Consumers of the same message type on one receive endpoint 一个快速生产者多个慢消费者的最佳方案是什么? - What is the best scenario for one fast producer multiple slow consumers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM