问题
Have a newbie question about Mass Transit ESB
I am trying MassTransit for the first time and trying to get my head around how the queues are created and how messages are consumed.
I have a web application and a Console application trying to do publish / consume respectively
This is my initialization code.
var bus = Bus.Factory.CreateUsingRabbitMq(sbc =>
{
var host = sbc.Host(new Uri(hostName), h =>
{
h.Username(userName);
h.Password(password);
});
});
Then from the web app i call the following code.
using (Bus.Start())
{
var pubr = Bus.Publish<T>(message);
pubr.Wait();
}
This leads to the message being lost in Rabbit MQ .
I can get the sample to work if I add a consumer in the console application.
sbc.ReceiveEndpoint(host,
e =>
e.Consumer<LoginEventConsumer>(d => { })
{ }
My question is why does my message get lost if there are no consumers ?
It looks like Rabbit MQ believes that there is no queue connected to the exchange and hence the message is lost . Is that correct ? Is there a way to create queues and exchange together during initialization without cluttering Rabbit MQ with lots of randomly named queues / exchanges ?
Looks like i am missing something very basic in my understanding of how MassTransit / Rabbit MQ works. I would have thought its a very common scenario that a consumer registration happens at a later point than the publish event and that the consumer will be sent all the items that have been published once it connects .
回答1:
RabbitMQ consists of exchanges and queues.
Exchanges are created by MassTransit when publishing messages, based on the message types. Publishing does not create any queues. Queues are where messages are stored for delivery to consumers.
Queues are created when receive endpoints are added to a bus. For the consumers, handlers, and sagas added to a receive endpoint, the exchanges are created and bound so that messages published to the exchanges are received by the receive endpoint (via the queue).
Until the receive endpoints are started, and their topology configured in RabbitMQ (the exchange-exchange-queue bindings), any messages published are not delivered because there are no bindings to any queues. Once the receive endpoints are started, those bindings exist and messages will be delivered to the queues.
来源:https://stackoverflow.com/questions/33633045/mass-transit-no-consumer