Ejemplo n.º 1
0
        /// <summary>
        /// Create the correct implementation of IConsumer based on queue properties
        /// </summary>
        /// <param name="queue"></param>
        /// <param name="onMessage"></param>
        /// <param name="connection"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        private IConsumer CreateConsumerInstance(
            IQueue queue,
            Func <byte[], MessageProperties, MessageReceivedInfo, Task> onMessage,
            PersistentConnection connection,
            ConsumerConfiguration configuration)
        {
            if (queue.IsExclusive)
            {
                return(new TransientConsumer(queue, onMessage, connection, configuration, this._internalConsumerFactory));
            }

            return(new PersistentConsumer(queue, onMessage, connection, configuration, this._internalConsumerFactory));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 创建一个消费者
        /// </summary>
        /// <param name="queue"></param>
        /// <param name="onMessage"></param>
        /// <param name="connection"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public IConsumer CreateConsumer(
            IQueue queue,
            Func <byte[], MessageProperties, MessageReceivedInfo, Task> onMessage,
            PersistentConnection connection,
            ConsumerConfiguration configuration
            )
        {
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(onMessage, "onMessage");
            Preconditions.CheckNotNull(connection, "connection");

            var consumer = this.CreateConsumerInstance(queue, onMessage, connection, configuration);

            this._consumers.TryAdd(consumer, null);
            return(consumer);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="queue"></param>
        /// <param name="onMessage"></param>
        /// <param name="configure"></param>
        /// <returns></returns>
        private IDisposable Consume(IQueue queue, Func <Byte[], MessageProperties, MessageReceivedInfo, Task> onMessage, Action <ConsumerConfiguration> configure)
        {
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(onMessage, "onMessage");
            Preconditions.CheckNotNull(configure, "configure");

            if (this._disposed)
            {
                throw new Exception("This bus has been disposed");
            }
            ConsumerConfiguration consumerConfiguration = new ConsumerConfiguration(this._connectionConfiguration.PrefetchCount);

            configure(consumerConfiguration);
            IConsumer consumer = this._consumerFactory.CreateConsumer(queue, onMessage, this._connection, consumerConfiguration);

            return(consumer.StartConsuming());
        }
Ejemplo n.º 4
0
        public PersistentConsumer(
            IQueue queue,
            Func <byte[], MessageProperties, MessageReceivedInfo, Task> onMessage,
            PersistentConnection connection,
            ConsumerConfiguration configuration,
            InternalConsumerFactory internalConsumerFactory)
        {
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(onMessage, "onMessage");
            Preconditions.CheckNotNull(connection, "connection");
            Preconditions.CheckNotNull(configuration, "configuration");
            Preconditions.CheckNotNull(internalConsumerFactory, "internalConsumerFactory");

            this._queue                   = queue;
            this._onMessage               = onMessage;
            this._connection              = connection;
            this._configuration           = configuration;
            this._internalConsumerFactory = internalConsumerFactory;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 收消息
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="queue"></param>
        /// <param name="onMessage"></param>
        /// <param name="configuration"></param>
        public void StartConsuming(PersistentConnection connection, IQueue queue, Func <byte[], MessageProperties, MessageReceivedInfo, Task> onMessage, ConsumerConfiguration configuration)
        {
            Preconditions.CheckNotNull(connection, "connection");
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(onMessage, "onMessage");
            Preconditions.CheckNotNull(configuration, "configuration");

            this.queue      = queue;
            this._onMessage = onMessage;
            var consumerTag = Conventions.ConsumerTagConvention();//目前就是一个Guid的值
            IDictionary <string, object> arguments = new Dictionary <string, object>
            {
                { "x-priority", configuration.Priority },
                { "x-cancel-on-ha-failover", configuration.CancelOnHaFailover || this._connectionConfiguration.CancelOnHaFailover }
            };

            try
            {
                this.Model = connection.CreateModel();

                this.Model.BasicQos(0, configuration.PrefetchCount, false);

                this.Model.BasicConsume(
                    queue.Name,         // queue
                    false,              // noAck
                    consumerTag,        // consumerTag
                    arguments,          // arguments
                    this);              // consumer

                ConsoleLogger.InfoWrite("Declared Consumer. queue='{0}', consumer tag='{1}' prefetchcount={2} priority={3} x-cancel-on-ha-failover={4}", queue.Name, consumerTag, _connectionConfiguration.PrefetchCount, configuration.Priority, configuration.CancelOnHaFailover);
            }
            catch (Exception exception)
            {
                ConsoleLogger.ErrorWrite("Consume failed. queue='{0}', consumer tag='{1}', message='{2}'", queue.Name, consumerTag, exception.Message);
            }
        }