MassTransit how to handle Start/Stop when Queue is down

3.9k views Asked by At

I am using RabbitMQ 3.6.0 with MasstTransit 3.2.0 and configure it as following:

// MassTransit
IBusControl bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
    var host = cfg.Host(new Uri(configuration.Url), h =>
    {
        h.Username(configuration.User.UserName);
        h.Password(configuration.User.Password);
    });

    cfg.UseXmlSerializer();

    cfg.ReceiveEndpoint(host, configuration.Queue, e =>
    {
    });
});

And I start it as following:

// start the bus
IBusControl bus = container.Resolve<IBusControl>();
bus.Start();

Unfortunately when RabbitMQ is down, and this is a scenario I have in my architecture, the method bus.Start() hang for a while until it bombs and throw an exception. Is there an alternative pattern that I can use so the application notifies via "log" that the Queue is down and I can try later to run bus.Start()?

1

There are 1 answers

0
Rob Willis On

There is no need to manually retry bus.Start() as if you handle the exception thrown (RabbitMqConnectionException), MassTransit (we're using v3.5.5) will keep on trying to establish a RabbitMQ connection in the background. Calling bus.Start() a second time will always succeed even if RabbitMQ is still unavailable.

In this scenario we catch the exception thrown on bus.Start() and evaluate it to see if it's a transient or permanent fault. If its permanent then terminate the application. If its transient then we start the application and then use Polly to retry any important bus.Publish()/bus.Send() calls that fail whilst RabbitMQ is offline.