Example #1
0
 //it's called in consume thread
 private void Write2File(object sender, string e)
 {
     if (!save2File_)
     {
         return;
     }
     if (writer_ == null)
     {
         BrokerAddr addr = new BrokerAddr(broker_);
         if (!Directory.Exists(cacheDir_))
         {
             Directory.CreateDirectory(cacheDir_);
         }
         writer_ = new StreamWriter(cacheDir_ + addr.ip_ + "-" + topic_ + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + ".log");
     }
     writer_.Write(e + System.Environment.NewLine);
 }
Example #2
0
        private void OnProduceClick(object sender, RoutedEventArgs arg)
        {
            ChangeStatusOnProduce();
            int cntMax   = int.Parse(cnt_);
            int interval = int.Parse(interval_);

            Task.Run(() =>
            {
                BrokerAddr addr = new BrokerAddr(broker_);
                try
                {
                    Utils.TcpPortTest(addr.ip_, addr.port_, 3000);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    ChangeStatusOnStop();
                    return;
                }
                int okayCnt             = 0;
                int errCnt              = 0;
                int sendCnt             = 0;
                m_cts                   = new CancellationTokenSource();
                HashSet <string> errSet = new HashSet <string>();
                Action <DeliveryReport <Null, string> > handler = new Action <DeliveryReport <Null, string> >((r) =>
                {
                    if (r.Error.IsError)
                    {
                        errCnt++; errSet.Add(r.Error.Reason);
                    }
                    else
                    {
                        okayCnt++;
                    }
                });
                var config = new ProducerConfig
                {
                    BootstrapServers = addr.broker_,
                    MessageTimeoutMs = 3000,
                };

                using (var p = new ProducerBuilder <Null, string>(config).Build())
                {
                    for (int i = 0; i < cntMax; i++)
                    {
                        p.Produce(topic_, new Message <Null, string> {
                            Value = msg_
                        }, handler);
                        sendCnt++;
                        if (interval > 0)
                        {
                            Thread.Sleep(interval);
                        }
                        if (m_cts.IsCancellationRequested)
                        {
                            break;
                        }
                    }
                    p.Flush(m_cts.Token);
                }
                int recvCnt   = okayCnt + errCnt;
                string retMsg = "send/recv/error:" + sendCnt + "/" + recvCnt + "/" + errCnt;
                if (errCnt > 0)
                {
                    foreach (var ele in errSet)
                    {
                        retMsg += "," + ele;
                    }
                }
                OnProduceCompleted?.Invoke(this, retMsg);
                ChangeStatusOnStop();
            });
        }
Example #3
0
        private void OnConsumeClick(object sender, RoutedEventArgs arg)
        {
            ChangeStatusOnConsume();
            Task.Run(() =>
            {
                BrokerAddr addr = new BrokerAddr(broker_);
                try {
                    Utils.TcpPortTest(addr.ip_, addr.port_, 3000);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    ChangeStatusOnStop();
                    return;
                }
                cts_           = new CancellationTokenSource();
                matchCnt_      = 0;
                totalCnt_      = 0;
                string groupId = null;
                if (groupId_ == "")
                {
                    groupId = DateTime.Now.ToString();
                }
                else
                {
                    groupId = groupId_;
                }
                var conf = new ConsumerConfig
                {
                    GroupId = groupId,
                    //AutoOffsetReset = AutoOffsetReset.Latest,
                    BootstrapServers = addr.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(topic_);
                    try
                    {
                        while (true)
                        {
                            try
                            {
                                var cr     = c.Consume(cts_.Token);
                                string msg = cr.Message.Value;
                                if (Match(msg))
                                {
                                    MatchInc();
                                    HandleMsg(msg);
                                }
                                if (CntIncThenContinue() == false)
                                {
                                    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;
                    }
                }
                //it is called in Write2File
                if (writer_ != null)
                {
                    writer_.Close();
                    writer_ = null;
                }
                ChangeStatusOnStop();
            });
        }