コード例 #1
0
        public KafkaConsumer(string brokerlist, string topic)
        {
            var config = new Config {
                GroupId = "group"
            };

            _consumer  = new EventConsumer(config, brokerlist);
            _topicName = topic;
            _i         = 0;

            _consumer.OnMessage += (sender, message) =>
            {
                if (message.Payload.Length <= 0)
                {
                    return;
                }

                _i++;

                if (_i % 5000 == 0)
                {
                    var msg = common.Message.Deserialize(message.Payload);
                    Console.WriteLine($"Topic {_topicName} received msg: {msg.Title} - {msg.Body} - {msg.DeviceId}");
                }
            };

            //_consumer.Subscribe(new List<string>{topic});
            _consumer.Assign(new List <TopicPartitionOffset> {
                new TopicPartitionOffset(topic, 0, Offset.End)
            });
            _consumer.Start();
        }
コード例 #2
0
        public static void Main(string[] args)
        {
            string brokerList = "localhost";
            var    topics     = args.Skip(1).ToList();

            var config = new Config()
            {
                GroupId = "simple-csharp-consumer"
            };

            using (var consumer = new EventConsumer(config, brokerList))
            {
                consumer.OnMessage += (obj, msg) =>
                {
                    string text = Encoding.UTF8.GetString(msg.Payload, 0, msg.Payload.Length);
                    Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {text}");
                };

                consumer.Assign(new List <TopicPartitionOffset> {
                    new TopicPartitionOffset("topicName", 0, 5)
                });
                consumer.Start();

                Console.WriteLine("Started consumer, press enter to stop consuming");
                Console.ReadLine();
            }
        }
コード例 #3
0
        public static void Main(string[] args)
        {
            //string brokerList = "40.118.249.252";
            string brokerList = "138.91.140.244";
            //string brokerList = "localhost";
            List <String> topicList = new List <string>();

            topicList.Add("testLikeWin");
            var topics = topicList;

            var config = new Config()
            {
                GroupId = "simple-csharp-consumer"
            };

            using (var consumer = new EventConsumer(config, brokerList))
            {
                consumer.OnMessage += (obj, msg) =>
                {
                    string text = Encoding.UTF8.GetString(msg.Payload, 0, msg.Payload.Length);
                    Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {text}");
                };

                consumer.Assign(new List <TopicPartitionOffset> {
                    new TopicPartitionOffset(topics.First(), 0, 5)
                });
                consumer.Start();

                Console.WriteLine("Started consumer, press enter to stop consuming");
                Console.ReadLine();
            }
        }
コード例 #4
0
        public void Start()
        {
            var config = new Config()
            {
                GroupId = "simple-csharp-consumer"
            };

            using (var consumer = new EventConsumer(config, "")) {
                consumer.OnMessage += (obj, msg) => {
                    string text = Encoding.UTF8.GetString(msg.Payload, 0, msg.Payload.Length);
                    Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {text}");
                };

                consumer.Assign(new List <TopicPartitionOffset> {
                    new TopicPartitionOffset(topics.First(), 0, 5)
                });

                consumer.Start();

                Console.WriteLine("Started consumer, press enter to stop consuming");
                Console.ReadLine();
            }


            Console.WriteLine("Starting...");
        }
