Esempio n. 1
0
        public void Consume(string topic)
        {
            var consumerThread = new Thread(() => {
                var rand = new Random();

                while (true)
                {
                    int i = rand.Next(10);

                    if (i >= 5)
                    {
                        var newUser = new User()
                        {
                            Firstname = "dg",
                            Surname   = " sdfg"
                        };

                        OnMessageRecieved.Invoke(newUser as T);
                    }
                    else
                    {
                        OnConsumeError.Invoke(null);
                    }
                }
            });

            consumerThread.Start();
        }
Esempio n. 2
0
        private void _Consume <T1>(string topic)
        {
            using (var c = new ConsumerBuilder <T1, string>(Config).Build()) {
                c.Subscribe(topic);

                try {
                    while (true)
                    {
                        try {
                            var cr = c.Consume();

                            var dMessage = Newtonsoft.Json.JsonConvert.DeserializeObject <t3>(cr.Value);

                            OnMessageRecieved?.Invoke(dMessage);
                            // Console.WriteLine($"Consumed message '{cr.Value}' at: '{cr.TopicPartitionOffset}'.");
                        } catch (ConsumeException e) {
                            OnConsumeError?.Invoke(e);
                            // Console.WriteLine($"Error occured: {e.Error.Reason}");
                        }
                    }
                } catch (OperationCanceledException) {
                    // Ensure the consumer leaves the group cleanly and final offsets are committed.
                    c.Close();
                }
            }
        }