public void SendMessage(Producer producer, EQueueMessage message, object routingKey) { _ioHelper.TryIOAction(() => { var result = producer.Send(message, routingKey); if (result.SendStatus != SendStatus.Success) { throw new IOException(result.ErrorMessage); } }, "SendQueueMessage"); }
public void SendMessage(Producer producer, EQueueMessage message, string routingKey) { try { _ioHelper.TryIOAction(() => { var result = producer.Send(message, routingKey); if (result.SendStatus != SendStatus.Success) { _logger.ErrorFormat("EQueue message sync send failed, sendResult: {0}, routingKey: {1}", result, routingKey); throw new IOException(result.ErrorMessage); } _logger.InfoFormat("EQueue message sync send success, sendResult: {0}, routingKey: {1}", result, routingKey); }, "SendQueueMessage"); } catch (Exception ex) { _logger.Error(string.Format("EQueue message synch send has exception, message: {0}, routingKey: {1}", message, routingKey), ex); throw; } }
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; } } }); }