Пример #1
0
        static void SendMessageTest()
        {
            _mode = ConfigurationManager.AppSettings["Mode"];

            var address       = ConfigurationManager.AppSettings["BrokerAddress"];
            var brokerAddress = string.IsNullOrEmpty(address) ? SocketUtils.GetLocalIPV4() : IPAddress.Parse(address);
            var clientCount   = int.Parse(ConfigurationManager.AppSettings["ClientCount"]);
            var messageSize   = int.Parse(ConfigurationManager.AppSettings["MessageSize"]);
            var messageCount  = int.Parse(ConfigurationManager.AppSettings["MessageCount"]);
            var actions       = new List <Action>();
            var payload       = new byte[messageSize];
            var topic         = ConfigurationManager.AppSettings["Topic"];

            for (var i = 0; i < clientCount; i++)
            {
                var setting = new ProducerSetting
                {
                    BrokerAddress      = new IPEndPoint(brokerAddress, 5000),
                    BrokerAdminAddress = new IPEndPoint(brokerAddress, 5002)
                };
                var producer = new BodeAbp.Queue.Clients.Producers.Producer(setting).Start();
                actions.Add(() => SendMessages(producer, _mode, messageCount, topic, payload));
            }

            Task.Factory.StartNew(() => Parallel.Invoke(actions.ToArray()));
        }
Пример #2
0
        static void SendMessages(BodeAbp.Queue.Clients.Producers.Producer producer, string mode, int messageCount, string topic, byte[] payload)
        {
            _logger.Info("----Send message starting----");

            var sendAction = default(Action <int>);

            if (_mode == "Oneway")
            {
                sendAction = index =>
                {
                    var message = new Message(topic, 100, payload);
                    producer.SendOneway(message, index.ToString());
                    _rtStatisticService.AddRT((DateTime.Now - message.CreatedTime).TotalMilliseconds);
                    Interlocked.Increment(ref _sentCount);
                };
            }
            else if (_mode == "Sync")
            {
                sendAction = index =>
                {
                    var message = new Message(topic, 100, payload);
                    var result  = producer.Send(message, index.ToString());
                    if (result.SendStatus != SendStatus.Success)
                    {
                        throw new Exception(result.ErrorMessage);
                    }
                    _rtStatisticService.AddRT((DateTime.Now - message.CreatedTime).TotalMilliseconds);
                    Interlocked.Increment(ref _sentCount);
                };
            }
            else if (_mode == "Async")
            {
                sendAction = index =>
                {
                    var message = new Message(topic, 100, payload);
                    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;
                        }
                        _rtStatisticService.AddRT((DateTime.Now - message.CreatedTime).TotalMilliseconds);
                        Interlocked.Increment(ref _sentCount);
                    });
                };
            }
            else if (_mode == "Callback")
            {
                producer.RegisterResponseHandler(new ResponseHandler());
                sendAction = index =>
                {
                    var message = new Message(topic, 100, payload);
                    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;
                    }
                }
            });
        }