/// <summary> /// Tries to remove the specified queue from the bus server. /// </summary> /// <param name="queueIdentifier"></param> public void DeleteQueue(string queueIdentifier) { using (var channel = PooledConnection.CreateModel()) { RabbitMQHelper.UseConnectionWithRetries(c => c.QueueDelete(queueIdentifier), channel); } }
/// <summary> /// Writes the provided message to the queue /// </summary> /// <param name="message"></param> public void Write(Message <TMessageData> message) { var messages = message.DecomposeBatchIntoIndividualMessages(); foreach (var messageBytes in messages.Select(m => _jsonSerializer.Make(m))) { RabbitMQHelper.UseConnectionWithRetries(c => c.BasicPublish(string.Empty, _queueName, MessageWriteProperties, messageBytes), _channel); } }
/// <summary> /// Creates the specified queue on the RabbitMQ server if it does not exist. If binding information has been provided, this will attempt to bind the queue as directed /// </summary> /// <param name="queueInfo"></param> /// <remarks>If you try to create a queue that already exist but you specify different configuration values than what exists, /// this will throw</remarks> public void CreateQueue(QueueCreationInfo queueInfo) { using (var channel = PooledConnection.CreateModel()) { RabbitMQHelper.UseConnectionWithRetries(c => c.QueueDeclare(queueInfo.Identifier, queueInfo.Durable, false, false, null), channel); if (queueInfo.BindingInfo != null) { RabbitMQHelper.UseConnectionWithRetries(c => c.QueueBind(queueInfo.Identifier, queueInfo.BindingInfo.ExchangeIdentifier, queueInfo.BindingInfo.RoutingKey), channel); } } }
/// <summary> /// Tries to create the exchange with the parameters specified on the bus server /// </summary> /// <param name="name"></param> /// <param name="type"></param> /// <param name="alternateExchange"></param> /// <remarks>If you try to create a queue that already exist but you specify different configuration values than what exists, /// this will throw</remarks> public void CreateExchange(string name, string type, string alternateExchange = null) { using (var channel = PooledConnection.CreateModel()) { Dictionary <string, object> args = null; if (!string.IsNullOrEmpty(alternateExchange)) { args = new Dictionary <string, object> { { "alternate-exchange", alternateExchange } }; } RabbitMQHelper.UseConnectionWithRetries(c => c.ExchangeDeclare(name, type, true, false, args), channel); } }
/// <summary> /// Returns information about the connected queue /// </summary> /// <returns></returns> public QueueMetadata GetQueueInfo() { var count = RabbitMQHelper.UseConnectionWithRetries(c => c.MessageCount(_queueName), _channel); if (count > int.MaxValue) { // This scenario seems unlikely enough that I don't feel like coding around it. Are we ever going to have queues // with more than two billion messages at a time? We can cross that bridge when we come to it throw new InvalidOperationException("Count of messages in the queue is greater than a 32bit signed integer."); } var result = new QueueMetadata((int)count, _queueName); return(result); }
/// <summary> /// Returns the next message in the queue. /// </summary> /// <returns>Null if no message exists in the queue.</returns> public Message <TMessageData> ReadNext() { var raw = RabbitMQHelper.UseConnectionWithRetries(c => c.BasicGet(_queueName, false), _channel); if (raw == null) { return(null); } Message <TMessageData> message; try { message = _jsonSerializer.Get <TMessageData>(raw.Body); } catch (Exception ex) { RabbitMQHelper.UseConnectionWithRetries(c => c.BasicAck(raw.DeliveryTag, false), _channel); var msfe = new MessageSerializationFailureException(raw.Body, ex); throw msfe; } message.ConversationId = raw.DeliveryTag; return(message); }
/// <summary> /// Returns the specified message to the head of the queue /// </summary> /// <param name="message"></param> public void ReturnMessageToQueue(Message <TMessageData> message) { RabbitMQHelper.UseConnectionWithRetries(c => c.BasicReject(message.ConversationId, true), _channel); }
/// <summary> /// Confirms the read of the specified message with the queue /// </summary> /// <param name="message"></param> /// <remarks>For RabbitMQ, this is required in order for the message to be cleared from the queue</remarks> public void ConfirmMessageReceipt(Message <TMessageData> message) { RabbitMQHelper.UseConnectionWithRetries(c => c.BasicAck(message.ConversationId, false), _channel); }