Пример #1
0
        private static void PullMQ()
        {
            Console.WriteLine("Begin pull all msg...");

            var MQQueueName = ConfigurationManager.AppSettings["MQQueueName"];
            var factory     = MQUtils.MQFactory;

            using (var conn = factory.CreateConnection())
            {
                using (RabbitMQ.Client.IModel channel = conn.CreateModel())
                {
                    IDictionary <string, object> queueArgs = new Dictionary <string, object>
                    {
                        { "x-ha-policy", "all" }
                    };
                    channel.QueueDeclare(queue: MQQueueName,
                                         durable: true,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: queueArgs);
                    channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
                    while (!MQUtils.QueueEmpty())
                    {
                        if (!MQUtils.Pull(channel, ExcuteMessage))
                        {
                            Console.WriteLine("{0} - Queue is empty!", Thread.CurrentThread.ManagedThreadId);
                            Thread.Sleep(1000);
                        }
                    }
                    Console.WriteLine("pull all msg done!");
                }
            }
        }
Пример #2
0
        private SimpleConsumer Consume(string queue, Connection.IConnection connection)
        {
            R.IModel       channel  = null;
            SimpleConsumer consumer = null;

            try
            {
                channel = connection.CreateChannel(IsChannelTransacted);
                channel.BasicQos(0, (ushort)PrefetchCount, false);  // TODO: Verify this
                consumer = new SimpleConsumer(this, connection, channel, queue);
                channel.QueueDeclarePassive(queue);
                consumer.ConsumerTag = channel.BasicConsume(
                    queue,
                    AcknowledgeMode.IsAutoAck(),
                    ConsumerTagStrategy != null ? ConsumerTagStrategy.CreateConsumerTag(queue) : string.Empty,
                    NoLocal,
                    Exclusive,
                    ConsumerArguments,
                    consumer);
            }

            // catch (AmqpApplicationContextClosedException e)
            // {
            //    throw new AmqpConnectException(e);
            // }
            catch (Exception e)
            {
                RabbitUtils.CloseChannel(channel, _logger);
                RabbitUtils.CloseConnection(connection, _logger);

                consumer = HandleConsumeException(queue, consumer, e);
            }

            return(consumer);
        }
Пример #3
0
        private async Task Run(string[] args)
        {
            var host  = ConfigurationManager.AppSettings["rabbit.host"];
            var user  = ConfigurationManager.AppSettings["rabbit.admuser"];
            var pwd   = ConfigurationManager.AppSettings["rabbit.admpwd"];
            var vhost = ConfigurationManager.AppSettings["rabbit.vhost"];

            LogAdapter.LogDebugFn              = (s, s1, arg3) => { };
            LogAdapter.ExtendedLogEnabled      = false;
            LogAdapter.ProtocolLevelLogEnabled = false;

            _hdrHistogram = new LongHistogram(1, 1000 * 10, 5);

            int  howManyQueues        = 10;
            bool exclusiveConnections = ConfigurationManager.AppSettings["exclusiveConnections"] == "true";
            bool useOfficialClient    = ConfigurationManager.AppSettings["useOfficialClient"] == "true";

            if (useOfficialClient)
            {
                RabbitMQ.Client.IConnection conn    = null;
                RabbitMQ.Client.IModel      channel = null;

                for (int i = 0; i < howManyQueues; i++)
                {
                    var connFac = new RabbitMQ.Client.ConnectionFactory()
                    {
                        HostName = host, UserName = user, Password = pwd, VirtualHost = vhost, AutomaticRecoveryEnabled = false
                    };

                    if (exclusiveConnections || conn == null)
                    {
                        conn    = connFac.CreateConnection();
                        channel = conn.CreateModel();
                        channel.BasicQos(0, 300, false);
                    }

                    var q = "q." + i;
                    channel.QueueDeclareNoWait(q, durable: true, exclusive: false, autoDelete: false, arguments: null);

                    channel.BasicConsume(q, false, "con_" + q, arguments: null, consumer: new Consumer(channel));
                }
            }
            else
            {
                RabbitMqNext.IConnection conn    = null;
                RabbitMqNext.IChannel    channel = null;

                for (int i = 0; i < howManyQueues; i++)
                {
                    if (exclusiveConnections || conn == null)
                    {
                        conn = await RabbitMqNext.ConnectionFactory.Connect(host, vhost, user, pwd, recoverySettings : null, connectionName : "mod_perf_server");

                        channel = await conn.CreateChannel();

                        await channel.BasicQos(0, 300, false);
                    }

                    var q = "q." + i;

                    await channel.QueueDeclare(q, passive : false, durable : true, exclusive : false, autoDelete : false, arguments : null,
                                               waitConfirmation : false);

                    // TODO: test with parallel buffer copy + serialized too
                    await channel.BasicConsume(ConsumeMode.SerializedWithBufferCopy, BuildConsumerFn(channel), q, "consumer_" + q,
                                               false, true, arguments : null, waitConfirmation : false);
                }
            }

            Console.WriteLine("Consuming..");

            Console.CancelKeyPress += (sender, eventArgs) =>
            {
                Console.WriteLine("Done\r\n");
                Console.WriteLine("howManyQueues {0} exclusive Connections: {1} official client: {2} count {3}", howManyQueues, exclusiveConnections, useOfficialClient, _hdrHistogram.TotalCount);
                Console.WriteLine("\r\n");

                _hdrHistogram.OutputPercentileDistribution(Console.Out);

                Console.ReadKey();
            };

            await Task.Delay(1);

            Thread.CurrentThread.Join();
        }