Ejemplo n.º 1
0
 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;
 }
Ejemplo n.º 2
0
 public QueueMessage StoreMessage(int queueId, long queueOffset, Message message)
 {
     var nextOffset = GetNextOffset();
     var queueMessage = new QueueMessage(message.Topic, message.Body, nextOffset, queueId, queueOffset, DateTime.Now);
     _messageDict[nextOffset] = queueMessage;
     return queueMessage;
 }
Ejemplo n.º 3
0
        public async Task<SendResult> SendAsync(Message message, string routingKey, int timeoutMilliseconds = 120000)
        {
            Ensure.NotNull(message, "message");

            var queueId = GetAvailableQueueId(message, routingKey);
            if (queueId < 0)
            {
                throw new Exception(string.Format("No available routing queue for topic [{0}].", message.Topic));
            }
            var remotingRequest = BuildSendMessageRequest(message, queueId);

            try
            {
                var remotingResponse = await _remotingClient.InvokeAsync(remotingRequest, timeoutMilliseconds).ConfigureAwait(false);

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

                return ParseSendResult(remotingResponse);
            }
            catch (Exception ex)
            {
                return new SendResult(SendStatus.Failed, null, ex.Message);
            }
        }
Ejemplo n.º 4
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"];
            var message = new Message(topic, 100, payload);

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

            Task.Factory.StartNew(() => Parallel.Invoke(actions.ToArray()));
        }
Ejemplo n.º 5
0
        static void WriteMessagePerfTest()
        {
            var threadCount = int.Parse(ConfigurationManager.AppSettings["concurrentThreadCount"]);                         //并行写消息的线程数
            var messageSize = int.Parse(ConfigurationManager.AppSettings["messageSize"]);                                   //消息大小,字节为单位
            var messageCount = int.Parse(ConfigurationManager.AppSettings["messageCount"]);                                 //总共要写入的消息数
            var payload = new byte[messageSize];
            var message = new Message("topic1", 100, payload);
            var queue = new Queue(message.Topic, 1);
            var count = 0L;

            _watch = Stopwatch.StartNew();
            StartPrintThroughputTask();

            for (var i = 0; i < threadCount; i++)
            {
                Task.Factory.StartNew(() =>
                {
                    while (true)
                    {
                        var current = Interlocked.Increment(ref count);
                        if (current > messageCount)
                        {
                            break;
                        }
                        _messageStore.StoreMessageAsync(queue, message, (x, y) =>
                        {
                            Interlocked.Increment(ref _currentCount);
                        }, null);
                    }
                });
            }
        }
Ejemplo n.º 6
0
        public void SendAsync(CommandExecutedMessage message, string topic)
        {
            var messageJson   = _jsonSerializer.Serialize(message);
            var messageBytes  = Encoding.UTF8.GetBytes(messageJson);
            var equeueMessage = new EQueueMessage(topic, (int)EQueueMessageTypeCode.CommandExecutedMessage, messageBytes);

            SendMessageAsync(equeueMessage, messageJson, message.CommandId, 0);
        }
Ejemplo n.º 7
0
 public int SelectQueueId(IList<int> availableQueueIds, Message message, string routingKey)
 {
     if (availableQueueIds.Count == 0)
     {
         return -1;
     }
     return availableQueueIds[(int)(Interlocked.Increment(ref _index) % availableQueueIds.Count)];
 }
Ejemplo n.º 8
0
 public int SelectQueueId(IList<int> availableQueueIds, Message message, object arg)
 {
     if (availableQueueIds.Count == 0)
     {
         throw new Exception(string.Format("No available queue for topic [{0}].", message.Topic));
     }
     return availableQueueIds[(int)(Interlocked.Increment(ref _index) % availableQueueIds.Count)];
 }
Ejemplo n.º 9
0
 public SendResult Send(Message message, object arg)
 {
     var queueIds = GetTopicQueueIds(message.Topic);
     var queueId = _queueSelector.SelectQueueId(queueIds, message, arg);
     var remotingRequest = BuildSendMessageRequest(message, queueId);
     var remotingResponse = _remotingClient.InvokeSync(remotingRequest, Setting.SendMessageTimeoutMilliseconds);
     var response = _binarySerializer.Deserialize<SendMessageResponse>(remotingResponse.Body);
     return new SendResult(SendStatus.Success, response.MessageOffset, response.MessageQueue, response.QueueOffset);
 }
