Пример #1
0
        private RabbitMQHost(IRabbitMQHostConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var diagnosticService = configuration.DiagnosticService;
            var baseUri           = configuration.BaseUri.WithoutTrailingSlash();

            _connectionManager = new ConnectionManager();
            var encoding = configuration.Encoding ?? Encoding.UTF8;

            var defaultQueueOptions = new QueueOptions
            {
                AutoAcknowledge  = configuration.AutoAcknowledge,
                MaxAttempts      = configuration.MaxAttempts,
                ConcurrencyLimit = configuration.ConcurrencyLimit,
                RetryDelay       = configuration.RetryDelay,
                IsDurable        = configuration.IsDurable
            };

            var securityTokenService = configuration.SecurityTokenService ?? new JwtSecurityTokenService();

            _messageJournal = configuration.MessageJournal;

            var queueingOptions = new RabbitMQMessageQueueingOptions(baseUri)
            {
                ConnectionManager    = _connectionManager,
                DefaultQueueOptions  = defaultQueueOptions,
                DiagnosticService    = diagnosticService,
                Encoding             = encoding,
                SecurityTokenService = securityTokenService
            };

            _messageQueueingService = new RabbitMQMessageQueueingService(queueingOptions);

            var transportServiceOptions = new RabbitMQTransportServiceOptions(baseUri, _connectionManager)
            {
                DiagnosticService    = configuration.DiagnosticService,
                MessageJournal       = configuration.MessageJournal,
                SecurityTokenService = configuration.SecurityTokenService,
                Encoding             = configuration.Encoding,
                DefaultQueueOptions  = defaultQueueOptions,
                Topics = configuration.Topics
            };

            _transportService = new RabbitMQTransportService(transportServiceOptions);

            Bus = new Bus(configuration, configuration.BaseUri, _transportService, _messageQueueingService);
        }
        /// <summary>
        /// Initializes a new <see cref="RabbitMQTransportService"/>
        /// </summary>
        /// <param name="options">The options that govern the configuration and behavior of the
        /// RabbitMQ transport</param>
        public RabbitMQTransportService(RabbitMQTransportServiceOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            _baseUri              = options.BaseUri;
            _connectionManager    = options.ConnectionManager;
            _diagnosticService    = options.DiagnosticService ?? DiagnosticService.DefaultInstance;
            _messageJournal       = options.MessageJournal;
            _securityTokenService = options.SecurityTokenService ?? new JwtSecurityTokenService();
            _encoding             = options.Encoding ?? Encoding.GetEncoding(RabbitMQDefaults.Encoding);
            _defaultQueueOptions  = options.DefaultQueueOptions ?? new QueueOptions();

            var connection = _connectionManager.GetConnection(_baseUri);
            var topics     = (options.Topics ?? Enumerable.Empty <TopicName>()).Where(t => t != null);

            using (var channel = connection.CreateModel())
            {
                foreach (var topicName in topics)
                {
                    var exchangeName = topicName.GetTopicExchangeName();
                    channel.ExchangeDeclare(exchangeName, "fanout", _defaultQueueOptions.IsDurable, false, new Dictionary <string, object>());
                    _diagnosticService.Emit(
                        new RabbitMQEventBuilder(this, RabbitMQEventType.RabbitMQExchangeDeclared)
                    {
                        Detail        = "Fanout exchange declared for topic",
                        Exchange      = exchangeName,
                        Topic         = topicName,
                        ChannelNumber = channel.ChannelNumber
                    }.Build());
                }
            }

            _inboundQueue = new RabbitMQQueue(connection, InboxQueueName, this,
                                              _encoding, _defaultQueueOptions, _diagnosticService, _securityTokenService, null);

            _inboundQueue.Init();
        }