public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
 {
     var topic = Encoding.UTF8.GetString(remotingRequest.Body);
     var queueIds = _queueService.GetQueues(topic).Select(x => x.QueueId).ToList();
     var data = Encoding.UTF8.GetBytes(string.Join(",", queueIds));
     return RemotingResponseFactory.CreateResponse(remotingRequest, data);
 }
Esempio n. 2
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);
        }
Esempio n. 3
0
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
 {
     var current = Interlocked.Increment(ref totalHandled);
     if (current == 1)
     {
         watch = Stopwatch.StartNew();
     }
     if (storeOption == "UnManagedMemory")
     {
         SaveMessage(remotingRequest.Body);
     }
     else if (storeOption == "ManagedMemory")
     {
         messageDictionary[remotingRequest.Sequence] = remotingRequest.Body;
     }
     else if (storeOption == "OnlyMapping")
     {
         messageDictionary[remotingRequest.Sequence] = remotingRequest.Sequence;
     }
     if (current % 10000 == 0)
     {
         Console.WriteLine("Handled request, size:{0}, count:{1}, timeSpent: {2}ms", remotingRequest.Body.Length, current, watch.ElapsedMilliseconds);
     }
     return new RemotingResponse(remotingRequest.Code, 10, remotingRequest.Type, response, remotingRequest.Sequence);
 }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest request)
 {
     var pullMessageRequest = _binarySerializer.Deserialize<PullMessageRequest>(request.Body);
     var messages = _messageService.GetMessages(
         pullMessageRequest.MessageQueue.Topic,
         pullMessageRequest.MessageQueue.QueueId,
         pullMessageRequest.QueueOffset,
         pullMessageRequest.PullMessageBatchSize);
     if (messages.Count() > 0)
     {
         var pullMessageResponse = new PullMessageResponse(messages);
         var responseData = _binarySerializer.Serialize(pullMessageResponse);
         return new RemotingResponse((int)PullStatus.Found, request.Sequence, responseData);
     }
     else
     {
         var pullRequest = new PullRequest(
             request.Sequence,
             pullMessageRequest,
             context,
             DateTime.Now,
             SuspendPullRequestMilliseconds,
             ExecutePullRequest,
             ExecutePullRequest,
             ExecuteReplacedPullRequest);
         _brokerController.SuspendedPullRequestManager.SuspendPullRequest(pullRequest);
         return null;
     }
 }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest request)
 {
     var topic = Encoding.UTF8.GetString(request.Body);
     var queueIds = _messageService.GetQueueIdsForConsumer(topic);
     var data = Encoding.UTF8.GetBytes(string.Join(",", queueIds));
     return new RemotingResponse((int)ResponseCode.Success, request.Sequence, data);
 }
        public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest request)
        {
            var queryTopicConsumeInfoRequest = _binarySerializer.Deserialize<QueryTopicQueueInfoRequest>(request.Body);
            var topicQueueInfoList = new List<TopicQueueInfo>();
            var topicList = !string.IsNullOrEmpty(queryTopicConsumeInfoRequest.Topic) ? new List<string> { queryTopicConsumeInfoRequest.Topic } : _messageService.GetAllTopics().ToList();

            foreach (var topic in topicList)
            {
                var queues = _messageService.QueryQueues(topic).ToList();
                foreach (var queue in queues)
                {
                    var queueMinOffset = queue.GetMinQueueOffset();
                    var topicQueueInfo = new TopicQueueInfo();
                    topicQueueInfo.Topic = queue.Topic;
                    topicQueueInfo.QueueId = queue.QueueId;
                    topicQueueInfo.QueueCurrentOffset = queue.CurrentOffset;
                    topicQueueInfo.QueueMinOffset = queueMinOffset != null ? queueMinOffset.Value : -1L;
                    topicQueueInfo.QueueMessageCount = queue.GetMessageRealCount();
                    topicQueueInfo.QueueMaxConsumedOffset = _offsetManager.GetMinOffset(queue.Topic, queue.QueueId);
                    topicQueueInfo.Status = queue.Status;
                    topicQueueInfoList.Add(topicQueueInfo);
                }
            }

            var data = _binarySerializer.Serialize(topicQueueInfoList);
            return new RemotingResponse((int)ResponseCode.Success, request.Sequence, data);
        }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest request)
 {
     var topic = Encoding.UTF8.GetString(request.Body);
     var queueCount = _messageService.GetTopicQueueCount(topic);
     var data = BitConverter.GetBytes(queueCount);
     return new RemotingResponse((int)ResponseCode.Success, request.Sequence, data);
 }
        public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest request)
        {
            var queryTopicConsumeInfoRequest = _binarySerializer.Deserialize<QueryTopicConsumeInfoRequest>(request.Body);
            var topicConsumeInfoList = new List<TopicConsumeInfo>();

            if (!string.IsNullOrEmpty(queryTopicConsumeInfoRequest.GroupName))
            {
                var consumerGroups = _brokerController.ConsumerManager.QueryConsumerGroup(queryTopicConsumeInfoRequest.GroupName);
                foreach (var consumerGroup in consumerGroups)
                {
                    foreach (var topicConsumeInfo in GetTopicConsumeInfoForGroup(consumerGroup, queryTopicConsumeInfoRequest.Topic))
                    {
                        topicConsumeInfoList.Add(topicConsumeInfo);
                    }
                }
            }
            else
            {
                var consumerGroups = _brokerController.ConsumerManager.GetAllConsumerGroups();
                foreach (var consumerGroup in consumerGroups)
                {
                    foreach (var topicConsumeInfo in GetTopicConsumeInfoForGroup(consumerGroup, queryTopicConsumeInfoRequest.Topic))
                    {
                        topicConsumeInfoList.Add(topicConsumeInfo);
                    }
                }
            }

            var data = _binarySerializer.Serialize(topicConsumeInfoList);
            return new RemotingResponse((int)ResponseCode.Success, request.Sequence, data);
        }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
 {
     var request = _binarySerializer.Deserialize<QueryMessageRequest>(remotingRequest.Body);
     var total = 0;
     var messages = _messageStore.QueryMessages(request.Topic, request.QueueId, request.Code, request.RoutingKey, request.PageIndex, request.PageSize, out total).ToList();
     return new RemotingResponse((int)ResponseCode.Success, remotingRequest.Sequence, _binarySerializer.Serialize(new QueryMessageResponse(total, messages)));
 }
        public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
        {
            var request = _binarySerializer.Deserialize<QueryConsumerInfoRequest>(remotingRequest.Body);
            var consumerInfoList = new List<ConsumerInfo>();

            if (!string.IsNullOrEmpty(request.GroupName))
            {
                var consumerGroups = _consumerManager.QueryConsumerGroup(request.GroupName);
                foreach (var consumerGroup in consumerGroups)
                {
                    foreach (var topicConsumeInfo in GetConsumerInfoForGroup(consumerGroup, request.Topic))
                    {
                        consumerInfoList.Add(topicConsumeInfo);
                    }
                }
            }
            else
            {
                var consumerGroups = _consumerManager.GetAllConsumerGroups();
                foreach (var consumerGroup in consumerGroups)
                {
                    foreach (var topicConsumeInfo in GetConsumerInfoForGroup(consumerGroup, request.Topic))
                    {
                        consumerInfoList.Add(topicConsumeInfo);
                    }
                }
            }

            return RemotingResponseFactory.CreateResponse(remotingRequest, _binarySerializer.Serialize(consumerInfoList));
        }
        public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
        {
            if (BrokerController.Instance.IsCleaning)
            {
                throw new BrokerCleanningException();
            }

            var request = _binarySerializer.Deserialize<QueryTopicQueueInfoRequest>(remotingRequest.Body);
            var topicQueueInfoList = new List<TopicQueueInfo>();
            var queues = _queueStore.QueryQueues(request.Topic).ToList().OrderBy(x => x.Topic).ThenBy(x => x.QueueId);

            foreach (var queue in queues)
            {
                var topicQueueInfo = new TopicQueueInfo();
                topicQueueInfo.Topic = queue.Topic;
                topicQueueInfo.QueueId = queue.QueueId;
                topicQueueInfo.QueueCurrentOffset = queue.NextOffset - 1;
                topicQueueInfo.QueueMinOffset = queue.GetMinQueueOffset();
                topicQueueInfo.QueueMinConsumedOffset = _offsetStore.GetMinConsumedOffset(queue.Topic, queue.QueueId);
                topicQueueInfo.ProducerVisible = queue.Setting.ProducerVisible;
                topicQueueInfo.ConsumerVisible = queue.Setting.ConsumerVisible;
                topicQueueInfoList.Add(topicQueueInfo);
            }

            return RemotingResponseFactory.CreateResponse(remotingRequest, _binarySerializer.Serialize(topicQueueInfoList));
        }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
 {
     var request = _binarySerializer.Deserialize<GetClusterBrokersRequest>(remotingRequest.Body);
     var brokerInfoList = _clusterManager.GetClusterBrokers(request);
     var data = _binarySerializer.Serialize(brokerInfoList);
     return RemotingResponseFactory.CreateResponse(remotingRequest, data);
 }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
 {
     var request = _binarySerializer.Deserialize<GetTopicAccumulateInfoListRequest>(remotingRequest.Body);
     var topicAccumulateInfoList = _clusterManager.GetTopicAccumulateInfoList(request);
     var data = _binarySerializer.Serialize(topicAccumulateInfoList);
     return RemotingResponseFactory.CreateResponse(remotingRequest, data);
 }
        public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
        {
            var request = _binarySerializer.Deserialize<QueryTopicQueueInfoRequest>(remotingRequest.Body);
            var topicQueueInfoList = new List<TopicQueueInfo>();
            var topicList = !string.IsNullOrEmpty(request.Topic) ? new List<string> { request.Topic } : _queueService.GetAllTopics().ToList();

            foreach (var topic in topicList)
            {
                var queues = _queueService.QueryQueues(topic).ToList();
                foreach (var queue in queues)
                {
                    var topicQueueInfo = new TopicQueueInfo();
                    topicQueueInfo.Topic = queue.Topic;
                    topicQueueInfo.QueueId = queue.QueueId;
                    topicQueueInfo.QueueCurrentOffset = queue.CurrentOffset;
                    topicQueueInfo.QueueMinOffset = queue.GetMinQueueOffset();
                    topicQueueInfo.QueueMessageCount = queue.GetMessageRealCount();
                    topicQueueInfo.QueueMaxConsumedOffset = _offsetManager.GetMinOffset(queue.Topic, queue.QueueId);
                    topicQueueInfo.Status = queue.Status;
                    topicQueueInfoList.Add(topicQueueInfo);
                }
            }

            return RemotingResponseFactory.CreateResponse(remotingRequest, _binarySerializer.Serialize(topicQueueInfoList));
        }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
 {
     var topic = Encoding.UTF8.GetString(remotingRequest.Body);
     var queueIds = _queueService.GetOrCreateQueues(topic, QueueStatus.Normal).Select(x => x.QueueId).ToList();
     var data = Encoding.UTF8.GetBytes(string.Join(",", queueIds));
     return new RemotingResponse((int)ResponseCode.Success, remotingRequest.Sequence, data);
 }
