public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest) { var request = MessageUtils.DecodeSendMessageRequest(remotingRequest.Body); var message = request.Message; var queueId = request.QueueId; var queue = _queueStore.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)); }
public void OnComplete() { if (Success) { 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); } }
public void OnComplete() { var result = new MessageStoreResult( MessageLogRecord.MessageId, MessageLogRecord.Code, MessageLogRecord.Topic, MessageLogRecord.QueueId, MessageLogRecord.QueueOffset, 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); } }