Ejemplo n.º 10
0
 public SendResult Send(Message message, object routingKey, int timeoutMilliseconds = 30000)
 {
     var sendResult = SendAsync(message, routingKey, timeoutMilliseconds).WaitResult<SendResult>(timeoutMilliseconds + 1000);
     if (sendResult == null)
     {
         sendResult = new SendResult(SendStatus.Timeout, string.Format("Send message timeout, message: {0}, routingKey: {1}, timeoutMilliseconds: {2}", message, routingKey, timeoutMilliseconds));
     }
     return sendResult;
 }
Ejemplo n.º 11
0
 public int SelectQueueId(int totalQueueCount, Message message, object arg)
 {
     var value = arg.GetHashCode();
     if (value < 0)
     {
         value = Math.Abs(value);
     }
     return value % totalQueueCount;
 }
Ejemplo n.º 12
0
 private void SendMessageAsync(EQueueMessage message, string messageJson, string routingKey, int retryTimes)
 {
     _ioHelper.TryAsyncActionRecursively <AsyncTaskResult>("PublishQueueMessageAsync",
                                                           () => _sendMessageService.SendMessageAsync(_producer, message, routingKey),
                                                           currentRetryTimes => SendMessageAsync(message, messageJson, routingKey, currentRetryTimes),
                                                           null,
                                                           () => string.Format("[message:{0}]", messageJson),
                                                           null,
                                                           retryTimes);
 }
Ejemplo n.º 13
0
 public MessageReply(string topic, string messageID, object result)
     : this()
 {
     if (result != null)
     {
         Message = new EQueueProtocols.Message(topic, Encoding.UTF8.GetBytes(this.ToJson()));
     }
     MessageID = messageID;
     Result    = result;
 }
Ejemplo n.º 14
0
 public MessageReply(string topic, string messageID, object result)
     : this()
 {
     if (result != null)
     {
         Message = new EQueueProtocols.Message(topic, Encoding.UTF8.GetBytes(this.ToJson()));
     }
     MessageID = messageID;
     Result = result;
 }
Ejemplo n.º 15
0
 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 Send(DomainEventHandledMessage message, string topic)
        {
            var messageJson   = _jsonSerializer.Serialize(message);
            var messageBytes  = Encoding.UTF8.GetBytes(messageJson);
            var equeueMessage = new EQueueMessage(topic, (int)EQueueMessageTypeCode.DomainEventHandledMessage, messageBytes);

            _ioHelper.TryIOActionRecursively("DomainEventHandledMessage", () => messageJson, () =>
            {
                _ioHelper.TryIOAction(() => _producer.SendAsync(equeueMessage, message.CommandId), "DomainEventHandledMessageAsync");
            });
        }
Ejemplo n.º 17
0
 public MessageLogRecord StoreMessage(int queueId, long queueOffset, Message message)
 {
     var record = new MessageLogRecord(
         message.Topic,
         message.Code,
         message.Body,
         queueId,
         queueOffset,
         message.CreatedTime,
         DateTime.Now,
         message.Tag);
     _chunkWriter.Write(record);
     return record;
 }
Ejemplo n.º 18
0
 public SendResult Send(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 remotingResponse = _remotingClient.InvokeSync(remotingRequest, Setting.SendMessageTimeoutMilliseconds);
     var response = _binarySerializer.Deserialize<SendMessageResponse>(remotingResponse.Body);
     var sendStatus = SendStatus.Success; //TODO, figure from remotingResponse.Code;
     return new SendResult(sendStatus, response.MessageOffset, response.MessageQueue, response.QueueOffset);
 }
Ejemplo n.º 19
0
 public MessageStoreResult StoreMessage(Message message, int queueId)
 {
     var queues = GetQueues(message.Topic);
     var queueCount = queues.Count;
     if (queueId >= queueCount || queueId < 0)
     {
         throw new InvalidQueueIdException(message.Topic, queueCount, queueId);
     }
     var queue = queues[queueId];
     var queueOffset = queue.IncrementCurrentOffset();
     var storeResult = _messageStore.StoreMessage(message, queue.QueueId, queueOffset);
     queue.SetMessageOffset(queueOffset, storeResult.MessageOffset);
     return storeResult;
 }
Ejemplo n.º 20
0
        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;
        }