Esempio n. 16
0
        public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
        {
            if (BrokerController.Instance.IsCleaning)
            {
                return BuildBrokerIsCleaningResponse(remotingRequest);
            }

            var request = DeserializePullMessageRequest(remotingRequest.Body);
            var topic = request.MessageQueue.Topic;
            var tags = request.Tags;
            var queueId = request.MessageQueue.QueueId;
            var pullOffset = request.QueueOffset;

            //如果消费者第一次过来拉取消息,则计算下一个应该拉取的位置,并返回给消费者
            if (pullOffset < 0)
            {
                var nextConsumeOffset = GetNextConsumeOffset(topic, queueId, request.ConsumerGroup, request.ConsumeFromWhere);
                return BuildNextOffsetResetResponse(remotingRequest, nextConsumeOffset);
            }

            //尝试拉取消息
            var pullResult = PullMessages(topic, tags, queueId, pullOffset, request.PullMessageBatchSize);

            //处理消息拉取结果
            if (pullResult.Status == PullStatus.Found)
            {
                return BuildFoundResponse(remotingRequest, pullResult.Messages);
            }
            else if (pullResult.Status == PullStatus.NextOffsetReset)
            {
                return BuildNextOffsetResetResponse(remotingRequest, pullResult.NextBeginOffset);
            }
            else if (pullResult.Status == PullStatus.QueueNotExist)
            {
                return BuildQueueNotExistResponse(remotingRequest);
            }
            else if (pullResult.Status == PullStatus.NoNewMessage)
            {
                if (request.SuspendPullRequestMilliseconds > 0)
                {
                    var pullRequest = new PullRequest(
                        remotingRequest,
                        request,
                        context,
                        DateTime.Now,
                        request.SuspendPullRequestMilliseconds,
                        ExecutePullRequest,
                        ExecutePullRequest,
                        ExecuteNoNewMessagePullRequest,
                        ExecuteReplacedPullRequest);
                    _suspendedPullRequestManager.SuspendPullRequest(pullRequest);
                    return null;
                }
                return BuildNoNewMessageResponse(remotingRequest);
            }
            else
            {
                throw new Exception("Invalid pull result status.");
            }
        }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest request)
 {
     var consumerData = _binarySerializer.Deserialize<ConsumerData>(request.Body);
     _brokerController.ConsumerManager.RegisterConsumer(
         consumerData.GroupName,
         new ClientChannel(consumerData.ConsumerId, context.Channel), consumerData.SubscriptionTopics);
     return null;
 }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
 {
     if (BrokerController.Instance.IsCleaning)
     {
         throw new BrokerCleanningException();
     }
     var statisticInfo = BrokerController.Instance.GetBrokerStatisticInfo();
     return RemotingResponseFactory.CreateResponse(remotingRequest, _binarySerializer.Serialize(statisticInfo));
 }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
 {
     if (BrokerController.Instance.IsCleaning)
     {
         throw new BrokerCleanningException();
     }
     var latestSendMessageIds = BrokerController.Instance.GetLatestSendMessageIds();
     return RemotingResponseFactory.CreateResponse(remotingRequest, Encoding.UTF8.GetBytes(latestSendMessageIds));
 }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
 {
     if (BrokerController.Instance.IsCleaning)
     {
         throw new BrokerCleanningException();
     }
     var producerIdList = _producerManager.GetAllProducers();
     var data = Encoding.UTF8.GetBytes(string.Join(",", producerIdList));
     return RemotingResponseFactory.CreateResponse(remotingRequest, data);
 }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
 {
     if (BrokerController.Instance.IsCleaning)
     {
         throw new BrokerCleanningException();
     }
     var request = _binarySerializer.Deserialize<SetQueueProducerVisibleRequest>(remotingRequest.Body);
     _queueStore.SetProducerVisible(request.Topic, request.QueueId, request.Visible);
     return RemotingResponseFactory.CreateResponse(remotingRequest);
 }