コード例 #5
0
        public static void Main(string[] args)
        {
            //string brokerList = "138.91.140.244";//d4
            string brokerList = "40.118.249.252";//b2
            //string brokerList = "localhost";
            List <String> topicList = new List <string>();

            topicList.Add("testBench24");
            var topics = topicList;

            var config = new Config()
            {
                GroupId = "simple-csharp-consumer"
            };
            DateTime      origin    = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            List <Double> latencies = new List <Double>();

            using (var consumer = new EventConsumer(config, brokerList))
            {
                consumer.OnMessage += (obj, msg) =>
                {
                    string text = Encoding.UTF8.GetString(msg.Payload, 0, msg.Payload.Length);
                    //Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {text}");
                    String receivedMessage = text;

                    String[] substrings    = receivedMessage.Split('_');
                    String   id            = substrings[0];
                    String   sentTimestamp = substrings[1];

                    TimeSpan diff = DateTime.Now.ToUniversalTime() - origin;
                    String   receivedTimestamp = String.Format(Math.Floor(diff.TotalMilliseconds).ToString());

                    String finalMessage = String.Format(id + "_" + sentTimestamp + "_" + receivedTimestamp);
                    //Console.WriteLine("Payload on Consumer side == " + finalMessage);

                    Double latency = Convert.ToDouble(receivedTimestamp) - Convert.ToDouble(sentTimestamp);
                    //Console.WriteLine("Payload on Consumer side == " + finalMessage + ", latency == " + latency);

                    latencies.Add(latency);

                    //printing only on the nearest 100th device, since frequent reporting adds to latency
                    if (latencies.Count == 1000000)
                    {
                        PrintLatencyReport(latencies);
                    }
                    //Console.WriteLine("");
                    //Console.WriteLine(latencies.Count);
                };

                consumer.Assign(new List <TopicPartitionOffset> {
                    new TopicPartitionOffset(topics.First(), 0, 5)
                });
                consumer.Start();

                Console.WriteLine("Started consumer, press enter to stop consuming");
                //Console.ReadLine();
                //PrintLatencyReport(latencies);
                Console.ReadLine();
            }
        }
コード例 #6
0
ファイル: QueueOperationRdKafka.cs プロジェクト: jjjyyy12/Jy
        public void SubscribeTopic <T, TH>(Func <TH> handler, string borkerList, string groupID, List <string> topics)
            where TH : IProcessMessage <T>
            where T : MessageBase
        {
            var containsKey = _subsManager.HasSubscriptionsForEvent <T>();

            if (!containsKey)
            {
                Task.Run(() =>
                {
                    try
                    {
                        if (string.IsNullOrWhiteSpace(borkerList))
                        {
                            borkerList = ConnectionBuilder.getBorkerList();
                        }
                        bool enableAutoCommit = false;

                        var config = new Config()
                        {
                            GroupId            = groupID,
                            EnableAutoCommit   = enableAutoCommit,
                            StatisticsInterval = TimeSpan.FromSeconds(60)
                        };

                        EventConsumer consumer = new EventConsumer(config, borkerList);

                        consumer.OnMessage += async(obj, msgs) => {
                            string text = Encoding.UTF8.GetString(msgs.Payload, 0, msgs.Payload.Length);
                            _logger.LogInformation("Topic: {0} Partition: {1} Offset: {2} {3}", msgs.Topic, msgs.Partition, msgs.Offset, text);

                            MessageBase mb = new MessageBase()
                            {
                                MessageBodyByte = msgs.Payload,
                                MessageRouter   = msgs.Topic
                            };

                            await ProcessEvent(mb.MessageRouter, mb);
                            if (!enableAutoCommit && msgs.Offset % 10 == 0)
                            {
                                _logger.LogInformation($"Committing offset");
                                consumer.Commit(msgs).Wait();
                                _logger.LogInformation($"Committed offset");
                            }
                        };

                        consumer.OnConsumerError += (obj, errorCode) =>
                        {
                            _logger.LogWarning("Consumer Error: {0}", errorCode);
                        };

                        consumer.OnEndReached += (obj, end) => {
                            _logger.LogInformation("Reached end of topic {0} partition {1}, next message will be at offset {2}", end.Topic, end.Partition, end.Offset);
                        };

                        consumer.OnError += (obj, error) => {
                            _logger.LogWarning("Error: {0} {1}", error.ErrorCode, error.Reason);
                        };

                        if (enableAutoCommit)
                        {
                            consumer.OnOffsetCommit += (obj, commit) => {
                                if (commit.Error != ErrorCode.NO_ERROR)
                                {
                                    _logger.LogInformation("Failed to commit offsets: {0}", commit.Error);
                                }
                                _logger.LogInformation("Successfully committed offsets: [{0}]", string.Join(", ", commit.Offsets));
                            };
                        }

                        consumer.OnPartitionsAssigned += (obj, partitions) => {
                            _logger.LogInformation("Assigned partitions: [{0}], member id: {1}", string.Join(", ", partitions), consumer.MemberId);
                            consumer.Assign(partitions);
                        };

                        consumer.OnPartitionsRevoked += (obj, partitions) => {
                            _logger.LogInformation("Revoked partitions: [{0}]", string.Join(", ", partitions));
                            consumer.Unassign();
                        };

                        consumer.OnStatistics += (obj, json) => {
                            _logger.LogInformation("Statistics: {0}", json);
                        };

                        consumer.Subscribe(topics);
                        consumer.Start();

                        _logger.LogInformation("Assigned to: [{0}]", string.Join(", ", consumer.Assignment));
                        _logger.LogInformation("Subscribed to: [{0}]", string.Join(", ", consumer.Subscription));
                        _logger.LogInformation("Started consumer, press enter to stop consuming");

                        while (true)
                        {
                            Thread.Sleep(1);
                        }
                        // Console.ReadLine();
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError("SubscribeTopicAsync", ex);
                        return(ex.Message);
                    }
                });//----------end run
            }//-----------end if
            _subsManager.AddSubscription <T, TH>(handler);
        }//-----------end SubscribeTopic