Ejemplo n.º 21
0
 public MessageStoreResult StoreMessage(Message message, int queueId, string routingKey)
 {
     var queue = _queueService.GetQueue(message.Topic, queueId);
     if (queue == null)
     {
         throw new Exception(string.Format("Queue not exist, topic: {0}, queueId: {1}", message.Topic, queueId));
     }
     lock (_syncObj)
     {
         var messageOffset = _messageStore.GetNextMessageOffset();
         var queueOffset = queue.IncrementCurrentOffset();
         var queueMessage = _messageStore.StoreMessage(queueId, messageOffset, queueOffset, message, routingKey);
         queue.SetQueueIndex(queueMessage.QueueOffset, queueMessage.MessageOffset);
         return new MessageStoreResult(queueMessage.MessageId, queueMessage.MessageOffset, queueMessage.QueueId, queueMessage.QueueOffset);
     }
 }
Ejemplo n.º 22
0
        public async Task <AsyncTaskResult> SendMessageAsync(Producer producer, EQueueMessage message, object routingKey)
        {
            try
            {
                var result = await producer.SendAsync(message, routingKey);

                if (result.SendStatus != SendStatus.Success)
                {
                    return(new AsyncTaskResult(AsyncTaskStatus.IOException, result.ErrorMessage));
                }
                return(AsyncTaskResult.Success);
            }
            catch (Exception ex)
            {
                return(new AsyncTaskResult(AsyncTaskStatus.IOException, ex.Message));
            }
        }
Ejemplo n.º 23
0
 public MessageStoreResult StoreMessage(Message message, int queueId)
 {
     var queues = GetQueues(message.Topic);
     if (queues.Count == 0)
     {
         throw new Exception(string.Format("No available queue for storing message. topic:{0}", message.Topic));
     }
     var queue = queues.SingleOrDefault(x => x.QueueId == queueId);
     if (queue == null)
     {
         throw new InvalidQueueIdException(message.Topic, queues.Select(x => x.QueueId), queueId);
     }
     var queueOffset = queue.IncrementCurrentOffset();
     var queueMessage = _messageStore.StoreMessage(queueId, queueOffset, message);
     queue.SetQueueIndex(queueMessage.QueueOffset, queueMessage.MessageOffset);
     return new MessageStoreResult(queueMessage.MessageOffset, queueMessage.QueueId, queueMessage.QueueOffset);
 }
Ejemplo n.º 24
0
 public MessageQueue SelectMessageQueue(IList<MessageQueue> availableMessageQueues, Message message, string routingKey)
 {
     if (availableMessageQueues.Count == 0)
     {
         return null;
     }
     unchecked
     {
         int hash = 23;
         foreach (char c in routingKey)
         {
             hash = (hash << 5) - hash + c;
         }
         if (hash < 0)
         {
             hash = Math.Abs(hash);
         }
         return availableMessageQueues[hash % availableMessageQueues.Count];
     }
 }
Ejemplo n.º 25
0
 public int SelectQueueId(IList<int> availableQueueIds, Message message, string routingKey)
 {
     if (availableQueueIds.Count == 0)
     {
         return -1;
     }
     unchecked
     {
         int hash = 23;
         foreach (char c in routingKey)
         {
             hash = (hash << 5) - hash + c;
         }
         if (hash < 0)
         {
             hash = Math.Abs(hash);
         }
         return availableQueueIds[(int)(hash % availableQueueIds.Count)];
     }
 }
Ejemplo n.º 26
0
        static void WriteMessagePerfTest()
        {
            var threadCount = int.Parse(ConfigurationManager.AppSettings["concurrentThreadCount"]);     //并行写消息的线程数
            var messageSize = int.Parse(ConfigurationManager.AppSettings["messageSize"]);               //消息大小,字节为单位
            var messageCount = int.Parse(ConfigurationManager.AppSettings["messageCount"]);             //总共要写入的消息数
            var payload = new byte[messageSize];
            var message = new Message("topic1", 100, payload);

            for (var i = 0; i < threadCount; i++)
            {
                Task.Factory.StartNew(() =>
                {
                    for (var j = 0; j < messageCount; j++)
                    {
                        _messageStore.StoreMessage(1, 1000, message);
                        Interlocked.Increment(ref _currentCount);
                    }
                });
            }
        }
Ejemplo n.º 27
0
 public int SelectQueueId(IList<int> availableQueueIds, Message message, object arg)
 {
     if (availableQueueIds.Count == 0)
     {
         throw new Exception(string.Format("No available queue for topic [{0}].", message.Topic));
     }
     unchecked
     {
         int hash = 23;
         foreach (char c in arg.ToString())
         {
             hash = (hash << 5) - hash + c;
         }
         if (hash < 0)
         {
             hash = Math.Abs(hash);
         }
         return availableQueueIds[(int)(hash % availableQueueIds.Count)];
     }
 }