Esempio n. 22
0
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
 {
     if (BrokerController.Instance.IsCleaning)
     {
         throw new BrokerCleanningException();
     }
     var addQueueRequest = _binarySerializer.Deserialize<AddQueueRequest>(remotingRequest.Body);
     _queueStore.AddQueue(addQueueRequest.Topic);
     return RemotingResponseFactory.CreateResponse(remotingRequest);
 }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest request)
 {
     var updateQueueOffsetRequest = _binarySerializer.Deserialize<UpdateQueueOffsetRequest>(request.Body);
     _offsetManager.UpdateQueueOffset(
         updateQueueOffsetRequest.MessageQueue.Topic,
         updateQueueOffsetRequest.MessageQueue.QueueId,
         updateQueueOffsetRequest.QueueOffset,
         updateQueueOffsetRequest.ConsumerGroup);
     return null;
 }
        public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
        {
            var request = DeserializePullMessageRequest(remotingRequest.Body);
            var topic = request.MessageQueue.Topic;
            var queueId = request.MessageQueue.QueueId;
            var pullOffset = request.QueueOffset;

            //如果消费者第一次过来拉取消息,则计算下一个应该拉取的位置,并返回给消费者
            if (pullOffset < 0)
            {
                var nextConsumeOffset = GetNextConsumeOffset(topic, queueId, request.ConsumerGroup, request.ConsumeFromWhere);
                return BuildNextOffsetResetResponse(remotingRequest, nextConsumeOffset);
            }

            //尝试拉取消息
            var messages = _messageService.GetMessages(topic, queueId, pullOffset, request.PullMessageBatchSize);

            //如果消息存在,则返回消息
            if (messages.Count() > 0)
            {
                return BuildFoundResponse(remotingRequest, messages);
            }

            //消息不存在,如果挂起时间大于0,则挂起请求
            if (request.SuspendPullRequestMilliseconds > 0)
            {
                var pullRequest = new PullRequest(
                    remotingRequest,
                    request,
                    context,
                    DateTime.Now,
                    request.SuspendPullRequestMilliseconds,
                    ExecutePullRequest,
                    ExecutePullRequest,
                    ExecuteReplacedPullRequest);
                _suspendedPullRequestManager.SuspendPullRequest(pullRequest);
                return null;
            }

            var queueMinOffset = _queueService.GetQueueMinOffset(topic, queueId);
            var queueCurrentOffset = _queueService.GetQueueCurrentOffset(topic, queueId);

            if (pullOffset < queueMinOffset)
            {
                return BuildNextOffsetResetResponse(remotingRequest, queueMinOffset);
            }
            else if (pullOffset > queueCurrentOffset + 1)
            {
                return BuildNextOffsetResetResponse(remotingRequest, queueCurrentOffset + 1);
            }
            else
            {
                return BuildNoNewMessageResponse(remotingRequest);
            }
        }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
 {
     var request = _binarySerializer.Deserialize<GetMessageDetailRequest>(remotingRequest.Body);
     var message = _messageStore.FindMessage(request.MessageOffset, request.MessageId);
     var messages = new List<QueueMessage>();
     if (message != null)
     {
         messages.Add(message);
     }
     return RemotingResponseFactory.CreateResponse(remotingRequest, _binarySerializer.Serialize(messages));
 }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
 {
     if (BrokerController.Instance.IsCleaning)
     {
         throw new BrokerCleanningException();
     }
     var request = _binarySerializer.Deserialize<CreateTopicRequest>(remotingRequest.Body);
     IEnumerable<int> queueIds = _queueStore.CreateTopic(request.Topic, request.InitialQueueCount);
     var data = _binarySerializer.Serialize(queueIds);
     return RemotingResponseFactory.CreateResponse(remotingRequest, data);
 }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
 {
     if (BrokerController.Instance.IsCleaning)
     {
         return RemotingResponseFactory.CreateResponse(remotingRequest, Encoding.UTF8.GetBytes(string.Empty));
     }
     var topic = Encoding.UTF8.GetString(remotingRequest.Body);
     var queueIds = _queueStore.GetQueues(topic, BrokerController.Instance.Setting.AutoCreateTopic).Where(x => x.Setting.ProducerVisible).Select(x => x.QueueId).ToList();
     var data = Encoding.UTF8.GetBytes(string.Join(",", queueIds));
     return RemotingResponseFactory.CreateResponse(remotingRequest, data);
 }
        public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
        {
            if (BrokerController.Instance.IsCleaning)
            {
                throw new BrokerCleanningException();
            }

            var request = _binarySerializer.Deserialize<GetTopicQueueInfoRequest>(remotingRequest.Body);
            var topicQueueInfoList = _queueStore.GetTopicQueueInfoList(request.Topic);

            return RemotingResponseFactory.CreateResponse(remotingRequest, _binarySerializer.Serialize(topicQueueInfoList));
        }
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest request)
 {
     var sendMessageRequest = MessageUtils.DecodeSendMessageRequest(request.Body);
     var storeResult = _messageService.StoreMessage(sendMessageRequest.Message, sendMessageRequest.QueueId);
     _brokerController.SuspendedPullRequestManager.NotifyNewMessage(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 = _binarySerializer.Deserialize<QueryConsumerRequest>(remotingRequest.Body);
     var consumerGroup = _consumerManager.GetConsumerGroup(request.GroupName);
     var consumerIdList = new List<string>();
     if (consumerGroup != null)
     {
         consumerIdList = consumerGroup.GetConsumerIdsForTopic(request.Topic).ToList();
         consumerIdList.Sort();
     }
     return RemotingResponseFactory.CreateResponse(remotingRequest, Encoding.UTF8.GetBytes(string.Join(",", consumerIdList)));
 }
Esempio n. 31
0
        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.Queue.AddMessage(record.LogPosition, record.Tag);
                storeContext.MessageLogRecord = record;
                _bufferQueue.EnqueueMessage(storeContext);
            }, new StoreContext
            {
                RequestHandlerContext = context,
                RemotingRequest       = remotingRequest,
                Queue = queue,
                SendMessageRequestHandler = this
            });

            return(null);
        }
        public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
        {
            if (BrokerController.Instance.IsCleaning)
            {
                throw new BrokerCleanningException();
            }

            var request = _binarySerializer.Deserialize <DeleteConsumerGroupRequest>(remotingRequest.Body);

            if (string.IsNullOrEmpty(request.GroupName))
            {
                throw new ArgumentException("DeleteConsumerGroupRequest.GroupName cannot be null or empty.");
            }
            var consumerGroup = _consumerManager.GetConsumerGroup(request.GroupName);

            if (consumerGroup != null && consumerGroup.GetConsumerCount() > 0)
            {
                throw new Exception("Consumer group has consumer exist, not allowed to delete.");
            }

            var success = _offsetStore.DeleteConsumerGroup(request.GroupName);

            return(RemotingResponseFactory.CreateResponse(remotingRequest, Encoding.UTF8.GetBytes(success ? "1" : "0")));
        }
