Reputation: 35544
Is it a valid solution to use different consumers of the same message type on a single receive endpoint or should we use a receive endpoint for each consumer?
cfg.ReceiveEndpoint(host, "MyQueue", e =>
{
logger.LogInformation("Consuming enabled.");
//register consumers with middleware components
e.Consumer<MyConsumer>(context);
e.Consumer<MyOtherConsumer>(context);
})
public class MyConsumer : IConsumer<MyMessage> {}
public class MyOtherConsumer : IConsumer<MyMessage> {}
The solution above works, each consumer receives the message. Even if one fails (exception).
Why do I ask this? Our current solution is that we have a single consumer for each message type. The consumer passes the received message to an internal custom extensible pipeline for processing. If the above solution is viable we could drop or own custom pipeline an use MassTransit instead.
Upvotes: 4
Views: 539
Reputation: 33258
Yes, you can have multiple consumers registered on the same endpoint, for the same message type, and MassTransit will handle dispatching the message to those consumers.
You can also customize the endpoint pipeline, as well as each consumer's pipeline, so that different filters can be applied to different consumers.
ec.Consumer<MyConsumer>(context, c => c.UseRetry(r => r.Interval(2,1000)));
ec.Consumer<MyOtherConsumer>(context, c => c.UseRetry(r => None()));
This was one of the core reasons MT was rewritten to be built around pipelines (this was years ago, but nonetheless) and how GreenPipes was created.
As a side note, you could put each consumer on a separate endpoint, and publish the message, which would give each consumer its own copy - and own execution context (including retry and broker error handling) if needed.
Upvotes: 4