Consumer channel is created for each message

93 views
Skip to first unread message

Yash Kumar Bajracharya

unread,
Jul 24, 2018, 8:40:30 PM7/24/18
to masstransit-discuss
I am using MassTransit to publish and consume messages in RabbitMQ. 
While publishing, new BusControl needs to be created for each message.
At consumer side, there is always single bus control.
Below is sample code replicating my scenario,

using MassTransit;
using System;
using System.Threading.Tasks;
class Program
{
    static void Main(string[] args)
    {
        var bus = Bus.Factory.CreateUsingRabbitMq(sbc =>
        {
            var host = sbc.Host(new Uri("rabbitmq://localhost"), h =>
            {
                h.Username("guest");
                h.Password("guest");
            });

            sbc.ReceiveEndpoint(host, "TestQueue", endpoint =>
            {
                endpoint.Consumer<SampleConsumer>();
            });
        });
        bus.Start();

        for (int i = 0; i < 100; i++)
        {
            var bus1 = Bus.Factory.CreateUsingRabbitMq(sbc =>
            {
                var host = sbc.Host(new Uri("rabbitmq://localhost"), h =>
                {
                    h.Username("guest");
                    h.Password("guest");
                });
            });
            bus1.Start();

            IRequestClient<MyMessage, Response> client =
                new MessageRequestClient<MyMessage, Response>(bus1, new Uri("rabbitmq://localhost/TestQueue"), new TimeSpan(0, 0, 30));
            var myMessage = new MyMessage { Guid = Guid.NewGuid(), Value = "Hello, World : " + i.ToString() };
            using (Task<Response> task = client.Request(myMessage))
            {
                var result = task.Result;
                Console.WriteLine($"Output for {myMessage.Value} is {result.Value};");
            }

            bus1.Stop();
        }
        Console.ReadLine();
    }
}

public class MyMessage
{
    public Guid Guid { get; set; }
    public string Value { get; set; }
}

public class Response
{
    public Guid Guid { get { return Guid.NewGuid(); } }
    public DateTime Value { get { return DateTime.Now; } }
}

public class SampleConsumer : IConsumer<MyMessage>
{
    public async Task Consume(ConsumeContext<MyMessage> context)
    {
        await Console.Out.WriteLineAsync($"Consumer : {context.Message.Value}");
        var response = new Response();
        context.Respond(response);
    }
}

After executing this sample code, RabbitMQ management shows 100+ channels. If I use single bus to publish all messages, then only 3 channels are created. 
So, what am I missing/should do so that too many channels are not created?

PS: I understand that if I use single bus at producer side, this issue will not occur. But design of the application where it should be used does not allow this. My code goes into an assembly, and this assembly is in turn called by main application which is not under my control.

Chris Patterson

unread,
Jul 25, 2018, 8:08:26 AM7/25/18
to masstrans...@googlegroups.com
You need to rethink your assembly design, and start one bus, and somehow register it to be stopped when the process exits.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstransit-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/2fdb158a-a7f4-4363-95e7-0d3aa2937590%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages