问题
I'm using the MassTransit library's InMemoryMessageBus
and I would like to know how I can get the number of messages in the queue (the size of the bus).
回答1:
The number of messages in any particular queue using the in-memory transport is not available. The message delivery is based on a queued task scheduler, and the message counts have not been made available. I'm not sure if they could be or not (well, easily. Anything is possible, but practical is another matter).
UPDATE: This was added to MassTransit and will be in the next release (3.5.x). The tracking issue is on GitHub, including example usage of the new code.
回答2:
If you are using RabbitMQ as your transport, You could use HareDu. The following snippet will get you started:
var client = HareDuFactory.New(x => x.ConnectTo(RabbitMqHostUrl));
var data = client
.Factory<VirtualHostResources>(y => y.Credentials(RabbitMqUser, RabbitMqPass))
.Queue
.GetAll()
.Data();
foreach (var queue in data)
{
/*then you can access
queue.Name, queue.VirtualHostName, queue.Memory, queue.Messages,
queue.MessagesReady, queue.MessagesUnacknowledged, queue.Node, queue.IsDurable, queue.Consumers, queue.IdleSince */
}
来源:https://stackoverflow.com/questions/33377354/how-to-get-size-number-of-messages-of-a-masstransit-ibus