示例#1
0
 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);
 }
 public async Task RunAction(CancellationToken cancellationToken)
 {
     var publisher = new ConfirmingPublisher(_model);
     var first = "Start! ";
     while (_model.IsOpen)
     {
         cancellationToken.ThrowIfCancellationRequested();
         var messageBody = first + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString();
         publisher.Publish(string.Empty, new[] { new RabbitMessage { Body = Encoding.UTF8.GetBytes(messageBody), RoutingKey = "testqueue2" } });
         first = string.Empty;
         await Task.Delay(TimeSpan.FromHours(1), cancellationToken);
     }
 }
示例#3
0
        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 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));
        }