Esempio n. 33
0
        public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
        {
            if (BrokerController.Instance.IsCleaning)
            {
                throw new BrokerCleanningException();
            }
            var messages          = new List <QueueMessage>();
            var request           = _binarySerializer.Deserialize <GetMessageDetailRequest>(remotingRequest.Body);
            var messageInfo       = MessageIdUtil.ParseMessageId(request.MessageId);
            var currentBrokerName = BrokerController.Instance.Setting.BrokerInfo.BrokerName;

            if (currentBrokerName != messageInfo.BrokerName)
            {
                return(RemotingResponseFactory.CreateResponse(remotingRequest, _binarySerializer.Serialize(messages)));
            }

            var message = _messageStore.GetMessage(messageInfo.MessagePosition);

            if (message != null)
            {
                messages.Add(message);
            }
            return(RemotingResponseFactory.CreateResponse(remotingRequest, _binarySerializer.Serialize(messages)));
        }
        public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
        {
            if (BrokerController.Instance.IsCleaning)
            {
                throw new BrokerCleanningException();
            }

            var request               = _binarySerializer.Deserialize <QueryConsumerInfoRequest>(remotingRequest.Body);
            var consumerInfoList      = new List <ConsumerInfo>();
            var consumerGroups        = default(IEnumerable <ConsumerGroup>);
            var allConsumerGroupNames = _offsetStore.GetAllConsumerGroupNames();

            if (!string.IsNullOrEmpty(request.GroupName))
            {
                consumerGroups = _consumerManager.QueryConsumerGroup(request.GroupName);
            }
            else
            {
                consumerGroups = _consumerManager.GetAllConsumerGroups();
            }

            var notEmptyConsumerGroups = _consumerManager.GetAllConsumerGroups().Where(x => x.GetConsumerCount() > 0);
            var emptyConsumerGroups    = allConsumerGroupNames.Where(x => notEmptyConsumerGroups.Count() == 0 || !notEmptyConsumerGroups.Any(y => y.GroupName == x));

            foreach (var groupName in emptyConsumerGroups)
            {
                consumerInfoList.Add(BuildConsumerInfoForEmptyGroup(groupName));
            }

            foreach (var consumerGroup in consumerGroups)
            {
                consumerInfoList.AddRange(GetConsumerInfoForGroup(consumerGroup, request.Topic));
            }

            return(RemotingResponseFactory.CreateResponse(remotingRequest, _binarySerializer.Serialize(consumerInfoList)));
        }