Ejemplo n.º 28
0
        public async Task<SendResult> SendAsync(Message message, object routingKey, int timeoutMilliseconds = 30000)
        {
            Ensure.NotNull(message, "message");

            var currentRoutingKey = GetStringRoutingKey(routingKey);
            var queueIds = GetTopicQueueIds(message.Topic);
            var queueId = _queueSelector.SelectQueueId(queueIds, message, currentRoutingKey);
            if (queueId < 0)
            {
                throw new Exception(string.Format("No available routing queue for topic [{0}].", message.Topic));
            }
            var remotingRequest = BuildSendMessageRequest(message, queueId, currentRoutingKey);

            try
            {
                var remotingResponse = await _remotingClient.InvokeAsync(remotingRequest, timeoutMilliseconds).ConfigureAwait(false);

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

                if (remotingResponse.Code == (int)ResponseCode.Success)
                {
                    var response = Encoding.UTF8.GetString(remotingResponse.Body).Split(':');
                    return new SendResult(SendStatus.Success, response[2], long.Parse(response[0]), new MessageQueue(message.Topic, queueId), long.Parse(response[1]));
                }
                else
                {
                    return new SendResult(SendStatus.Failed, Encoding.UTF8.GetString(remotingResponse.Body));
                }
            }
            catch (Exception ex)
            {
                return new SendResult(SendStatus.Failed, ex.Message);
            }
        }
Ejemplo n.º 29
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);
                }
            }
        }
Ejemplo n.º 30
0
        static Task SendMessageAsync(Producer producer, Message message, string routingKey)
        {
            return producer.SendAsync(message, routingKey).ContinueWith(t =>
            {
                if (t.Exception != null)
                {
                    _logger.ErrorFormat("Send message has exception, errorMessage: {0}", t.Exception.GetBaseException().Message);
                    return;
                }
                if (t.Result == null)
                {
                    _logger.Error("Send message timeout.");
                    return;
                }
                if (t.Result.SendStatus != SendStatus.Success)
                {
                    _logger.ErrorFormat("Send message failed, errorMessage: {0}", t.Result.ErrorMessage);
                    return;
                }

                _sendResultLogger.InfoFormat("Message send success, routingKey: {0}, sendResult: {1}", routingKey, t.Result);
                Interlocked.Increment(ref _sentCount);
            });
        }
Ejemplo n.º 31
0
 private int GetAvailableQueueId(Message message, string routingKey)
 {
     var queueIds = GetTopicQueueIds(message.Topic);
     if (queueIds.IsEmpty())
     {
         return -1;
     }
     return _queueSelector.SelectQueueId(queueIds, message, routingKey);
 }
Ejemplo n.º 32
0
 public int SelectQueueId(int totalQueueCount, Message message, object arg)
 {
     return (int)(Interlocked.Increment(ref _index) % totalQueueCount);
 }
Ejemplo n.º 33
0
 private RemotingRequest BuildSendMessageRequest(Message message, int queueId, object routingKey)
 {
     var request = new SendMessageRequest { Message = message, QueueId = queueId, RoutingKey = routingKey.ToString() };
     var data = MessageUtils.EncodeSendMessageRequest(request);
     return new RemotingRequest((int)RequestCode.SendMessage, data);
 }
Ejemplo n.º 34
0
 public QueueMessage StoreMessage(int queueId, long messageOffset, long queueOffset, Message message, string routingKey)
 {
     var queueMessage = new QueueMessage(
         ObjectId.GenerateNewStringId(),
         message.Topic,
         message.Code,
         message.Body,
         messageOffset,
         queueId,
         queueOffset,
         message.CreatedTime,
         DateTime.Now,
         DateTime.Now,
         routingKey);
     _messageDict[messageOffset] = queueMessage;
     return queueMessage;
 }
Ejemplo n.º 35
0
 private RemotingRequest BuildSendMessageRequest(Message message, int queueId)
 {
     var request = new SendMessageRequest { Message = message, QueueId = queueId };
     var data = MessageUtils.EncodeSendMessageRequest(request);
     return new RemotingRequest((int)RequestCode.SendMessage, data);
 }
Ejemplo n.º 36
0
 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;
 }