コード例 #7
0
        public static void Run(string brokerList, List <string> topics)
        {
            bool enableAutoCommit = false;

            var config = new Config()
            {
                GroupId            = "advanced-csharp-consumer",
                EnableAutoCommit   = enableAutoCommit,
                StatisticsInterval = TimeSpan.FromSeconds(60)
            };

            using (var consumer = new EventConsumer(config, brokerList))
            {
                consumer.OnMessage += (obj, msg) =>
                {
                    string text = Encoding.UTF8.GetString(msg.Payload, 0, msg.Payload.Length);
                    Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {text}");
                    Console.WriteLine("1 Response: Partition {0},Offset {1} : {2}",
                                      msg.Partition, msg.Offset, text);
                    if (!enableAutoCommit && msg.Offset % 10 == 0)
                    {
                        Console.WriteLine("Committing offset");
                        consumer.Commit(msg).Wait();
                        Console.WriteLine("Committed offset");
                    }
                };

                consumer.OnConsumerError += (obj, errorCode) =>
                {
                    Console.WriteLine($"Consumer Error: {errorCode}");
                };

                consumer.OnEndReached += (obj, end) =>
                {
                    Console.WriteLine($"Reached end of topic {end.Topic} partition {end.Partition}, next message will be at offset {end.Offset}");
                };

                consumer.OnError += (obj, error) =>
                {
                    Console.WriteLine($"Error: {error.ErrorCode} {error.Reason}");
                };

                if (enableAutoCommit)
                {
                    consumer.OnOffsetCommit += (obj, commit) =>
                    {
                        if (commit.Error != ErrorCode.NO_ERROR)
                        {
                            Console.WriteLine($"Failed to commit offsets: {commit.Error}");
                        }
                        Console.WriteLine($"Successfully committed offsets: [{string.Join(", ", commit.Offsets)}]");
                    };
                }

                consumer.OnPartitionsAssigned += (obj, partitions) =>
                {
                    Console.WriteLine($"Assigned partitions: [{string.Join(", ", partitions)}], member id: {consumer.MemberId}");
                    consumer.Assign(partitions);
                };

                consumer.OnPartitionsRevoked += (obj, partitions) =>
                {
                    Console.WriteLine($"Revoked partitions: [{string.Join(", ", partitions)}]");
                    consumer.Unassign();
                };

                consumer.OnStatistics += (obj, json) =>
                {
                    Console.WriteLine($"Statistics: {json}");
                };

                consumer.Subscribe(topics);
                consumer.Start();

                Console.WriteLine($"Assigned to: [{string.Join(", ", consumer.Assignment)}]");
                Console.WriteLine($"Subscribed to: [{string.Join(", ", consumer.Subscription)}]");

                Console.WriteLine($"Started consumer, press enter to stop consuming");
                Console.ReadLine();
            }
        }