Example #1
0
        public void Can_consume_messages_from_RabbitMQ_with_BasicConsume()
        {
            RabbitMqConfig.UsingChannel(channel => {
                var consumer = new RabbitMqBasicConsumer(channel);
                channel.BasicConsume(MqQueueNames <HelloRabbit> .Direct, false, consumer);
                string recvMsg = null;

                ExecUtils.ExecMultiThreading(1, () => PublishHelloRabbit(channel));

                while (true)
                {
                    try {
                        var e = consumer.Queue.Dequeue();
                        Console.WriteLine("Dequeued");
                        recvMsg = e.Body.FromUtf8Bytes();
                        // ... process the message
                        Console.WriteLine(recvMsg);

                        channel.BasicAck(e.DeliveryTag, false);
                        break;
                    } catch (OperationInterruptedException) {
                        // The consumer was removed, either through
                        // channel or connection closure, or through the
                        // action of IModel.BasicCancel().
                        Console.WriteLine("End of the road...");
                        break;
                    }
                }

                Assert.That(recvMsg, Is.Not.Null);
            });
        }
Example #2
0
        public void Can_interrupt_BasicConsumer_in_bgthread_by_closing_channel()
        {
            RabbitMqConfig.UsingChannel(channel => {
                string recvMsg = null;
                EndOfStreamException lastEx = null;

                var bgThread = new Thread(() => {
                    try {
                        var consumer = new RabbitMqBasicConsumer(channel);
                        channel.BasicConsume(MqQueueNames <HelloRabbit> .Direct, false, consumer);

                        while (true)
                        {
                            try {
                                var e   = consumer.Queue.Dequeue();
                                recvMsg = e.Body.FromUtf8Bytes();
                            } catch (EndOfStreamException ex) {
                                // The consumer was cancelled, the model closed, or the
                                // connection went away.
                                Console.WriteLine("EndOfStreamException in bgthread: {0}", ex.Message);
                                lastEx = ex;
                                return;
                            } catch (Exception ex) {
                                Assert.Fail("Unexpected exception in bgthread: " + ex.Message);
                            }
                        }
                    } catch (Exception ex) {
                        Console.WriteLine("Exception in bgthread: {0}: {1}", ex.GetType().Name, ex.Message);
                    }
                })
                {
                    Name         = "Closing Channel Test",
                    IsBackground = true
                };
                bgThread.Start();

                PublishHelloRabbit(channel);
                Thread.Sleep(100);

                // closing either throws EndOfStreamException in bgthread
                channel.Close();
                // connection.Close();

                Thread.Sleep(2000);

                Assert.That(recvMsg, Is.Not.Null);
                Assert.That(lastEx, Is.Not.Null);
            });
        }