RADU
RADU

Reputation: 108

.Net Core RabbitMQ/Masstransit one consumer per configurable number of threads in same app

I can create a workerservices that create and configure a n number of RabbitMQ consumer, each one in is own thread for in-app load balancing.

I am new to Masstransit and I don not know how to configure the way I want. I found this code to consume message type queues in configureservices:

   // MassTransit-RabbitMQ Configuration
        services.AddMassTransit(config => {

            config.AddConsumer<BasketCheckoutConsumer>();

            config.UsingRabbitMq((ctx, cfg) => {
                cfg.Host(Configuration["EventBusSettings:HostAddress"]);
                cfg.UseHealthCheck(ctx);

                cfg.ReceiveEndpoint(EventBusConstants.BasketCheckoutQueue, c => {
                    c.ConfigureConsumer<BasketCheckoutConsumer>(ctx);
                });
            });
        });
        services.AddMassTransitHostedService();

        // General Configuration
        services.AddScoped<BasketCheckoutConsumer>();

The question is, if I run a loop from 0 to n-1 running this line services.AddScoped<BasketCheckoutConsumer>();

will this crate n number of consumers? this is, consumers taking message from the same queue.

Am I in good direction?

Upvotes: 2

Views: 1346

Answers (1)

Alexey Zimarev
Alexey Zimarev

Reputation: 19600

Consumers in MassTransit aren't using threads continuously. Consumers are also instantiated in a scope of a single message handling, so talking about threads makes no sense when it comes to the number of consumer instances.

MassTransit is fully asynchronous in terms of IO, and it will instantiate multiple instances of the same consumers to handle the load. You can specify how many concurrent consumers you want for a given receive endpoint by setting ConcurtrentMessageLimit in your configuration as it's described in the docs. Remember also to adjust the prefetch count, so you actually get the concurrency level you want.

Upvotes: 2

Related Questions