Created
January 12, 2015 22:11
-
-
Save pdoran/2651cfce26fc788750e8 to your computer and use it in GitHub Desktop.
Count messages in a RabbtiMQ Queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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