Skip to content

Instantly share code, notes, and snippets.

@pdoran
Created January 12, 2015 22:11
Show Gist options
  • Save pdoran/2651cfce26fc788750e8 to your computer and use it in GitHub Desktop.
Save pdoran/2651cfce26fc788750e8 to your computer and use it in GitHub Desktop.
Count messages in a RabbtiMQ Queue
public class RawBrokerConnectionFactory {
private readonly ConnectionFactory _ConnFactory = new ConnectionFactory();
public RawBrokerConnectionFactory(string host, string username, string password) {
ConfigureConnetionFactory(host, username, password);
}
private void ConfigureConnetionFactory(string host, string username, string password) {
_ConnFactory.UserName = username;
_ConnFactory.Password = password;
_ConnFactory.HostName = host;
_ConnFactory.Port = AmqpTcpEndpoint.UseDefaultPort; //assumes default RabbitMQPort
}
public RawQueueConnection GetConnection() {
return GetConnection("/");
}
public RawQueueConnection GetConnection(string virtualHost) {
_ConnFactory.VirtualHost = virtualHost;
return new RawQueueConnection(_ConnFactory.CreateConnection());
}
}
public class RawQueueConnection : IDisposable {
private readonly IConnection _Conn;
public RawQueueConnection(IConnection conn) {
_Conn = conn;
}
public int GetQueueDepth(string queueName) {
var model = _Conn.CreateModel();
var queue = model.QueueDeclarePassive(queueName);
var count = queue.MessageCount;
model.Close();
return Convert.ToInt32(count);
}
public void Dispose() {
_Conn.Close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment