Exemplo n.º 1
0
        public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
        {
            var request = MessageUtils.DecodeSendMessageRequest(remotingRequest.Body);
            var message = request.Message;
            var queueId = request.QueueId;
            var queue = _queueService.GetQueue(message.Topic, queueId);
            if (queue == null)
            {
                throw new QueueNotExistException(message.Topic, queueId);
            }

            //消息写文件需要加锁,确保顺序写文件
            MessageStoreResult result = null;
            lock (_syncObj)
            {
                var queueOffset = queue.NextOffset;
                var messageRecord = _messageStore.StoreMessage(queueId, queueOffset, message);
                queue.AddMessage(messageRecord.LogPosition);
                queue.IncrementNextOffset();
                result = new MessageStoreResult(messageRecord.MessageId, message.Code, message.Topic, queueId, queueOffset);
            }

            //如果需要立即通知所有消费者有新消息,则立即通知
            if (BrokerController.Instance.Setting.NotifyWhenMessageArrived)
            {
                _suspendedPullRequestManager.NotifyNewMessage(request.Message.Topic, result.QueueId, result.QueueOffset);
            }

            var data = MessageUtils.EncodeMessageStoreResult(result);
            return RemotingResponseFactory.CreateResponse(remotingRequest, data);
        }
Exemplo n.º 2
0
        public static byte[] EncodeMessageStoreResult(MessageStoreResult result)
        {
            //code
            var messageCodeBytes = BitConverter.GetBytes(result.Code);

            //queueId
            var queueIdBytes = BitConverter.GetBytes(result.QueueId);

            //queueOffset
            var queueOffsetBytes = BitConverter.GetBytes(result.QueueOffset);

            //messageId
            var messageIdBytes = Encoding.UTF8.GetBytes(result.MessageId);
            var messageIdLengthBytes = BitConverter.GetBytes(messageIdBytes.Length);

            //topic
            var topicBytes = Encoding.UTF8.GetBytes(result.Topic);
            var topicLengthBytes = BitConverter.GetBytes(topicBytes.Length);

            //tag
            var tagBytes = EmptyBytes;
            if (!string.IsNullOrEmpty(result.Tag))
            {
                tagBytes = Encoding.UTF8.GetBytes(result.Tag);
            }
            var tagLengthBytes = BitConverter.GetBytes(tagBytes.Length);

            //createdTimeTicks
            var createdTimeTicksBytes = BitConverter.GetBytes(result.CreatedTime.Ticks);
            //storedTimeTicks
            var storedTimeTicksBytes = BitConverter.GetBytes(result.StoredTime.Ticks);

            return Combine(
                messageCodeBytes,
                queueIdBytes,
                queueOffsetBytes,
                messageIdLengthBytes,
                messageIdBytes,
                topicLengthBytes,
                topicBytes,
                tagLengthBytes,
                tagBytes,
                createdTimeTicksBytes,
                storedTimeTicksBytes);
        }
Exemplo n.º 3
0
        public static byte[] EncodeMessageStoreResult(MessageStoreResult result)
        {
            //code
            var messageCodeBytes = BitConverter.GetBytes(result.Code);

            //queueId
            var queueIdBytes = BitConverter.GetBytes(result.QueueId);

            //queueOffset
            var queueOffsetBytes = BitConverter.GetBytes(result.QueueOffset);

            //messageId
            var messageIdBytes = Encoding.UTF8.GetBytes(result.MessageId);
            var messageIdLengthBytes = BitConverter.GetBytes(messageIdBytes.Length);

            //topic
            var topicBytes = Encoding.UTF8.GetBytes(result.Topic);
            var topicLengthBytes = BitConverter.GetBytes(topicBytes.Length);

            return Combine(
                messageCodeBytes,
                queueIdBytes,
                queueOffsetBytes,
                messageIdLengthBytes,
                messageIdBytes,
                topicLengthBytes,
                topicBytes);
        }
Exemplo n.º 4
0
            public void OnComplete()
            {
                if (MessageLogRecord.LogPosition >= 0 && !string.IsNullOrEmpty(MessageLogRecord.MessageId))
                {
                    var result = new MessageStoreResult(
                        MessageLogRecord.MessageId,
                        MessageLogRecord.Code,
                        MessageLogRecord.Topic,
                        MessageLogRecord.QueueId,
                        MessageLogRecord.QueueOffset,
                        MessageLogRecord.CreatedTime,
                        MessageLogRecord.StoredTime,
                        MessageLogRecord.Tag);
                    var data = MessageUtils.EncodeMessageStoreResult(result);
                    var response = RemotingResponseFactory.CreateResponse(RemotingRequest, data);

                    RequestHandlerContext.SendRemotingResponse(response);

                    if (SendMessageRequestHandler._notifyWhenMessageArrived)
                    {
                        SendMessageRequestHandler._suspendedPullRequestManager.NotifyNewMessage(MessageLogRecord.Topic, result.QueueId, result.QueueOffset);
                    }

                    SendMessageRequestHandler._brokerController.AddLatestMessage(result.MessageId, result.CreatedTime, result.StoredTime);
                }
                else
                {
                    var response = RemotingResponseFactory.CreateResponse(RemotingRequest, ResponseCode.Failed, Encoding.UTF8.GetBytes(SendMessageFailedText));
                    RequestHandlerContext.SendRemotingResponse(response);
                }
            }
Exemplo n.º 5
0
 public SendResult(SendStatus sendStatus, MessageStoreResult messageStoreResult, string errorMessage)
 {
     SendStatus = sendStatus;
     MessageStoreResult = messageStoreResult;
     ErrorMessage = errorMessage;
 }