public void RepublishError(Error error, QueueParameters parameters) { using (var connection = HosepipeConnection.FromParamters(parameters)) using (var model = connection.CreateModel()) { try { if (error.Exchange != string.Empty) { model.ExchangeDeclarePassive(error.Exchange); } var properties = model.CreateBasicProperties(); error.BasicProperties.CopyTo(properties); var body = Encoding.UTF8.GetBytes(error.Message); model.BasicPublish(error.Exchange, error.RoutingKey, properties, body); } catch (OperationInterruptedException) { Console.WriteLine("The exchange, '{0}', described in the error message does not exist on '{1}', '{2}'", error.Exchange, parameters.HostName, parameters.VHost); } } }
public void PublishMessagesToQueue(IEnumerable <string> messages, QueueParameters parameters) { using (var connection = HosepipeConnection.FromParamters(parameters)) using (var channel = connection.CreateModel()) { try { channel.QueueDeclarePassive(parameters.QueueName); } catch (OperationInterruptedException exception) { Console.WriteLine(exception.Message); return; } foreach (var message in messages) { var body = Encoding.UTF8.GetBytes(message); var properties = channel.CreateBasicProperties(); // take advantage of the fact that every AMQP queue binds to the default ("") // queue using its name as the routing key channel.BasicPublish("", parameters.QueueName, properties, body); } } }
public IEnumerable <string> GetMessagesFromQueue(QueueParameters parameters) { using (var connection = HosepipeConnection.FromParamters(parameters)) using (var channel = connection.CreateModel()) { try { channel.QueueDeclarePassive(parameters.QueueName); } catch (OperationInterruptedException exception) { Console.WriteLine(exception.Message); yield break; } var count = 0; while (++count < parameters.NumberOfMessagesToRetrieve) { var basicGetResult = channel.BasicGet(parameters.QueueName, noAck: parameters.Purge); if (basicGetResult == null) { break; // no more messages on the queue } yield return(Encoding.UTF8.GetString(basicGetResult.Body)); } } }
public void PublishMessagesToQueue(IEnumerable <HosepipeMessage> messages, QueueParameters parameters) { using (var connection = HosepipeConnection.FromParamters(parameters)) using (var channel = connection.CreateModel()) { foreach (var message in messages) { var body = Encoding.UTF8.GetBytes(message.Body); var properties = new BasicProperties(); message.Properties.CopyTo(properties); channel.BasicPublish(message.Info.Exchange, message.Info.RoutingKey, properties, body); } } }
public IEnumerable <HosepipeMessage> GetMessagesFromQueue(QueueParameters parameters) { using (var connection = HosepipeConnection.FromParamters(parameters)) using (var channel = connection.CreateModel()) { try { channel.QueueDeclarePassive(parameters.QueueName); } catch (OperationInterruptedException exception) { Console.WriteLine(exception.Message); yield break; } var count = 0; while (count++ < parameters.NumberOfMessagesToRetrieve) { var basicGetResult = channel.BasicGet(parameters.QueueName, noAck: parameters.Purge); if (basicGetResult == null) { break; // no more messages on the queue } var properties = new MessageProperties(basicGetResult.BasicProperties); var info = new MessageReceivedInfo( "hosepipe", basicGetResult.DeliveryTag, basicGetResult.Redelivered, basicGetResult.Exchange, basicGetResult.RoutingKey, parameters.QueueName); yield return(new HosepipeMessage(errorMessageSerializer.Serialize(basicGetResult.Body), properties, info)); } } }