Ejemplo n.º 1
0
        static void SendMessages(Producer producer, string mode, int messageCount, Message message)
        {
            _logger.Info("----Send message starting----");

            var sendAction = default(Action<int>);

            if (_mode == "Oneway")
            {
                sendAction = index =>
                {
                    producer.SendOneway(message, index.ToString());
                    Interlocked.Increment(ref _sentCount);
                };
            }
            else if (_mode == "Sync")
            {
                sendAction = index =>
                {
                    var result = producer.Send(message, index.ToString());
                    if (result.SendStatus != SendStatus.Success)
                    {
                        throw new Exception(result.ErrorMessage);
                    }
                    Interlocked.Increment(ref _sentCount);
                };
            }
            else if (_mode == "Async")
            {
                sendAction = index => producer.SendAsync(message, index.ToString()).ContinueWith(t =>
                {
                    if (t.Exception != null)
                    {
                        _hasError = true;
                        _logger.ErrorFormat("Send message has exception, errorMessage: {0}", t.Exception.GetBaseException().Message);
                        return;
                    }
                    if (t.Result == null)
                    {
                        _hasError = true;
                        _logger.Error("Send message timeout.");
                        return;
                    }
                    if (t.Result.SendStatus != SendStatus.Success)
                    {
                        _hasError = true;
                        _logger.ErrorFormat("Send message failed, errorMessage: {0}", t.Result.ErrorMessage);
                        return;
                    }

                    Interlocked.Increment(ref _sentCount);
                });
            }
            else if (_mode == "Callback")
            {
                producer.RegisterResponseHandler(new ResponseHandler());
                sendAction = index => producer.SendWithCallback(message, index.ToString());
            }

            Task.Factory.StartNew(() =>
            {
                for (var i = 0; i < messageCount; i++)
                {
                    try
                    {
                        sendAction(i);
                    }
                    catch (Exception ex)
                    {
                        _hasError = true;
                        _logger.ErrorFormat("Send message failed, errorMsg:{0}", ex.Message);
                    }

                    if (_hasError)
                    {
                        Thread.Sleep(3000);
                        _hasError = false;
                    }
                }
            });
        }
Ejemplo n.º 2
0
        static void SendMessages(Producer producer, string mode, int count, int sleepMilliseconds, int batchSize, Message message)
        {
            _logger.InfoFormat("----Send message starting, producerId:{0}----", producer.Id);

            if (mode == "Oneway")
            {
                for (var i = 1; i <= count; i++)
                {
                    TryAction(() => producer.SendOneway(message, message.Key));
                    var current = Interlocked.Increment(ref _sendingCount);
                    if (current % 10000 == 0)
                    {
                        _logger.InfoFormat("Sening {0} messages, timeSpent: {1}ms, throughput: {2}/s", current, _watch.ElapsedMilliseconds, current * 1000 / _watch.ElapsedMilliseconds);
                    }
                    WaitIfNecessory(current, batchSize, sleepMilliseconds);
                }
            }
            else if (mode == "Async")
            {
                for (var i = 1; i <= count; i++)
                {
                    TryAction(() => producer.SendAsync(message, message.Key, 100000).ContinueWith(SendCallback));
                    var current = Interlocked.Increment(ref _sendingCount);
                    WaitIfNecessory(current, batchSize, sleepMilliseconds);
                }
            }
            else if (mode == "Callback")
            {
                producer.RegisterResponseHandler(new ResponseHandler());
                for (var i = 1; i <= count; i++)
                {
                    TryAction(() => producer.SendWithCallback(message, message.Key));
                    var current = Interlocked.Increment(ref _sendingCount);
                    WaitIfNecessory(current, batchSize, sleepMilliseconds);
                }
            }
        }