Reputation: 2879
I'm developing an application for messaging using Rabbit MQ. I use EventingBasicConsumer, Exchange and QueueBind. For test i run my application for receaving messages and then turn my ethernet controller off. After i turn it on receaving messges start again, but about 500 messages was lost. My code:
private void DoWork()
{
try
{
var connection = ConnectionFactory.CreateConnection();
IModel model = connection.CreateModel();
// Configure the Quality of service for the model. Below is how what each setting means.
// BasicQos(0="Dont send me a new message untill I’ve finshed", 1= "Send me one message at a time", false ="Apply to this Model only")
model.BasicQos(0, 1, false);
model.ExchangeDeclare(Options.RabbitConnectionOptions.Exchange, RabbitConstants.ExchangeType, RabbitConstants.ExchangeDurable, RabbitConstants.ExchangeAutoDelete);
var queueDeclareOk = model.QueueDeclare("SomeSubsruberQueue3", RabbitConstants.QueueDurable, RabbitConstants.QueueExclusive, RabbitConstants.ExchangeAutoDelete);
var queueName = queueDeclareOk.QueueName;
foreach (var optionsBindingKey in Options.BindingKeys)
{
Logger.Debug($"QueueBind for {nameof(optionsBindingKey)}={optionsBindingKey}");
model.QueueBind(queueName, Options.RabbitConnectionOptions.Exchange, optionsBindingKey);
}
var consumer = new EventingBasicConsumer(model);
consumer.Received += (ch, ea) =>
{
try
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
var routingKey = ea.RoutingKey;
var messageToLog = $" [x] Received '{routingKey}':'{message}'";
Logger.Info(messageToLog);
Console.WriteLine($"receavedCount={++receavedCount}");
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
};
model.BasicConsume(queueName, RabbitConstants.QueueAutoAck, consumer);
}
catch (Exception e)
{
Logger.Error(e);
Console.WriteLine(e);
throw;
}
}
How can i prevent loosing messages?
P.s. here the log, shows lost messages. "Id" is sequential, so u can see, that massages with id's from 926 to 1299 was lost
2017-02-14 10:52:01.4916 <Root><Subscribers><Subscriber Id="922" Name="1c" />
2017-02-14 10:52:01.4916 <Root><Subscribers><Subscriber Id="923" Name="1c" />
2017-02-14 10:52:01.4916 <Root><Subscribers><Subscriber Id="924" Name="1c" />
2017-02-14 10:52:01.5056 <Root><Subscribers><Subscriber Id="925" Name="1c" />
2017-02-14 10:52:22.5606 <Root><Subscribers><Subscriber Id="1300" Name="1c" />
2017-02-14 10:52:22.5606 <Root><Subscribers><Subscriber Id="1301" Name="1c" />
2017-02-14 10:52:22.5676 <Root><Subscribers><Subscriber Id="1302" Name="1c" />
2017-02-14 10:52:22.6046 <Root><Subscribers><Subscriber Id="1303" Name="1c" />
UPD:
if i modify:
model.BasicConsume(queueName,true, consumer);
to
model.BasicConsume(queueName,false, consumer);
and use explicit ASK
((EventingBasicConsumer)ch).Model.BasicAck(ea.DeliveryTag, false);
i have very strange behaviour:
2017-02-14 13:06:35.9835| DeliveryTag=23, <Subscriber Id="778" Name="1c" />
2017-02-14 13:06:36.0295| DeliveryTag=24, <Subscriber Id="779" Name="1c" />
2017-02-14 13:06:57.3285| DeliveryTag=26, <Subscriber Id="782" Name="1c" />
2017-02-14 13:06:57.3755| DeliveryTag=27, <Subscriber Id="783" Name="1c" />
There is no DeliveryTag=25 !
Upvotes: 1
Views: 2810
Reputation: 2879
Ок, thanks Emil Vikström, i must use explicit ASK:
model.BasicConsume(queueName,false, consumer);
and do ask after processing message:
((EventingBasicConsumer)ch).Model.BasicAck(ea.DeliveryTag, false);
Other question about lost message: rabbitMq reordering message when returning it to queue. So its not los, its just will receaved later
Upvotes: 1
Reputation: 91983
Don't use auto-ACK. Read a message, do whatever you want to do with it, then ACK explicitly when you are done with it.
Upvotes: 4