Esempio n. 1
0
        static void Main(string[] args)
        {
            _stopWatch = Stopwatch.StartNew();
            var timer = new Timer(PrintStatistics, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));

            _client = new ConfluentClient(ConfigurationManager.AppSettings["Confluent.BaseUrl"]);


            try
            {
                int batchSize = Convert.ToInt32(ConfigurationManager.AppSettings["Confluent.BatchSize"]);
                var random    = new Random();

                if (batchSize <= 0)
                {
                    batchSize = 1;
                }

                while (true)
                {
                    var people = new Person[batchSize];
                    for (int i = 0; i < batchSize; i++)
                    {
                        string name = string.Format("[{0}] {1}", _totalLogs, Guid.NewGuid().ToString("N"));
                        people[i] = new Person {
                            Name = name, Age = random.Next(20, 100)
                        };
                        Interlocked.Increment(ref _totalLogs);
                    }

                    string response = _client.Publish(Topic, people).Result;
                }
            }
            catch (Exception ex)
            {
                timer.Change(Timeout.Infinite, Timeout.Infinite);
                Console.WriteLine(ex);
            }
            finally
            {
                Console.WriteLine("Done!");
                Console.ReadLine();
            }
        }
        private void buttonPublish_Click(object sender, EventArgs e)
        {
            var random = new Random();

            Task.Run(() =>
            {
                int numberOfMessages = Convert.ToInt32(textBoxNumberOfMsg.Text);

                for (int i = 0; i < numberOfMessages; i++)
                {
                    _index++;

                    string name = string.Format("[{0}] {1}", _index, Guid.NewGuid().ToString("N"));
                    var person  = new Person {
                        Name = name, Age = random.Next(20, 100)
                    };
                    PrependMessage(JsonConvert.SerializeObject(person));

                    string response = _client.Publish(textBoxTopic.Text, new[] { person }).Result;
                    PrependMessage(response);
                }
            });
        }