Esempio n. 35
0
 /// <summary>
 /// Handle request async
 /// </summary>
 /// <param name="param">request</param>
 /// <param name="handlerContext">handler context</param>
 /// <returns></returns>
 public Task HandleAsync(TRequest param, IRequestHandlerContext handlerContext)
 {
     return(_action(param, handlerContext));
 }
Esempio n. 36
0
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
 {
     return(_serverMethodCaller.HandleRequest(remotingRequest));
 }
Esempio n. 37
0
 public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
 {
     Interlocked.Increment(ref _handledCount);
     return(new RemotingResponse(remotingRequest.Code, 10, remotingRequest.Type, response, remotingRequest.Sequence));
 }
Esempio n. 38
0
 public Task HandleAsync(TestRequestTrigger param, IRequestHandlerContext handlerContext)
 {
     handlerContext.Response = param.Result;
     return(Task.Delay(1));
 }
        public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
        {
            var statisticInfo = BrokerController.Instance.GetBrokerStatisticInfo();

            return(RemotingResponseFactory.CreateResponse(remotingRequest, _binarySerializer.Serialize(statisticInfo)));
        }
Esempio n. 40
0
        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  = BatchMessageUtils.DecodeSendMessageRequest(remotingRequest.Body);
            var messages = request.Messages;

            if (messages.Count() == 0)
            {
                throw new ArgumentException("Invalid batchSendMessage, message list cannot be blank.");
            }

            var topic   = messages.First().Topic;
            var queueId = request.QueueId;
            var queue   = _queueStore.GetQueue(topic, queueId);

            if (queue == null)
            {
                throw new QueueNotExistException(topic, queueId);
            }

            _messageStore.BatchStoreMessageAsync(queue, request.Messages, (batchRecord, parameter) =>
            {
                var storeContext = parameter as StoreContext;
                if (batchRecord.Records.Any(x => x.LogPosition < 0 || string.IsNullOrEmpty(x.MessageId)))
                {
                    storeContext.Success = false;
                }
                else
                {
                    foreach (var record in batchRecord.Records)
                    {
                        storeContext.Queue.AddMessage(record.LogPosition, record.Tag);
                    }
                    storeContext.BatchMessageLogRecord = batchRecord;
                    storeContext.Success = true;
                }
                _bufferQueue.EnqueueMessage(storeContext);
            }, new StoreContext
            {
                RequestHandlerContext = context,
                RemotingRequest       = remotingRequest,
                Queue = queue,
                BatchSendMessageRequestHandler = this
            }, request.ProducerAddress);

            foreach (var message in request.Messages)
            {
                _tpsStatisticService.AddTopicSendCount(topic, queueId);
            }

            return(null);
        }
        public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
        {
            if (BrokerController.Instance.IsCleaning)
            {
                return(BuildBrokerIsCleaningResponse(remotingRequest));
            }

            var request    = DeserializePullMessageRequest(remotingRequest.Body);
            var topic      = request.MessageQueue.Topic;
            var tags       = request.Tags;
            var queueId    = request.MessageQueue.QueueId;
            var pullOffset = request.QueueOffset;

            //如果消费者第一次过来拉取消息,则计算下一个应该拉取的位置,并返回给消费者
            var nextConsumeOffset = 0L;

            if (pullOffset < 0)
            {
                nextConsumeOffset = GetNextConsumeOffset(topic, queueId, request.ConsumerGroup, request.ConsumeFromWhere, request.IgnoreLastConsumedOffset);
                return(BuildNextOffsetResetResponse(remotingRequest, nextConsumeOffset));
            }
            //如果用户人工指定了下次要拉取的位置,则返回该位置给消费者并清除该指定的位置
            else if (_offsetStore.TryFetchNextConsumeOffset(topic, queueId, request.ConsumerGroup, out nextConsumeOffset))
            {
                return(BuildNextOffsetResetResponse(remotingRequest, nextConsumeOffset));
            }

            //尝试拉取消息
            var pullResult = PullMessages(topic, tags, queueId, pullOffset, request.PullMessageBatchSize);

            //处理消息拉取结果
            if (pullResult.Status == PullStatus.Found)
            {
                return(BuildFoundResponse(remotingRequest, pullResult.Messages));
            }
            else if (pullResult.Status == PullStatus.NextOffsetReset)
            {
                return(BuildNextOffsetResetResponse(remotingRequest, pullResult.NextBeginOffset));
            }
            else if (pullResult.Status == PullStatus.QueueNotExist)
            {
                return(BuildQueueNotExistResponse(remotingRequest));
            }
            else if (pullResult.Status == PullStatus.NoNewMessage)
            {
                if (request.SuspendPullRequestMilliseconds > 0)
                {
                    var pullRequest = new PullRequest(
                        remotingRequest,
                        request,
                        context,
                        DateTime.Now,
                        request.SuspendPullRequestMilliseconds,
                        ExecutePullRequest,
                        ExecutePullRequest,
                        ExecuteNoNewMessagePullRequest,
                        ExecuteReplacedPullRequest);
                    _suspendedPullRequestManager.SuspendPullRequest(pullRequest);
                    return(null);
                }
                return(BuildNoNewMessageResponse(remotingRequest));
            }
            else
            {
                throw new Exception("Invalid pull result status.");
            }
        }
        public RemotingResponse HandleRequest(IRequestHandlerContext context, RemotingRequest remotingRequest)
        {
            var statisticInfo = BrokerController.Instance.GetBrokerStatisticInfo();

            return(new RemotingResponse((int)ResponseCode.Success, remotingRequest.Sequence, _binarySerializer.Serialize(statisticInfo)));
        }