The rabbit channel.
상속: IChannel
예제 #1
0
        public RabbitChannel OpenChannel(CancellationToken token)
        {
            lock (this.syncRoot)
            {
                while (true)
                {
                    token.ThrowIfCancellationRequested();

                    if (this.connection == null || !this.connection.IsOpen)
                    {
                        this.Open(token);
                    }

                    try
                    {
                        var model   = this.connection.CreateModel();
                        var channel = new RabbitChannel(this.Id, model, this.busContext);
                        return(channel);
                    }
                    catch (Exception ex)
                    {
                        this.logger.Error($"Failed to open a new channel due to: {ex.Message}; retrying...", ex);
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// The resolve for.
        /// </summary>
        /// <param name="configuration">
        /// The configuration.
        /// </param>
        /// <returns>
        /// The <see cref="Listener"/>.
        /// </returns>
        public Listener ResolveFor(IReceiverConfiguration configuration)
        {
            using (RabbitChannel channel = this.bus.OpenChannel())
            {
                var topologyBuilder = new TopologyBuilder(channel);
                var builder         = new SubscriptionEndpointBuilder(this.bus.Endpoint, topologyBuilder, configuration);

                Maybe <Func <ISubscriptionEndpointBuilder, ISubscriptionEndpoint> > endpointBuilder = configuration.Options.GetEndpointBuilder();

                Assumes.True(endpointBuilder != null, "EndpointBuilder is null for [{0}].", configuration.Label);

                ISubscriptionEndpoint endpoint = endpointBuilder.Value(builder);

                lock (this.listeners)
                {
                    Listener listener = this.listeners.FirstOrDefault(l => l.Endpoint.ListeningSource.Equals(endpoint.ListeningSource));
                    if (listener == null)
                    {
                        listener = new Listener(this.bus, endpoint, (RabbitReceiverOptions)configuration.Options, this.bus.Configuration.ValidatorRegistry);
                        this.listeners.Add(listener);
                    }
                    else
                    {
                        EnsureConfigurationIsCompatible(listener, configuration);
                    }

                    return(listener);
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Инициализирует новый экземпляр класса <see cref="RabbitDelivery"/>.
        /// </summary>
        /// <param name="channel">Канал поставки сообщения.</param>
        /// <param name="args">Параметры поставки сообщения.</param>
        /// <param name="requiresAccept">Верно, если требуется подтверждение доставки.</param>
        public RabbitDelivery(RabbitChannel channel, BasicDeliverEventArgs args, bool requiresAccept)
        {
            this.Channel        = channel;
            this.Label          = channel.Bus.MessageLabelHandler.Resolve(args);
            this.Args           = args;
            this.requiresAccept = requiresAccept;

            this.headers = new Lazy <IDictionary <string, object> >(
                () => this.ExtractHeadersFrom(args));
        }
예제 #4
0
        /// <summary>
        /// The open channel.
        /// </summary>
        /// <returns>
        /// The <see cref="RabbitChannel"/>.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// </exception>
        public virtual RabbitChannel OpenChannel()
        {
            if (this.Connection == null || !this.Connection.IsOpen)
            {
                throw new InvalidOperationException("RabbitMQ Connection is not open.");
            }

            try
            {
                var channel = new RabbitChannel(this, this.Connection.CreateModel());
                channel.Failed += (ch, args) => this.HandleBusFailure(args.GetException());
                return(channel);
            }
            catch (Exception ex)
            {
                this.HandleBusFailure(ex);
                throw;
            }
        }
예제 #5
0
 public RabbitChannel OpenChannel()
 {
     if (this.connection == null || !this.connection.IsOpen)
     {
         throw new InvalidOperationException("RabbitMQ connection is not open.");
     }
     try
     {
         var model   = this.connection.CreateModel();
         var channel = new RabbitChannel(model, this.busContext);
         channel.Failed += (ch, args) => this.OnChannelFailed(ch, args.GetException());
         return(channel);
     }
     catch (Exception ex)
     {
         this.logger.Error($"Failed to open a new channel in connection [{this}] due to: {ex.Message}", ex);
         throw;
     }
 }
예제 #6
0
        /// <summary>
        /// The resolve for.
        /// </summary>
        /// <param name="configuration">
        /// The configuration.
        /// </param>
        /// <returns>
        /// The <see cref="Producer"/>.
        /// </returns>
        public Producer ResolveFor(ISenderConfiguration configuration)
        {
            Producer producer = this.TryResolverFor(configuration.Label);

            if (producer == null)
            {
                using (RabbitChannel channel = this._bus.OpenChannel())
                {
                    var topologyBuilder = new TopologyBuilder(channel);
                    var builder         = new RouteResolverBuilder(this._bus.Endpoint, topologyBuilder, configuration);
                    Maybe <Func <IRouteResolverBuilder, IRouteResolver> > routeResolverBuilder = configuration.Options.GetRouteResolverBuilder();

                    Assumes.True(routeResolverBuilder.HasValue, "RouteResolverBuilder must be set for [{0}]", configuration.Label);

                    IRouteResolver routeResolver = routeResolverBuilder.Value(builder);

                    producer         = new Producer(this._bus, configuration.Label, routeResolver, configuration.Options.IsConfirmationRequired());
                    producer.Failed += p =>
                    {
                        {
                            // lock (_producers)
                            p.Dispose();
                            this._producers.Remove(p.Label);
                        }
                    };

                    // lock (_producers)
                    this._producers.Add(configuration.Label, producer);
                }

                if (configuration.RequiresCallback)
                {
                    producer.UseCallbackListener(this._bus.ListenerRegistry.ResolveFor(configuration.CallbackConfiguration));
                }
            }

            return(producer);
        }
예제 #7
0
        private CancellableQueueingConsumer InitializeConsumer(CancellationToken token, out RabbitChannel channel)
        {
            // Opening a new channel may lead to a new connection creation
            channel           = this.connection.OpenChannel(token);
            channel.Shutdown += this.OnChannelShutdown;
            this.channels.Add(channel);

            if (this.ReceiverOptions.GetQoS().HasValue)
            {
                channel.SetQos(
                    this.ReceiverOptions.GetQoS().Value);
            }

            var consumer = channel.BuildCancellableConsumer(token);
            var tag      = channel.StartConsuming(
                this.endpoint.ListeningSource,
                this.ReceiverOptions.IsAcceptRequired(),
                consumer);

            this.logger.Trace(
                $"A consumer tagged [{tag}] has been registered in listener of [{string.Join(",", this.AcceptedLabels)}]");

            return(consumer);
        }
예제 #8
0
 /// <summary>
 /// Создает входящее сообщение.
 /// </summary>
 /// <param name="deliveryChannel">
 /// The delivery Channel.
 /// </param>
 /// <param name="args">
 /// Аргументы, с которыми получено сообщение.
 /// </param>
 /// <returns>
 /// Входящее сообщение.
 /// </returns>
 public RabbitDelivery BuildDeliveryFrom(RabbitChannel deliveryChannel, BasicDeliverEventArgs args)
 {
     return(new RabbitDelivery(this.busContext, deliveryChannel, args, this.ReceiverOptions.IsAcceptRequired()));
 }
예제 #9
0
        /// <summary>
        /// The open channel.
        /// </summary>
        /// <returns>
        /// The <see cref="RabbitChannel"/>.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// </exception>
        public virtual RabbitChannel OpenChannel()
        {
            if (this.Connection == null || !this.Connection.IsOpen)
            {
                throw new InvalidOperationException("RabbitMQ Connection is not open.");
            }

            try
            {
                var channel = new RabbitChannel(this, this.Connection.CreateModel());
                channel.Failed += (ch, args) => this.HandleBusFailure(args.GetException());
                return channel;
            }
            catch (Exception ex)
            {
                this.HandleBusFailure(ex);
                throw;
            }
        }
 /// <summary>
 /// Инициализирует новый экземпляр класса <see cref="DefaultPublishConfirmationTracker"/>.
 /// </summary>
 /// <param name="channel">
 /// The channel.
 /// </param>
 public DefaultPublishConfirmationTracker(RabbitChannel channel)
 {
     this.channel = channel;
 }
예제 #11
0
 /// <summary>
 /// Инициализирует новый экземпляр класса <see cref="TopologyBuilder"/>.
 /// </summary>
 /// <param name="channel">
 /// Соединение с шиной сообщений, через которое настраивается топология.
 /// </param>
 public TopologyBuilder(IChannel channel)
 {
     // TODO: нужно работать с более общим типом IChannel.
     this.rabbitChannel = (RabbitChannel)channel;
 }
예제 #12
0
 /// <summary>
 /// Инициализирует новый экземпляр класса <see cref="RabbitClient"/>.
 /// </summary>
 /// <param name="channel">
 /// The channel.
 /// </param>
 protected RabbitClient(RabbitChannel channel)
 {
     this.Channel = channel;
 }
예제 #13
0
 /// <summary>
 /// Инициализирует новый экземпляр класса <see cref="RabbitClient"/>.
 /// </summary>
 /// <param name="channel">
 /// The channel.
 /// </param>
 protected RabbitClient(RabbitChannel channel)
 {
     this.Channel = channel;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="PublishConfirmationTracker"/> class.
 /// </summary>
 /// <param name="channel">
 /// The channel.
 /// </param>
 public PublishConfirmationTracker(RabbitChannel channel)
 {
     this.logger            = LogManager.GetLogger($"{this.GetType().FullName}({this.GetHashCode()})");
     this.channel           = channel;
     this.channel.Shutdown += this.OnChannelShutdown;
 }
예제 #15
0
 /// <summary>
 /// »нициализирует новый экземпл¤р класса <see cref="DefaultPublishConfirmationTracker"/>.
 /// </summary>
 /// <param name="channel">
 /// The channel.
 /// </param>
 public DefaultPublishConfirmationTracker(RabbitChannel channel)
 {
     this.channel = channel;
 }