public IList<MessageView> SendToDestination(IEnumerable<MessageView> viewsToSend, string queueName) { var viewsToSendList = viewsToSend.ToList(); using (var publishChannel = new RabbitMqModel(_sourceConnection)) { try { publishChannel.QueueDeclarePassive(queueName); } catch (Exception ex) { throw new InvalidQueueException(string.Format("Could not find queue '{0}'",queueName),ex); } var publisher = new ConfirmingPublisher(publishChannel); foreach (var view in viewsToSendList) { var message = view.ProvideOriginalMessage(); message.RoutingKey = queueName; message.Properties.Timestamp = DateTime.Now.ToAmqpTimestamp(); publisher.Publish(string.Empty, new[] {message}); _messageSource.Acknowledge(message); } } return GetAvailableMessages(viewsToSendList.Count); }
public Controller(IConnectionFactory sourceConnectionFactory, string queueName) { _queueName = queueName; _sourceConnection = new RabbitMqConnection(sourceConnectionFactory); _sourceChannel = new RabbitMqModel(_sourceConnection); _sourceChannel.QueueDeclarePassive(queueName); _messageSource = new ConfirmingGetter(_sourceChannel) { QueueName = queueName }; _messagePublisher = new ConfirmingPublisher(_sourceChannel); }
private void Dispose(bool disposing) { if (disposing) { if (_sourceConnection != null) { _sourceConnection.Dispose(); _sourceConnection = null; } if (_sourceChannel != null) { _sourceChannel.Dispose(); _sourceChannel = null; } } }
public async Task SendAndRecieveMessage() { var queueName = "testqueueB"; var message = new RabbitMessage() { RoutingKey = queueName, Body = new byte[]{0,1,2,3,4}}; var messageHandler = new TestMessageHandler(); var connectionFactory = new ConnectionFactory { HostName = "mqBalancer", UserName = "******", Password = "******", RequestedHeartbeat = 5, }; using (var connection = new RabbitMqConnection(connectionFactory)) { using (var model = new RabbitMqModel(connection)) { model.QueueDeclare(queueName, false, true, false, new Dictionary<string, object>()); var consumer = model.HandleMessagesAsync(queueName); var publisher = new ConfirmingPublisher(model); await Task.Delay(TimeSpan.FromSeconds(1)); publisher.Publish(string.Empty, new[] { message }); await consumer.ConsumeMessages(messageHandler, CancellationToken.None); } } var result = messageHandler.LastMessage; Assert.That(result.Body, Is.EquivalentTo(message.Body)); }