public Task<SendResult> SendAsync(Message message, object arg) { var queueCount = GetTopicQueueCount(message.Topic); if (queueCount == 0) { throw new Exception(string.Format("No available queue for topic [{0}], producerId:{1}.", message.Topic, Id)); } var queueId = _queueSelector.SelectQueueId(queueCount, message, arg); var remotingRequest = BuildSendMessageRequest(message, queueId); var taskCompletionSource = new TaskCompletionSource<SendResult>(); _remotingClient.InvokeAsync(remotingRequest, Setting.SendMessageTimeoutMilliseconds).ContinueWith((requestTask) => { var remotingResponse = requestTask.Result; if (remotingResponse != null) { var response = _binarySerializer.Deserialize<SendMessageResponse>(remotingResponse.Body); var sendStatus = SendStatus.Success; //TODO, figure from remotingResponse.Code; var result = new SendResult(sendStatus, response.MessageOffset, response.MessageQueue, response.QueueOffset); taskCompletionSource.SetResult(result); } else { var result = new SendResult(SendStatus.Failed, "Send message request failed or wait for response timeout."); taskCompletionSource.SetResult(result); } }); return taskCompletionSource.Task; }
public SendResult Send(Message message, string routingKey, int timeoutMilliseconds = 30 * 1000) { if (!_started) { throw new Exception("Producer not started, please start the producer first."); } var sendResult = SendAsync(message, routingKey, timeoutMilliseconds).WaitResult(timeoutMilliseconds + 3000); if (sendResult == null) { sendResult = new SendResult(SendStatus.Timeout, null, string.Format("Send message timeout, message: {0}, routingKey: {1}, timeoutMilliseconds: {2}", message, routingKey, timeoutMilliseconds)); } return sendResult; }
public async Task<SendResult> SendAsync(Message message, string routingKey, int timeoutMilliseconds = 30 * 1000) { Ensure.NotNull(message, "message"); if (!_started) { throw new Exception("Producer not started, please start the producer first."); } var sendResult = default(SendResult); var retryCount = 0; while (retryCount <= Setting.SendMessageMaxRetryCount) { MessageQueue messageQueue; BrokerConnection brokerConnection; if (!TryGetAvailableMessageQueue(message, routingKey, out messageQueue, out brokerConnection)) { throw new Exception(string.Format("No available message queue for topic [{0}]", message.Topic)); } var remotingRequest = BuildSendMessageRequest(message, messageQueue.QueueId, brokerConnection); try { var remotingResponse = await brokerConnection.RemotingClient.InvokeAsync(remotingRequest, timeoutMilliseconds).ConfigureAwait(false); if (remotingResponse == null) { sendResult = new SendResult(SendStatus.Timeout, null, string.Format("Send message timeout, message: {0}, routingKey: {1}, timeoutMilliseconds: {2}, brokerInfo: {3}", message, routingKey, timeoutMilliseconds, brokerConnection.BrokerInfo)); } return ParseSendResult(remotingResponse); } catch (Exception ex) { sendResult = new SendResult(SendStatus.Failed, null, ex.Message); } if (sendResult.SendStatus == SendStatus.Success) { return sendResult; } if (retryCount > 0) { _logger.ErrorFormat("Send message failed, queue: {0}, broker: {1}, sendResult: {2}, retryTimes: {3}", messageQueue, brokerConnection.BrokerInfo, sendResult, retryCount); } else { _logger.ErrorFormat("Send message failed, queue: {0}, broker: {1}, sendResult: {2}", messageQueue, brokerConnection.BrokerInfo, sendResult); } retryCount++; } return sendResult; }
public Task<SendResult> SendAsync(Message message, object arg) { var queueIds = GetTopicQueueIds(message.Topic); var queueId = _queueSelector.SelectQueueId(queueIds, message, arg); var remotingRequest = BuildSendMessageRequest(message, queueId); var taskCompletionSource = new TaskCompletionSource<SendResult>(); _remotingClient.InvokeAsync(remotingRequest, Setting.SendMessageTimeoutMilliseconds).ContinueWith((requestTask) => { var remotingResponse = requestTask.Result; if (remotingResponse != null) { var response = _binarySerializer.Deserialize<SendMessageResponse>(remotingResponse.Body); var result = new SendResult(SendStatus.Success, response.MessageOffset, response.MessageQueue, response.QueueOffset); taskCompletionSource.SetResult(result); } else { var errorMessage = "Unknown error occurred when sending message to broker."; if (!requestTask.IsCompleted) { errorMessage = "Send message to broker timeout."; } else if (requestTask.IsFaulted) { errorMessage = requestTask.Exception.Message; } taskCompletionSource.SetResult(new SendResult(SendStatus.Failed, errorMessage)); } }); return taskCompletionSource.Task; }