Configuring & Using Message Queues

We would like to incorporate fire-and-forget message handling in our Restful API - we’re very unsure of how to properly implement this.

Here’s a sample of our AppHost configuration where we configure the client manager and register the handlers in our web application:

And here’s a sample piece of code where we’d like to fire the event in one of our services:

It’s in our roadmap to eventually fully support a SOA with proper request/response handling via message queues, so if anyone can help steer us in the right direction would be greatly appreciated.

Thanks!

Since you’ve registered an MQ Server in your AppHost you can publish messages in your Services with:

base.MessageProducer.Publish(new SendEmail { Email = "test@email.com" });

Thanks for the prompt reply and helping us solve our issue. We had everything configured correctly except for failing to declare the RedisMessageFactory; once we added that, everything worked as intended.

Is it possible to block until we receive a response from the handler?

For that you’d need to use the IMessageQueueClient and it’s Get<T> API, e.g:

public IMessageFactory MqFactory { get; set; }

public object Any(MyRequest request)
{
    using (var mqClient = MqFactory.CreateMessageQueueClient())
    {
        mqClient.Publish(new SendEmail { Email = "test@email.com" });
        var response = mqClient.Get<SendEmailResponse>(
            QueueNames<SendEmailResponse>.In, TimeSpan.FromMinutes(1));
    }
}

Which uses the MQ Request/Reply pattern, which for the Redis MQ Client needs to periodically poll behind the scenes. Although I’d advise against blocking on MQ Services since if you’re going to rely on time-coupling for your System workflow you may as well call a Web Service directly and not go through an intermediary broker since you’re relying on your endpoint being available at the time you call it.