Brod.Consumers.ConsumerStateStorage.ReadStreamState C# (CSharp) Метод

ReadStreamState() публичный Метод

public ReadStreamState ( String topic, String group, List partitions ) : StreamState
topic String
group String
partitions List
Результат StreamState
        public StreamState ReadStreamState(String topic, String group, List<Int32> partitions)
        {
            if (partitions == null || partitions.Count == 0)
                throw new ArgumentException("Empty partitions set not allowed");

            Dictionary<Int32, Int32> offsetByPartition = new Dictionary<Int32, Int32>(partitions.Count);

            foreach (var partition in partitions)
            {
                var stateFilePath = GetPartitionStateFilePath(topic, group, partition);

                if (!File.Exists(stateFilePath))
                {
                    var dir = GetPartitionDirectoryPath(topic, group, partition);
                    Directory.CreateDirectory(dir);
                    using (File.Create(stateFilePath)) { }
                }

                var offset = File.ReadAllText(stateFilePath);

                offsetByPartition[partition] = String.IsNullOrWhiteSpace(offset) ? 0 :
                    Int32.Parse(offset, CultureInfo.InvariantCulture);
            }

            return new StreamState(topic, group, offsetByPartition);
        }

Usage Example

Пример #1
0
        private void Start()
        {
            _started      = true;
            _stateStorage = new ConsumerStateStorage(_stateStorageDirectory);
            _streamState  = _stateStorage.ReadStreamState(Topic, "default-group", Partitions);

            var task = Task.Factory.StartNew(() =>
            {
                var consumer = new PartitionConsumer(_brokerAddress, _context);

                var offsetByPartition = new Dictionary <Int32, Int32>();
                foreach (var pair in _streamState.OffsetByPartition)
                {
                    offsetByPartition.Add(pair.Key, pair.Value);
                }

                while (true)
                {
                    // If we already have enough messages in queue - wait
                    if (Messages.Count > 100)
                    {
                        continue;
                    }

                    var result = consumer.Load(Topic, offsetByPartition, 300);

                    var messageCount = 0;
                    foreach (var tuple in result)
                    {
                        Messages.Enqueue(tuple);
                        offsetByPartition[tuple.Item1] += Message.CalculateOnDiskMessageLength(tuple.Item2.Payload.Length);
                        messageCount++;
                    }

                    // Wait for 500 msecond, if there is no new messages
                    // This value should be configurable
                    if (messageCount == 0)
                    {
                        Thread.Sleep(500);
                    }
                }
            });
        }
All Usage Examples Of Brod.Consumers.ConsumerStateStorage::ReadStreamState