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 RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest) { var request = MessageUtils.DecodeSendMessageRequest(remotingRequest.Body); var storeResult = _messageService.StoreMessage(request.Message, request.QueueId, request.RoutingKey); _suspendedPullRequestManager.NotifyNewMessage(request.Message.Topic, storeResult.QueueId, storeResult.QueueOffset); var responseData = Encoding.UTF8.GetBytes(string.Format("{0}:{1}:{2}", storeResult.MessageOffset, storeResult.QueueOffset, storeResult.MessageId)); return new RemotingResponse((int)ResponseCode.Success, remotingRequest.Sequence, responseData); }
public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest) { if (remotingRequest.Body.Length > _brokerController.Setting.MessageMaxSize) { throw new Exception("Message size cannot exceed max message size:" + _brokerController.Setting.MessageMaxSize); } if (BrokerController.Instance.IsCleaning) { throw new BrokerCleanningException(); } 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); } _messageStore.StoreMessageAsync(queue, message, (record, parameter) => { var storeContext = parameter as StoreContext; if (record.LogPosition >= 0 && !string.IsNullOrEmpty(record.MessageId)) { storeContext.Queue.AddMessage(record.LogPosition, record.Tag); storeContext.MessageLogRecord = record; storeContext.Success = true; } else { storeContext.Success = false; } _bufferQueue.EnqueueMessage(storeContext); }, new StoreContext { RequestHandlerContext = context, RemotingRequest = remotingRequest, Queue = queue, SendMessageRequestHandler = this }, request.ProducerAddress); _tpsStatisticService.AddTopicSendCount(message.Topic, queueId); return(null); }
public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest request) { var sendMessageRequest = MessageUtils.DecodeSendMessageRequest(request.Body); var storeResult = _messageService.StoreMessage(sendMessageRequest.Message, sendMessageRequest.QueueId); if (_brokerController.Setting.NotifyWhenMessageArrived) { _brokerController.SuspendedPullRequestManager.NotifyMessageArrived(sendMessageRequest.Message.Topic, storeResult.QueueId, storeResult.QueueOffset); } var sendMessageResponse = new SendMessageResponse( storeResult.MessageOffset, new MessageQueue(sendMessageRequest.Message.Topic, storeResult.QueueId), storeResult.QueueOffset); var responseData = _binarySerializer.Serialize(sendMessageResponse); return(new RemotingResponse((int)ResponseCode.Success, request.Sequence, responseData)); }
public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest) { var request = MessageUtils.DecodeSendMessageRequest(remotingRequest.Body); var storeResult = _messageService.StoreMessage(request.Message, request.QueueId, request.RoutingKey); _suspendedPullRequestManager.NotifyNewMessage(request.Message.Topic, storeResult.QueueId, storeResult.QueueOffset); var data = MessageUtils.EncodeMessageSendResponse(new SendMessageResponse( request.Message.Key, storeResult.MessageId, storeResult.MessageOffset, request.Message.Code, request.Message.Topic, request.QueueId, storeResult.QueueOffset)); return(RemotingResponseFactory.CreateResponse(remotingRequest, data)); }
public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest) { if (remotingRequest.Body.Length > _brokerController.Setting.MessageMaxSize) { throw new Exception("Message size cannot exceed max message size:" + _brokerController.Setting.MessageMaxSize); } if (BrokerController.Instance.IsCleaning) { throw new BrokerCleanningException(); } 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); } _messageStore.StoreMessageAsync(queue, message, (record, parameter) => { var storeContext = parameter as StoreContext; storeContext.MessageLogRecord = record; _bufferQueue.EnqueueMessage(storeContext); }, new StoreContext { RequestHandlerContext = context, RemotingRequest = remotingRequest, Queue = queue, SendMessageRequestHandler = this }); return(null); }