简体   繁体   中英

how to know if queue is empty and there are no more messages to read using MassTransit 3.0

I am using MassTransit 3.0 with RabbitMq. I want to know if my queue is empty because i need to drop the temp table from db if it is. i am not able to figure out how to see if the queue is empty.

var inventoryBus = new InventoryBus(new RabbitServiceConfig());
inventoryBus.Instance.Start();
inventoryBus.Instance.ConnectConsumer<InventoryConsumer>();

========== Consumer ===============

public class InventoryConsumer: IConsumer<IAvailableStockChanged>
{
    private readonly IInventoryService _service;
    public InventoryConsumer() {

    }
    public InventoryConsumer(IInventoryService service) {
        _service = service;
    }

    public async Task Consume(ConsumeContext<IAvailableStockChanged> context) {
        await Console.Out.WriteLineAsync("Message received....." + context.Message.Sku);
        //_service.AddOrUpdate(context.Message);
    }
}

Also, when i am performing Db operations after every message is read, i want to stop the consumer so that i dont read any pending messages.

any help would be appreciated...

The link gets you the API to check messages in the queue, as shown above.

However, I'd like to point out that the approach you are taking to setup your bus and connect your consumer is going to cause you trouble down the road.

Please look at the documentation on how to register a consumer on a specific queue:

http://docs.masstransit-project.com/en/latest/usage/consumer.html#connecting-a-message-consumer

Also, if you want to process one message at a time, you can set the prefetch count to 1 (and use a concurrency limit filter if that's too slow).

e.PrefetchCount = 1;

The concurrency limit filter would also work in addition to the above.

e.PrefetchCount = 20; // in case your messages are small and fast
e.UseConcurrencyLimit(1); // sets one at a time message handling

With the concurrency limit, you may want to set a retry policy as well.

e.UseRetry(x => x.Interval(10, 500)); // retry 10 times every 500ms

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM