Ejemplo n.º 1
0
        public async Task <BatchSendResult> BatchSendAsync(IEnumerable <Message> messages, string routingKey, int timeoutMilliseconds = 30 * 1000)
        {
            Ensure.NotNull(messages, "messages");
            if (messages.Count() == 0)
            {
                throw new Exception("Batch message must contains at least one message.");
            }
            if (!_started)
            {
                throw new Exception("Producer not started, please start the producer first.");
            }

            var message    = messages.First();
            var sendResult = default(BatchSendResult);
            var retryCount = 0;

            while (retryCount <= Setting.SendMessageMaxRetryCount)
            {
                var messageQueueInfo = await TryGetAvailableMessageQueue(message, routingKey);

                if (messageQueueInfo == null)
                {
                    throw new Exception(string.Format("No available message queue for topic [{0}]", message.Topic));
                }

                var messageQueue     = messageQueueInfo.MessageQueue;
                var brokerConnection = messageQueueInfo.BrokerConnection;

                var remotingRequest = BuildBatchSendMessageRequest(messages, messageQueue.QueueId, brokerConnection);
                try
                {
                    var remotingResponse = await brokerConnection.RemotingClient.InvokeAsync(remotingRequest, timeoutMilliseconds).ConfigureAwait(false);

                    if (remotingResponse == null)
                    {
                        sendResult = new BatchSendResult(SendStatus.Timeout, null, string.Format("Batch send message timeout, queue: {0}, routingKey: {1}, timeoutMilliseconds: {2}, brokerInfo: {3}", messageQueue, routingKey, timeoutMilliseconds, brokerConnection.BrokerInfo));
                    }
                    return(ParseBatchSendResult(remotingResponse));
                }
                catch (Exception ex)
                {
                    sendResult = new BatchSendResult(SendStatus.Failed, null, ex.Message);
                }
                if (sendResult.SendStatus == SendStatus.Success)
                {
                    return(sendResult);
                }
                if (retryCount > 0)
                {
                    _logger.ErrorFormat("Batch send message failed, queue: {0}, routingKey: {1}, broker: {2}, sendResult: {3}, retryTimes: {4}", messageQueue, routingKey, brokerConnection.BrokerInfo, sendResult, retryCount);
                }
                else
                {
                    _logger.ErrorFormat("Batch send message failed, queue: {0}, routingKey: {1}, broker: {2}, sendResult: {3}", messageQueue, routingKey, brokerConnection.BrokerInfo, sendResult);
                }

                retryCount++;
            }
            return(sendResult);
        }
Ejemplo n.º 2
0
        public BatchSendResult BatchSend(IEnumerable <Message> messages, string routingKey, int timeoutMilliseconds = 30 * 1000)
        {
            if (!_started)
            {
                throw new Exception("Producer not started, please start the producer first.");
            }

            var sendResult = BatchSendAsync(messages, routingKey, timeoutMilliseconds).WaitResult(timeoutMilliseconds + 3000);

            if (sendResult == null)
            {
                sendResult = new BatchSendResult(SendStatus.Timeout, null, string.Format("Send message timeout, routingKey: {0}, timeoutMilliseconds: {1}", routingKey, timeoutMilliseconds));
            }
            return(sendResult);
        }