b.T
b.T

Reputation: 9

Acknowledge when the client has completely processed the message. Rabbitmq

var consumer = new EventingBasicConsumer(channel);
consumer.Received +=  (model, ea) =>
{
    var eventName = ea.RoutingKey;
    var message = Encoding.UTF8.GetString(ea.Body);
              
    ProcessEvent(eventName, message);
    channel.BasicAck(ea.DeliveryTag, multiple: false);
};

channel.BasicConsume(queue: _queueName,
    autoAck: false,
    consumer: consumer);
 private void ProcessEvent(string eventName, string message)`
 {
 //code send the acknowledgement here, before completing the processing
   if (_subsManager.HasSubscriptionsForEvent(eventName)`
   {
    using (var scope = _autofac.BeginLifetimeScope(AUTOFAC_SCOPE_NAME))`
     {
      var subscriptions = _subsManager.GetHandlersForEvent(eventName);`
        foreach (var subscription in subscriptions)`
          {
             if (subscription.IsDynamic)`
        { 
          var handler = scope.ResolveOptional(subscription.HandlerType) as IDynamicIntegrationEventHandler;
          dynamic eventData = JObject.Parse(message);`
            handler.Handle(eventData);                         
        }

I need to acknowledge when the client has completely processed the message using RabbitMQ. In my application, acknowledgement comes once consumer constructor is called. I want it to be done only after processing is finished. I have created a common class for message Broker. but I want this functionality for a particular subscribe event. Here is my code, I am processing the event**

Upvotes: 0

Views: 633

Answers (1)

user11044402
user11044402

Reputation:

The documentation shows us how to do this:

var consumer = new EventingBasicConsumer(channel);
consumer.Received += (ch, ea) =>
                {
                    var body = ea.Body;
                    // ... process the message

                    // ACK the message
                    channel.BasicAck(ea.DeliveryTag, false);
                };
String consumerTag = channel.BasicConsume(queueName, false, consumer);

See also https://www.rabbitmq.com/confirms.html

Upvotes: 2

Related Questions