[英]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. DataImporter
和Cars
项目都引用了 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.Cars
和Services.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.