Esempio n. 1
0
        static void Main(string[] args)
        {
            var serverAddress = "192.168.0.196";
              var queueName = "message_data_cs";

              using (var conn = new ConnectionFactory().CreateConnection(serverAddress))
              {
            using (var ch = conn.CreateModel())
            {
              ch.QueueDeclare(queueName, true);
              var properties = ch.CreateBasicProperties();
              properties.SetPersistent(true);

              for (int i = 0; i < 10; i++)
              {
            var message = new Message()
            {
              From = "C# Sample",
              Body = String.Format("Message No. {0}", i)
            };

            Console.WriteLine("Sending: {0}", i);
            ch.BasicPublish("", queueName, properties, Message.Serialize(message));
              }
            }
              }
        }
Esempio n. 2
0
        public void WillCreateInputAndErrorQueue()
        {
            var testRabbitQueues = "test.rabbit.queues";
            CreateBus(testRabbitQueues, new HandlerActivatorForTesting()).Start(1);


            using (var connection = new ConnectionFactory {Uri = ConnectionString}.CreateConnection())
            using (var model = connection.CreateModel())
            {
                Assert.DoesNotThrow(() => model.BasicGet(testRabbitQueues, true));
                Assert.DoesNotThrow(() => model.BasicGet(testRabbitQueues + ".error", true));
            }
        }
Esempio n. 3
0
 public static void DeleteQueue(string queueName)
 {
     using (var connection = new ConnectionFactory {Uri = ConnectionString}.CreateConnection())
     using (var model = connection.CreateModel())
     {
         // just ignore if it fails...
         try
         {
             model.QueueDelete(queueName);
         }
         catch
         {
         }
     }
 }
Esempio n. 4
0
        static void Main(string[] args)
        {
            var serverAddress = "192.168.0.196";
              var queueName = "string_data";

              using (var conn = new ConnectionFactory().CreateConnection(serverAddress))
              {
            using (var ch = conn.CreateModel())
            {
              var messages = Enumerable.Range(1, 10).Select(x => String.Format("Hello from .NET! Message No. {0}", x));

              foreach (var message in messages)
              {
            Console.WriteLine("Sending: {0}", message);
            ch.BasicPublish("", queueName, null, Encoding.ASCII.GetBytes(message));
              }
            }
              }
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            var serverAddress = @"192.168.0.196";
              var queueName = "string_data";

              var conn = new ConnectionFactory().CreateConnection(serverAddress);
              using (var ch = conn.CreateModel())
              {
            conn.AutoClose = true;
            ch.QueueDeclare(queueName);

            BasicGetResult result = null;
            while ((result = ch.BasicGet(queueName, false)) != null)
            {
              ch.BasicAck(result.DeliveryTag, false);
              Console.WriteLine("Message: {0}", Encoding.ASCII.GetString(result.Body));
            }
              }
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            var serverAddress = @"192.168.0.196";
              var queueName = "message_data_cs";

              var conn = new ConnectionFactory().CreateConnection(serverAddress);
              using (var ch = conn.CreateModel())
              {
            conn.AutoClose = true;
            ch.QueueDeclare(queueName, true);

            BasicGetResult result = null;
            while ((result = ch.BasicGet(queueName, false)) != null)
            {
              ch.BasicAck(result.DeliveryTag, false);
              var message = Message.Deserialize(result.Body);
              Console.WriteLine(message.Text);
            }
              }

              Console.ReadKey();
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            var serverAddress = "192.168.0.196";
              var queueName = "string_data";

              using (var conn = new ConnectionFactory().CreateConnection(serverAddress))
              {
            using (var ch = conn.CreateModel())
            {
              ch.QueueDeclare(queueName, true);
              var properties = ch.CreateBasicProperties();
              properties.SetPersistent(true);

              for (int i = 0; i < 10; i++)
              {
            var message = String.Format("Hello from .NET! Message No. {0}", i);
            Console.WriteLine("Sending: {0}", message);
            ch.BasicPublish("", queueName, (i % 2 == 1) ? properties : null, Encoding.ASCII.GetBytes(message));
              }
            }
              }
        }
Esempio n. 8
0
        static void TestRabbitMq(int messageCount)
        {
            IConnection connection = new ConnectionFactory().CreateConnection();

            var serializer = new BinarySerializationStrategy();

            for (int i = 0; i < messageCount; i++)
            {
                IModel model = connection.CreateModel();
                model.ExchangeDeclare("rabbitMQ.test.exchange", ExchangeType.Direct, false, true, null);
                byte[] serializedMessage = serializer.Serialize(new TestMessage("test"));
                model.BasicPublish("rabbitMQ.test.exchange", string.Empty, null, serializedMessage);
                model.Close();
            }

            connection.Close();
        }
Esempio n. 9
0
        bool QueueExists(string queueName)
        {
            using (var connection = new ConnectionFactory { Uri = ConnectionString }.CreateConnection())
            {
                using (var model = connection.CreateModel())
                {
                    try
                    {
                        model.QueueDeclarePassive(queueName);

                        // if the call succeeds, then the queue exists
                        return true;
                    }
                    catch (OperationInterruptedException exception)
                    {
                        if (exception.Message.Contains("NOT_FOUND"))
                        {
                            return false;
                        }

                        throw;
                    }
                }
            }
        }
Esempio n. 10
0
        public void RabbitTransportDoesNotChokeOnMessagesContainingComplexHeaders()
        {
            // arrange
            const string recipientInputQueue = "test.roundtripping.receiver";
            const string someText = "whoohaa!";

            DeleteQueue(recipientInputQueue);

            // ensure recipient queue is created...
            using (var recipient = new RabbitMqMessageQueue(ConnectionString, recipientInputQueue))
            {
                // force creation of the queue
                recipient.ReceiveMessage(new NoTransaction());

                // act
                // send a message with a complex header
                using (var connection = new ConnectionFactory { Uri = ConnectionString }.CreateConnection())
                using (var model = connection.CreateModel())
                {
                    var props = model.CreateBasicProperties();
                    props.Headers = new Dictionary<string, object>
                        {
                            {
                                "someKey", new Hashtable
                                    {
                                        {"someContainedKey", "someContainedValue"},
                                        {"anotherContainedKey", "anotherContainedValue"},
                                    }
                            }
                        };

                    model.BasicPublish(recipient.ExchangeName,
                                       recipientInputQueue,
                                       props,
                                       Encoding.GetBytes(someText));
                }

                Thread.Sleep(2.Seconds());

                // assert
                var receivedTransportMessage = recipient.ReceiveMessage(new NoTransaction());
                receivedTransportMessage.ShouldNotBe(null);
                Encoding.GetString(receivedTransportMessage.Body).ShouldBe(someText);
            }

            // assert
        }