Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get IModel.BasicAcks to fire?

Tags:

c#

.net

rabbitmq

I'm playing with RabbitMQ's .NET API for the first time, and I came up with a use case that seems plausible to me: I want to create publisher that publishes messages and does something after they've been Ack-ed. the IModel.BasicAcks event seemed like a decent way to find out about this, so--

I wrote a publisher:

       private static void Post(string message) {

            model.ExchangeDeclare("MyExchange", ExchangeType.Fanout, true);
            model.QueueDeclare("MyQueue", true, false, false, null);
            model.QueueBind("MyQueue", "MyExchange", "", new Dictionary<string, object>());

            byte[] messageBodyBytes = System.Text.UTF8Encoding.ASCII.GetBytes(message);
            IBasicProperties props = model.CreateBasicProperties();
            props.ContentType = "text/plain";
            props.DeliveryMode = 2;
            model.BasicPublish("MyExchange", "", props, messageBodyBytes);
    }

And a subscriber:

    private static void Receive() {
            var gotten = model.BasicGet("MyQueue", false);
            var text = System.Text.UTF8Encoding.ASCII.GetString(gotten.Body);
            Console.WriteLine(text);
            model.BasicAck(gotten.DeliveryTag, false);
    }

And this is the entry point for the Console App:

        static void Main(string[] args) {

        connectionFactory = new ConnectionFactory();
        connectionFactory.HostName = "localhost";
        connection = connectionFactory.CreateConnection();
        model = connection.CreateModel();
        model.BasicAcks += new RabbitMQ.Client.Events.BasicAckEventHandler(model_BasicAcks);
        Post("Hello, World!");
        Receive();
        Console.ReadKey();

        connection.Dispose();
        model.Dispose();

    }

For some reason, my event handler is not being called. The "Hello, World!" message gets published, read, Acked, and printed to the console, but for some reason the event handler is never called.

Am I doing something wrong? Subscribing to IModel.ModelShutdown seemed to work just fine.

like image 548
Luke Winikates Avatar asked Sep 06 '25 18:09

Luke Winikates


1 Answers

I'll refer the curious to the response that I got from the RabbitMQ team on this question.

Here's what they had to say

Briefly put, the event fires, but it's not what I thought it might be-- it's for Publisher Confirms, explained in this RabbitMQ blog post

like image 137
Luke Winikates Avatar answered Sep 09 '25 14:09

Luke Winikates