Exemplo n.º 1
0
        public string PublishMessage(string message)
        {
            try
            {
                List <QueueDeclareOk> results = new List <QueueDeclareOk>();
                var  queueName = "queue0";
                bool res       = false;
                var  command   = new Command();

                lock (_lock)
                {
                    if (model == null)
                    {
                        model = mqService.GetChannel();
                    }

                    var result = model.QueueDeclare(queueName, true, false, false, null);
                    if (result.MessageCount >= 30)
                    {
                        for (int i = 1; i < 13; i++)
                        {
                            result = model.QueueDeclare("queue" + i);

                            if (result.MessageCount < 30)
                            {
                                if (result.MessageCount == 0)
                                {
                                    command.Add(WhatToDo.Add, result.QueueName);
                                }
                                else
                                {
                                    command.Add(WhatToDo.Nothing, result.QueueName);
                                }
                                queueName = result.QueueName;
                            }
                        }
                    }

                    model.QueueBind(result.QueueName, "exchange", result.QueueName, null);

                    mqService.Send(model, message, result.QueueName, "exchange");
                    return(res ? result.QueueName : string.Empty);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("При отправке сообщения произошла ошибка.\n Message: " + ex.Message);
            }
        }
Exemplo n.º 2
0
        public void MqTest_SendMessage()
        {
            var mqInfo = GenerateQueueInfo();

            try
            {
                Assert.DoesNotThrow(() =>
                {
                    mqService.CreateRabbitQueue(mqInfo, false);
                    mqService.Send("helloRabbit", mqInfo);
                });
            }
            finally
            {
                mqService.DeleteQueue(mqInfo.QueueName);
            }
        }