コード例 #1
0
ファイル: ConsumerCtl.xaml.cs プロジェクト: gitluopu/TaaMgr
        private void ConsumeClick(object sender, RoutedEventArgs arg)
        {
            m_btnConsume.IsEnabled = false;
            m_btnStop.Dispatcher.Invoke(() => { m_btnStop.IsEnabled = true; });
            BrokerUnit bu = new BrokerUnit(m_broker);

            try
            {
                Common.Net.TcpConnectionTest(bu.m_ip, bu.m_port, int.Parse(AppConfig.m_conf.AppSettings.Settings["operationTimeout"].Value));
            }
            catch (Exception ex)
            {
                m_btnConsume.IsEnabled = true;
                m_btnStop.IsEnabled    = false;
                MessageBox.Show(ex.Message);
                return;
            }
            Task.Run(() =>
            {
                UInt32 cntMax       = UInt32.Parse(m_cnt);
                m_cts               = new CancellationTokenSource();
                StreamWriter writer = null;
                if (m_save2File)
                {
                    writer = new StreamWriter(Common.Dir.GetCacheDir() + bu.m_ip + "-" + m_topic + ".log");
                }
                string groupId = null;
                if (m_groupId == "")
                {
                    groupId = DateTime.Now.ToString();
                }
                else
                {
                    groupId = m_groupId;
                }
                var conf = new ConsumerConfig
                {
                    GroupId = groupId,
                    //AutoOffsetReset = AutoOffsetReset.Latest,
                    BootstrapServers = bu.m_broker,
                    //SocketTimeoutMs = 1000,
                    //SessionTimeoutMs = 1000,
                    //MetadataRequestTimeoutMs=1000,
                    ReconnectBackoffMs    = 1000,
                    ReconnectBackoffMaxMs = 3000,


                    // Note: The AutoOffsetReset property determines the start offset in the event
                    // there are not yet any committed offsets for the consumer group for the
                    // topic/partitions of interest. By default, offsets are committed
                    // automatically, so in this example, consumption will only start from the
                    // earliest message in the topic 'my-topic' the first time you run the program.
                    AutoOffsetReset = AutoOffsetReset.Latest
                };
                using (var c = new ConsumerBuilder <Ignore, string>(conf).Build())
                {
                    c.Subscribe(m_topic);
                    uint cnt = 0;
                    try
                    {
                        while (true)
                        {
                            try
                            {
                                var cr = c.Consume(m_cts.Token);
                                if (m_save2File)
                                {
                                    writer.Write(cr.Message.Value + System.Environment.NewLine);
                                }
                                OnConsumeMsg?.Invoke(this, cr.Message.Value);
                                cnt++;
                                if (cntMax != 0)
                                {
                                    if (cnt >= cntMax)
                                    {
                                        c.Close();
                                        break;
                                    }
                                }
                                Thread.Sleep(10);
                                //Console.WriteLine($"Consumed message '{cr.Value}' at: '{cr.TopicPartitionOffset}'.");
                            }
                            catch (ConsumeException ex)
                            {
                                MessageBox.Show(ex.Message);
                                //Log.LogErr(e.Error.Reason);
                                //Console.WriteLine($"Error occured: {e.Error.Reason}");
                            }
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        // Ensure the consumer leaves the group cleanly and final offsets are committed.
                        c.Close();
                        //return;
                    }
                }
                if (m_save2File)
                {
                    writer.Close();
                }

                m_btnConsume.Dispatcher.Invoke(() => { m_btnConsume.IsEnabled = true; });
                m_btnStop.Dispatcher.Invoke(() => { m_btnStop.IsEnabled = false; });
            });
        }
コード例 #2
0
ファイル: Consumer.xaml.cs プロジェクト: gitluopu/TAAKit
 private void HandleMsg(string msg)
 {
     OnConsumeMsg?.Invoke(this, msg);
 }