示例#1
0
 public void CreateExchange()
 {
     WithChannel.Do(channel => channel.ExchangeDeclare(
                        exchange: subscription,
                        type: ExchangeType.Direct,
                        durable: true));
 }
示例#2
0
        public void Spike()
        {
            WithChannel.Do(model =>
            {
                var queue = model.QueueDeclare("confirms.test", false, false, false, null);
                model.ConfirmSelect();

                model.BasicAcks += (model1, args) =>
                                   Console.Out.WriteLine("Delivered DeliveryTag: '{0}', Multiple: {1}", args.DeliveryTag, args.Multiple);

                model.BasicNacks += (model1, args) =>
                                    Console.Out.WriteLine("Failed DeliverTag: '{0}', Multiple: {1}", args.DeliveryTag, args.Multiple);

                Publish(model, queue, "Hello 1");
                Publish(model, queue, "Hello 2");

                Console.Out.WriteLine("Waiting for result");
                Thread.Sleep(2000);

                BasicGetResult result;
                do
                {
                    Thread.Sleep(100);
                    result = model.BasicGet(queue, false);
                } while (result == null);

                Console.Out.WriteLine("result.Body = {0}", Encoding.UTF8.GetString(result.Body));

                model.BasicAck(result.DeliveryTag, false);
            });
        }
示例#3
0
 public void SetUp()
 {
     WithChannel.Do(channel =>
     {
         channel.QueueDeclare(high, true, false, false, null);
         channel.QueueDeclare(low, true, false, false, null);
     });
 }
示例#4
0
 public void CreateQueue()
 {
     WithChannel.Do(channel =>
     {
         var returnedName = channel.QueueDeclare(queueName, false, false, false, null);
         Console.WriteLine("Declared queue: {0}", returnedName);
     });
 }
示例#5
0
 public void Publish(string queue)
 {
     WithChannel.Do(channel =>
     {
         for (var i = 0; i < publishCount; i++)
         {
             channel.BasicPublish("", queue, channel.CreateBasicProperties(), GetMessage());
         }
     });
 }
示例#6
0
 public void InitializeQueue()
 {
     WithChannel.Do(channel =>
                    channel.QueueDeclare(
                        ackNackQueue,
                        durable: true,
                        exclusive: false,
                        autoDelete: false,
                        arguments: null));
 }
示例#7
0
 public void PublishSequence()
 {
     WithChannel.Do(channel =>
     {
         for (int i = 0; i < 10; i++)
         {
             var properties = channel.CreateBasicProperties();
             var body       = Encoding.UTF8.GetBytes(i.ToString());
             channel.BasicPublish("", ackNackQueue, properties, body);
         }
     });
 }
示例#8
0
        public void GetMessagesFor(string subscriber)
        {
            WithChannel.Do(channel =>
            {
                var consumer = new CallbackConsumer();
                channel.BasicConsume(subscriber, true, consumer);

                // give the consumer some time to get messages
                Thread.Sleep(1000);
            });
            Console.WriteLine("Stopped consuming messages");
        }
示例#9
0
        public void PublishMessage()
        {
            WithChannel.Do(channel =>
            {
                var defaultProperties = channel.CreateBasicProperties();
                var messageText       = "Hello World " + Guid.NewGuid().ToString().Substring(0, 5);
                var message           = Encoding.UTF8.GetBytes(messageText);

                channel.BasicPublish(subscription, subscription, defaultProperties, message);
                Console.WriteLine("Published Message '{0}'", messageText);
            });
        }
示例#10
0
        public void SubscribeWithSubscriber()
        {
            WithChannel.Do(channel =>
            {
                var subscription = new Subscription(channel, ackNackQueue);
                foreach (BasicDeliverEventArgs deliverEventArgs in subscription)
                {
                    var message = Encoding.UTF8.GetString(deliverEventArgs.Body);
                    Console.Out.WriteLine("message = {0}", message);

                    subscription.Ack(deliverEventArgs);
                }
            });
        }
示例#11
0
        public void Subscribe(string subscriberName)
        {
            WithChannel.Do(channel =>
            {
                var queue = channel.QueueDeclare(subscriberName,
                                                 durable: true,
                                                 exclusive: false,
                                                 autoDelete: false,
                                                 arguments: null);

                channel.QueueBind(queue, subscription, subscription);
                Console.WriteLine("{0}: {1}", subscriberName, queue);
            });
        }
示例#12
0
        public void SubscribeAck()
        {
            const int  prefetchCount             = 1;
            const bool noAck                     = false;
            const int  numberOfMessagesToConsume = 1;

            WithChannel.Do(channel =>
            {
                channel.BasicQos(0, prefetchCount, false);

                var consumer = new LocalQueueingBasicConsumer(channel);
                channel.BasicConsume(ackNackQueue, noAck, consumer);

                var running = true;
                var thread  = new Thread(_ =>
                {
                    var count = 0;
                    while (running && count++ < numberOfMessagesToConsume)
                    {
                        try
                        {
                            var basicDeliverEventArgs = (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                            if (basicDeliverEventArgs != null)
                            {
                                var message = Encoding.UTF8.GetString(basicDeliverEventArgs.Body);
                                Console.Out.WriteLine("message = {0}", message);

                                Console.WriteLine("Redelivered: {0}", basicDeliverEventArgs.Redelivered);

                                consumer.Model.BasicAck(basicDeliverEventArgs.DeliveryTag, false);
                                // consumer.Model.BasicNack(basicDeliverEventArgs.DeliveryTag, false, requeue:true);
                            }
                        }
                        catch (EndOfStreamException)
                        {
                            break;
                        }
                    }
                });
                thread.Start();

                Thread.Sleep(1000);
                running = false;
                channel.Close();
                consumer.Queue.Close();
            });
        }
示例#13
0
        private static void Publish(string routingKey)
        {
            WithChannel.Do(channel =>
            {
                channel.ExchangeDeclare(
                    exchange: exchange,
                    type: ExchangeType.Topic,
                    durable: true);

                var defaultProperties = channel.CreateBasicProperties();
                var messageText       = "Hello World " + Guid.NewGuid().ToString().Substring(0, 5);
                var message           = Encoding.UTF8.GetBytes(messageText);

                channel.BasicPublish(exchange, routingKey, defaultProperties, message);
                Console.WriteLine("Published Message '{0}'", messageText);
            });
        }
示例#14
0
        public void BasicGet()
        {
            WithChannel.Do(channel =>
            {
                var result = channel.BasicGet(queueName, true);

                if (result == null)
                {
                    Console.WriteLine("Queue is empty");
                }
                else
                {
                    var messageText = Encoding.UTF8.GetString(result.Body);
                    Console.WriteLine("Got message: {0}", messageText);
                }
            });
        }
示例#15
0
        private static void TopicSubscribe(string routingKey, string queueName)
        {
            WithChannel.Do(channel =>
            {
                var queue = channel.QueueDeclare(queueName,
                                                 durable: true,
                                                 exclusive: false,
                                                 autoDelete: false,
                                                 arguments: null);

                channel.QueueBind(queue, exchange, routingKey);
                Console.WriteLine("{0}: {1}", queueName, queue);

                var consumer = new CallbackConsumer();
                channel.BasicConsume(queue, true, consumer);

                // give the consumer some time to get messages
                Thread.Sleep(1000);

                Console.WriteLine("Stopped consuming");
            });
        }