public static void ConfigureHost(this RabbitMqTransportFactoryConfigurator configurator, Uri hostAddress,
                                         Action <ConnectionFactoryConfigurator> configureHost)
        {
            var hostConfigurator = new ConnectionFactoryConfiguratorImpl(RabbitMqEndpointAddress.Parse(hostAddress));

            configureHost(hostConfigurator);

            configurator.AddConfigurator(hostConfigurator);
        }
 private void DeleteQueue(string addressUrl)
 {
     using (var channel = _connection.CreateModel())
     {
         var name = RabbitMqEndpointAddress.Parse(addressUrl).Name;
         channel.QueueDelete(name);
         channel.ExchangeDelete(name);
         channel.Close(200, "ok");
     }
 }
        private void BindQueue(string addressUrl)
        {
            using (var channel = _connection.CreateModel())
            {
                var name = RabbitMqEndpointAddress.Parse(addressUrl).Name;

                var queue = channel.QueueDeclare(name, true, false, false, null);
                //var args = new Dictionary<string, object> { { "x-expires", 15000 } };
                //var queue = channel.QueueDeclare(name, true, false, false, args);

                channel.ExchangeDeclare(name, ExchangeType.Fanout, true);
                //channel.ExchangeDeclare(name, ExchangeType.Fanout, true, false, args);
                channel.QueueBind(queue, name, "");

                channel.Close(200, "ok");
            }
        }
        void CleanUpQueuesAndExchanges()
        {
            RabbitMqEndpointAddress address           = RabbitMqEndpointAddress.Parse(_serviceBusUri);
            ConnectionFactory       connectionFactory = address.ConnectionFactory;

            if (string.IsNullOrWhiteSpace(connectionFactory.UserName))
            {
                connectionFactory.UserName = "******";
            }
            if (string.IsNullOrWhiteSpace(connectionFactory.Password))
            {
                connectionFactory.Password = "******";
            }

            using (IConnection connection = connectionFactory.CreateConnection())
            {
                using (IModel model = connection.CreateModel())
                {
                    model.ExchangeDelete(address.Name);
                    model.QueueDelete(address.Name);

                    for (int i = 0; i < 10000; i++)
                    {
                        string name = string.Format("{0}_client_{1}", address.Name, i);
                        try
                        {
                            model.QueueDeclarePassive(name);
                        }
                        catch (OperationInterruptedException)
                        {
                            break;
                        }

                        model.ExchangeDelete(name);
                        model.QueueDelete(name);
                    }
                }
            }
        }
示例#5
0
 public void WhenParsed()
 {
     _addr = RabbitMqEndpointAddress.Parse(Uri);
 }
示例#6
0
 public void TheQueue()
 {
     _addr = RabbitMqEndpointAddress.Parse(Uri);
 }
        Task StartStressGenerator(int instance, Task start)
        {
            var ready = new TaskCompletionSource <bool>();

            var composer = new TaskComposer <bool>(_cancel.Token, false);

            var    endpointAddress = _serviceBus.Endpoint.Address as IRabbitMqEndpointAddress;
            string queueName       = string.Format("{0}_client_{1}", endpointAddress.Name, instance);
            Uri    uri             = RabbitMqEndpointAddress.Parse(_clientUri).ForQueue(queueName).Uri;

            var uriBuilder = new UriBuilder(uri);

            uriBuilder.Query = _clientUri.Query.Trim('?');

            Uri address = uriBuilder.Uri;

            composer.Execute(() => { Interlocked.Increment(ref _instanceCount); });

            IServiceBus bus = null;

            composer.Execute(() =>
            {
                _log.InfoFormat("Creating {0}", address);

                bus = ServiceBusFactory.New(x =>
                {
                    x.UseRabbitMq(r =>
                    {
                        r.ConfigureHost(address, h =>
                        {
                            h.SetUsername(_username);
                            h.SetPassword(_password);
                            h.SetRequestedHeartbeat(_heartbeat);
                        });
                    });

                    x.ReceiveFrom(address);
                });
            }, false);

            Stopwatch clientTimer = null;

            composer.Execute(() =>
            {
                ready.TrySetResult(true);
                return(start);
            });

            composer.Execute(() => clientTimer = Stopwatch.StartNew());

            for (int requestClient = 0; requestClient < _requestsPerInstance; requestClient++)
            {
                int clientIndex = requestClient;

                composer.Execute(() =>
                {
                    Task task = composer.Compose(x =>
                    {
                        for (int i = 0; i < _iterations; i++)
                        {
                            int iteration = i;
                            x.Execute(() =>
                            {
                                string messageContent = _mixed && iteration % 2 == 0
                                    ? new string('*', 128)
                                    : _messageContent;
                                var requestMessage = new StressfulRequestMessage(messageContent);

                                ITaskRequest <StressfulRequest> taskRequest =
                                    bus.PublishRequestAsync <StressfulRequest>(requestMessage, r =>
                                {
                                    r.Handle <StressfulResponse>(response =>
                                    {
                                        Interlocked.Increment(ref _responseCount);

                                        TimeSpan timeSpan = response.Timestamp - requestMessage.Timestamp;
                                        Interlocked.Add(ref _responseTime, (long)timeSpan.TotalMilliseconds);
                                        _timings[instance][clientIndex * _iterations + iteration] = (int)timeSpan.TotalMilliseconds;

                                        if (response.RequestId != requestMessage.RequestId)
                                        {
                                            Interlocked.Increment(ref _mismatchedResponseCount);
                                        }
                                    });
                                });

                                Interlocked.Increment(ref _requestCount);

                                return(taskRequest.Task);
                            });
                        }
                    });

                    return(task);
                });
            }

            composer.Execute(() => clientTimer.Stop());

            composer.Execute(() => bus.Dispose(), false);

            composer.Compensate(compensation => { return(compensation.Handled()); });

            composer.Finally(status =>
            {
                Interlocked.Add(ref _totalTime, clientTimer.ElapsedMilliseconds);
                int count = Interlocked.Decrement(ref _instanceCount);
                if (count == 0)
                {
                    Task.Factory.StartNew(() => _hostControl.Stop());
                }
            }, false);

            _clientTasks.Add(composer.Finish());

            return(ready.Task);
        }
