Exemplo n.º 1
0
        /// <summary>
        /// Subscription is performed asynchronously.
        /// </summary>
        /// <param name="seedBrokers">Comma separated list of seed brokers. Port numbers are optional.
        /// <example>192.168.56.10,192.168.56.20:8081,broker3.local.net:8181</example>
        /// </param>
        /// <param name="topic"></param>
        /// <param name="startPosition"></param>
        /// <param name="maxWaitTimeMs"></param>
        /// <param name="minBytesPerFetch"></param>
        /// <param name="maxBytesPerFetch"></param>
        /// <param name="highWatermark"></param>
        /// <param name="useFlowControl">
        ///     If set to true, subscriber must call consumers Ack function to keep data flowing.
        ///     Is used to prevent out of memory errors when subscriber is slow and data velocity is high
        ///     (re-reading the log from beginning for example).
        ///     Make sure that subscriber consumes more than lowWatermark, or data might stop flowing because driver would
        ///     wait for subscriber to drain until lowWatermark and subscriber would wait for more data until continue processing
        ///     (could happen when buffering is used).
        /// </param>
        /// <param name="lowWatermark"></param>
        /// <param name="stopPosition"></param>
        /// <param name="scheduler">Driver will schedule outgoing messages events using this scheduler. By default it is
        /// EventLoopScheduler. Be careful if you want to redefine it. Concurrent scheduler can rearrange order of messages
        /// within the same partition!</param>
        public ConsumerConfiguration(
            string seedBrokers,
            string topic,
            IStartPositionProvider startPosition,
            int maxWaitTimeMs    = 500,
            int minBytesPerFetch = 1,
            int maxBytesPerFetch = 256 *1024,
            int lowWatermark     = 500,
            int highWatermark    = 2000,
            bool useFlowControl  = false,
            IStopPositionProvider stopPosition = null,
            IScheduler scheduler = null)
        {
            LowWatermark   = lowWatermark;
            HighWatermark  = highWatermark;
            UseFlowControl = useFlowControl;

            if (lowWatermark < 0)
            {
                throw new ArgumentException("Can not be negative", "lowWatermark");
            }

            if (highWatermark < 0)
            {
                throw new ArgumentException("Can not be negative", "highWatermark");
            }

            if (highWatermark < lowWatermark)
            {
                throw new InvalidOperationException("highWatermark must be greater than lowWatermark");
            }

            SeedBrokers       = seedBrokers;
            StartPosition     = startPosition;
            Topic             = topic;
            MaxWaitTimeMs     = maxWaitTimeMs;
            MinBytesPerFetch  = minBytesPerFetch;
            MaxBytesPerFetch  = maxBytesPerFetch;
            StopPosition      = stopPosition ?? new StopPositionNever();
            OutgoingScheduler = scheduler ?? new EventLoopScheduler(ts => new Thread(ts)
            {
                IsBackground = true
            });
        }
        /// <summary>
        /// Subscription is performed asynchronously.
        /// </summary>
        /// <param name="seedBrokers">Comma separated list of seed brokers. Port numbers are optional.
        /// <example>192.168.56.10,192.168.56.20:8081,broker3.local.net:8181</example>
        /// </param>
        /// <param name="topic"></param>
        /// <param name="startPosition"></param>
        /// <param name="maxWaitTimeMs"></param>
        /// <param name="minBytesPerFetch"></param>
        /// <param name="maxBytesPerFetch"></param>
        /// <param name="highWatermark"></param>
        /// <param name="useFlowControl">
        ///     If set to true, subscriber must call consumers Ack function to keep data flowing.
        ///     Is used to prevent out of memory errors when subscriber is slow and data velocity is high 
        ///     (re-reading the log from beginning for example).
        ///     Make sure that subscriber consumes more than lowWatermark, or data might stop flowing because driver would
        ///     wait for subscriber to drain until lowWatermark and subscriber would wait for more data until continue processing 
        ///     (could happen when buffering is used).
        /// </param>
        /// <param name="lowWatermark"></param>
        /// <param name="stopPosition"></param>
        /// <param name="scheduler">Driver will schedule outgoing messages events using this scheduler. By default it is
        /// EventLoopScheduler. Be careful if you want to redefine it. Concurrent scheduler can rearrange order of messages
        /// within the same partition!</param>
        public ConsumerConfiguration(
            string seedBrokers,
            string topic, 
            IStartPositionProvider startPosition, 
            int maxWaitTimeMs=500, 
            int minBytesPerFetch = 1, 
            int maxBytesPerFetch=256*1024,
            int lowWatermark = 500,
            int highWatermark = 2000,
            bool useFlowControl = false,
            IStopPositionProvider stopPosition = null,
            IScheduler scheduler = null)
        {
            LowWatermark = lowWatermark;
            HighWatermark = highWatermark;
            UseFlowControl = useFlowControl;

            if(lowWatermark < 0)
                throw new ArgumentException("Can not be negative", "lowWatermark");

            if (highWatermark < 0)
                throw new ArgumentException("Can not be negative", "highWatermark");

            if(highWatermark < lowWatermark)
                throw new InvalidOperationException("highWatermark must be greater than lowWatermark");

            SeedBrokers = seedBrokers;
            StartPosition = startPosition;
            Topic = topic;
            MaxWaitTimeMs = maxWaitTimeMs;
            MinBytesPerFetch = minBytesPerFetch;
            MaxBytesPerFetch = maxBytesPerFetch;
            StopPosition = stopPosition ?? new StopPositionNever();
            OutgoingScheduler = scheduler ?? new EventLoopScheduler(ts => new Thread(ts) { IsBackground = true});
        }