Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MassTransit RabbitMq Sending Messages

I am not able to figure out on how to specify the Exchange and Queue in my GetSendEndpoint()) task when sending / publishing messages?

As per MassTransit documentation https://masstransit-project.com/usage/producers.html#send you can specify the exchange and queue like

GetSendEndpoint(new Uri("queue:input-queue"))

However, I can only do one or the other?

Is there an alternative way of sending with exchange and queue specified?

I am doing this in Asp.Net Core so here are my configuration:

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
   services.AddMassTransit();

   services.AddSingleton(p => Bus.Factory.CreateUsingRabbitMq(cfg =>
   {
         cfg.Host("rabbitmq://localhost", h =>
         {
             h.Username("admin");
             h.Password("admin");
         });
   }));

   services.AddSingleton<IBus>(p => p.GetRequiredService<IBusControl>());
   services.AddSingleton<IHostedService, BusService>();
}

And this is how send the message

var endpoint = await _bus.GetSendEndpoint(new Uri(queue:Test.Queue));
await endpoint.Send(new Message()
{
     Text = "This is a test message"
});

As you can see I can only specify the queue name.

like image 325
KJSR Avatar asked Dec 23 '22 19:12

KJSR


1 Answers

If you specify an exchange, only the exchange is declared on the broker. The message will be sent directly to the exchange.

"exchange:your-exchange-name"

If you specify a queue, the queue along with an exchange of the same name will be declared and the exchange will be bound to the queue. Messages will be delivered to the exchange of the same name, which will deliver them to the queue.

"queue:your-queue-name"

If you want a different exchange and queue name, you can specify both using:

"exchange:your-exchange-name?bind=true&queue=your-queue-name"

Or you could simplify, but it's a little confusing with two queues:

"queue:your-exchange-name&queue=your-queue-name"
like image 81
Chris Patterson Avatar answered Jan 10 '23 02:01

Chris Patterson