示例#8
0
        Task StartStressGenerator(int instance, Task start)
        {
            var ready = new TaskCompletionSource <bool>();

            var composer = new TaskComposer <bool>(_cancel.Token, false);

            string queueName = string.Format("{0}_client_{1}", _serviceBusAddress.Name, instance);
            Uri    uri       = RabbitMqEndpointAddress.Parse(_clientUri).ForQueue(queueName).Uri;

            var uriBuilder = new UriBuilder(uri);

            uriBuilder.Query = _clientUri.Query.Trim('?');

            Uri address = uriBuilder.Uri;

            composer.Execute(() => { Interlocked.Increment(ref _instanceCount); });

            composer.Execute(async() => { await Task.Yield(); });

            IServiceBus bus = null;

            composer.Execute(() =>
            {
                _log.InfoFormat("Creating {0}", address);

                bus = ServiceBusFactory.New(x =>
                {
                    x.UseRabbitMq(r =>
                    {
                        r.ConfigureHost(address, h =>
                        {
                            h.SetUsername(_username);
                            h.SetPassword(_password);
                            h.SetRequestedHeartbeat(_heartbeat);
                        });
                    });

                    x.ReceiveFrom(address);
                });
            }, false);

            Stopwatch clientTimer = null;

            composer.Execute(async() =>
            {
                ready.TrySetResult(true);
                await start;
            });

            composer.Execute(() => clientTimer = Stopwatch.StartNew());

            composer.Execute(() =>
            {
                Task task = composer.Compose(x =>
                {
                    for (int i = 0; i < _iterations; i++)
                    {
                        int iteration = i;
                        x.Execute(() =>
                        {
                            string messageContent = _mixed && iteration % 2 == 0
                                ? new string('*', 128)
                                : _messageContent;
                            var requestMessage = new StressfulRequestMessage(messageContent);

                            Stopwatch sendTimer = Stopwatch.StartNew();

                            bus.Publish <StressfulRequest>(requestMessage);

                            sendTimer.Stop();

                            Interlocked.Add(ref _sendTime, sendTimer.ElapsedTicks);
                            _timings[instance][iteration] = (int)sendTimer.ElapsedTicks;

                            Interlocked.Increment(ref _sendCount);
                        });
                    }
                });

                return(task);
            });

            composer.Execute(() => clientTimer.Stop());

            composer.Execute(() => bus.Dispose(), false);

            composer.Compensate(compensation => { return(compensation.Handled()); });

            composer.Finally(status =>
            {
                Interlocked.Add(ref _totalTime, clientTimer.ElapsedMilliseconds);
                int count = Interlocked.Decrement(ref _instanceCount);
                if (count == 0)
                {
                    Task.Factory.StartNew(() => _hostControl.Stop());
                }
            }, false);

            _clientTasks.Add(composer.Finish());

            return(ready.Task);
        }