Пример #1
0
        static async Task PublishMessage()
        {
            //var routingKey = "wonga";
            var initial = new ManagementClient("http://localhost", "rabbitmq", "password");

            // first create a new virtual host
            var vhost = await initial.CreateVirtualHostAsync("wonga_host").ConfigureAwait(false);

            // next create a user for that virutal host
            var user = await initial.CreateUserAsync(new UserInfo("mazizi", "wonga").AddTag("administrator"));

            // give the new user all permissions on the virtual host
            await initial.CreatePermissionAsync(new PermissionInfo(user, vhost));

            // now log in again as the new user
            var management = new ManagementClient("http://localhost", user.Name, "wonga");

            // test that everything's OK
            await management.IsAliveAsync(vhost);

            //// create an exchange
            //var exchange = await management.CreateExchangeAsync(new ExchangeInfo("wonga_exchagne", "direct"), vhost);

            //// create a queue
            //var queue = await management.CreateQueueAsync(new QueueInfo("wonga_queue"), vhost);

            //// bind the exchange to the queue
            //await management.CreateBinding(exchange, queue, new BindingInfo(routingKey));

            //var input = "";
            //while ((input = Console.ReadLine()) != "Quit")
            //{
            //    // publish a test message
            //    //using (var bus = RabbitHutch.CreateBus("host=localhost"))
            //    //{
            //    //    bus.Send("wonga_queue", new TextMessage { Text = input });
            //    //}
            //    await management.PublishAsync(exchange, new PublishInfo(routingKey,
            //        JsonConvert.SerializeObject(new TextMessage { Text = input })));
            //}

            //using (var bus = RabbitHutch.CreateBus(string.Format("host={0};virtualhost={1};username={2};password={3}",
            //    "localhost", "wonga_host", "mazizi", "wonga")))
            //{
            //    var input = "";
            //    Console.WriteLine("Enter a message. 'Quit' to quit.");
            //    while ((input = Console.ReadLine()) != "Quit")
            //    {
            //        bus.Publish(new TextMessage
            //        {
            //            Text = input
            //        });
            //    }
            //}
        }
        public async Task Should_be_able_to_provision_a_virtual_host()
        {
            var initial = new ManagementClient(rabbitMqUrl, Configuration.RabbitMqUser,
                                               Configuration.RabbitMqPassword, Configuration.RabbitMqManagementPort);

            // first create a new virtual host
            var vhost = await initial.CreateVirtualHostAsync("my_virtual_host").ConfigureAwait(false);

            // next create a user for that virutal host
            var user = await initial.CreateUserAsync(new UserInfo("mike", "topSecret").AddTag("administrator")).ConfigureAwait(false);

            // give the new user all permissions on the virtual host
            await initial.CreatePermissionAsync(new PermissionInfo(user, vhost)).ConfigureAwait(false);

            // now log in again as the new user
            var management = new ManagementClient(rabbitMqUrl, user.Name, "topSecret",
                                                  Configuration.RabbitMqManagementPort);

            // test that everything's OK
            await management.IsAliveAsync(vhost).ConfigureAwait(false);

            // create an exchange
            var exchange = await management.CreateExchangeAsync(new ExchangeInfo("my_exchagne", "direct"), vhost).ConfigureAwait(false);

            // create a queue
            var queue = await management.CreateQueueAsync(new QueueInfo("my_queue"), vhost).ConfigureAwait(false);

            // bind the exchange to the queue
            await management.CreateBinding(exchange, queue, new BindingInfo("my_routing_key")).ConfigureAwait(false);

            // publish a test message
            await management.PublishAsync(exchange, new PublishInfo("my_routing_key", "Hello World!")).ConfigureAwait(false);

            // get any messages on the queue
            var messages = await management.GetMessagesFromQueueAsync(queue, new GetMessagesCriteria(1, false)).ConfigureAwait(false);

            foreach (var message in messages)
            {
                Console.Out.WriteLine("message.payload = {0}", message.Payload);
            }
        }