コード例 #1
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;
 }
コード例 #2
0
 void IQueueMessageHandler.Handle(QueueMessage queueMessage, IMessageContext context)
 {
     var applicationMessageType = _typeNameProvider.GetType(queueMessage.Tag);
     var message = _jsonSerializer.Deserialize(Encoding.UTF8.GetString(queueMessage.Body), applicationMessageType) as IApplicationMessage;
     var processContext = new EQueueProcessContext(queueMessage, context);
     var processingMessage = new ProcessingApplicationMessage(message, processContext);
     _processor.Process(processingMessage);
 }
コード例 #3
0
ファイル: DomainEventConsumer.cs プロジェクト: ulswww/enode
 void IQueueMessageHandler.Handle(QueueMessage queueMessage, IMessageContext context)
 {
     var message = _jsonSerializer.Deserialize<EventStreamMessage>(Encoding.UTF8.GetString(queueMessage.Body));
     var domainEventStreamMessage = ConvertToDomainEventStream(message);
     var processContext = new DomainEventStreamProcessContext(this, domainEventStreamMessage, queueMessage, context);
     var processingMessage = new ProcessingDomainEventStreamMessage(domainEventStreamMessage, processContext);
     _processor.Process(processingMessage);
 }
コード例 #4
0
ファイル: Program.cs プロジェクト: peidachang/equeue
 public void Handle(QueueMessage message, IMessageContext context)
 {
     var count = Interlocked.Increment(ref _handledCount);
     if (count % 1000 == 0)
     {
         Console.WriteLine("Total handled {0} messages.", count);
     }
     context.OnMessageHandled(message);
 }
コード例 #5
0
ファイル: EventConsumer.cs プロジェクト: key-value/enode
        void IMessageHandler.Handle(QueueMessage message, IMessageContext context)
        {
            var eventMessage = _binarySerializer.Deserialize(message.Body, typeof(EventMessage)) as EventMessage;
            var eventStream = ConvertToEventStream(eventMessage);

            if (_messageContextDict.TryAdd(eventStream.CommitId, context))
            {
                _eventProcessor.Process(eventStream, new EventProcessContext(message, eventMessage, EventHandledCallback));
            }
        }
コード例 #6
0
        internal void AddMessage(EQueueMessages.QueueMessage message)
        {
            var slidingDoor = SlidingDoors.GetOrAdd(message.QueueId, partition =>
            {
                return(new SlidingDoor(CommitOffset,
                                       partition,
                                       Configuration.Instance.GetCommitPerMessage()));
            });

            slidingDoor.AddOffset(message.QueueOffset);
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: edgevagrant/equeue
            public void Handle(QueueMessage message, IMessageContext context)
            {
                var count = Interlocked.Increment(ref _handledCount);
                if (count == 1)
                {
                    _watch = Stopwatch.StartNew();
                }
                else if (count % 1000 == 0)
                {
                    _logger.InfoFormat("Total handled {0} messages, time spent:{1}", count, _watch.ElapsedMilliseconds);
                }

                context.OnMessageHandled(message);
            }
コード例 #8
0
ファイル: Program.cs プロジェクト: huoxudong125/equeue
            public void Handle(QueueMessage message, IMessageContext context)
            {
                var currentCount = Interlocked.Increment(ref _handledCount);
                if (currentCount == 1)
                {
                    _watch = Stopwatch.StartNew();
                }
                if (currentCount % 10000 == 0)
                {
                    _logger.InfoFormat("Total handled {0} messages, timeSpent: {1}ms, throughput: {2}/s", currentCount, _watch.ElapsedMilliseconds, currentCount * 1000 / _watch.ElapsedMilliseconds);
                }

                context.OnMessageHandled(message);
            }
コード例 #9
0
ファイル: ProcessQueue.cs プロジェクト: wangjiepower/equeue
 public void RemoveMessage(QueueMessage message)
 {
     lock (this)
     {
         if (_messageDict.Remove(message.QueueOffset))
         {
             if (_messageDict.Keys.IsNotEmpty())
             {
                 _consumedQueueOffset = _messageDict.Keys.First() - 1;
             }
             else
             {
                 _consumedQueueOffset = message.QueueOffset;
             }
         }
     }
 }
コード例 #10
0
 void IQueueMessageHandler.Handle(QueueMessage queueMessage, IMessageContext context)
 {
     var exceptionMessage = _jsonSerializer.Deserialize<PublishableExceptionMessage>(Encoding.UTF8.GetString(queueMessage.Body));
     var exceptionType = _typeNameProvider.GetType(queueMessage.Tag);
     var exception = FormatterServices.GetUninitializedObject(exceptionType) as IPublishableException;
     exception.Id = exceptionMessage.UniqueId;
     exception.Timestamp = exceptionMessage.Timestamp;
     exception.RestoreFrom(exceptionMessage.SerializableInfo);
     var sequenceMessage = exception as ISequenceMessage;
     if (sequenceMessage != null)
     {
         sequenceMessage.AggregateRootTypeName = exceptionMessage.AggregateRootTypeName;
         sequenceMessage.AggregateRootStringId = exceptionMessage.AggregateRootId;
     }
     var processContext = new EQueueProcessContext(queueMessage, context);
     var processingMessage = new ProcessingPublishableExceptionMessage(exception, processContext);
     _publishableExceptionProcessor.Process(processingMessage);
 }
コード例 #11
0
ファイル: ProcessQueue.cs プロジェクト: a407121393/equeue
 public void RemoveMessage(QueueMessage message)
 {
     lock (_lockObj)
     {
         if (_messageDict.Remove(message.QueueOffset))
         {
             if (_messageDict.Keys.IsNotEmpty())
             {
                 _consumedQueueOffset = _messageDict.Keys.First() - 1;
             }
             else
             {
                 _consumedQueueOffset = _maxQueueOffset;
             }
             _messageCount--;
         }
     }
 }
コード例 #12
0
        protected override void ConsumeMessage(MessageReply reply, EQueueProtocols.QueueMessage queueMessage)
        {
            _Logger.DebugFormat("Handle reply:{0} content:{1}", reply.MessageID, reply.ToJson());
            var messageState = CommandStateQueue[reply.MessageID] as MessageState;

            if (messageState != null)
            {
                CommandStateQueue.TryRemove(reply.MessageID);
                if (reply.Result is Exception)
                {
                    messageState.TaskCompletionSource.TrySetException(reply.Result as Exception);
                }
                else
                {
                    messageState.TaskCompletionSource.TrySetResult(reply.Result);
                }
            }
        }
コード例 #13
0
ファイル: ProcessQueue.cs プロジェクト: kouweizhong/equeue
        public long RemoveMessage(QueueMessage message)
        {
            var result = -1L;

            AtomWrite(_lock, () =>
            {
                if (_messageDict.Count > 0)
                {
                    result = _queueOffsetMax + 1;
                    _messageDict.Remove(message.QueueOffset);
                    if (_messageDict.Count > 0)
                    {
                        result = _messageDict.Keys.First();
                    }
                }
            });

            return result;
        }
コード例 #14
0
ファイル: Program.cs プロジェクト: huyuezheng/equeue
            public void Handle(QueueMessage message, IMessageContext context)
            {
                var currentCount = Interlocked.Increment(ref _handledCount);
                if (currentCount == 1)
                {
                    _watch = Stopwatch.StartNew();
                }
                if (currentCount % 10000 == 0)
                {
                    var currentElapsedMilliseconds = _watch.ElapsedMilliseconds;
                    _logger.InfoFormat("Total handled {0} messages, throughput: {1}, latency: {2}ms",
                        currentCount,
                        (currentCount - _previousHandledCount) * 1000 / (currentElapsedMilliseconds - _previousElapsedMilliseconds),
                        (DateTime.Now - message.CreatedTime).TotalMilliseconds);
                    _previousHandledCount = currentCount;
                    _previousElapsedMilliseconds = currentElapsedMilliseconds;
                }

                context.OnMessageHandled(message);
            }
コード例 #15
0
 public QueueMessage GetMessage(long position)
 {
     var buffer = GetMessageBuffer(position);
     if (buffer != null)
     {
         var nextOffset = 0;
         var messageLength = MessageUtils.DecodeInt(buffer, nextOffset, out nextOffset);
         if (messageLength > 0)
         {
             var message = new QueueMessage();
             var messageBytes = new byte[messageLength];
             Buffer.BlockCopy(buffer, nextOffset, messageBytes, 0, messageLength);
             message.ReadFrom(messageBytes);
             return message;
         }
     }
     return null;
 }
コード例 #16
0
        protected override void ConsumeMessage(IFramework.MessageQueue.EQueue.MessageFormat.MessageContext eventContext, EQueueProtocols.QueueMessage queueMessage)
        {
            _Logger.DebugFormat("Start Handle event , messageContextID:{0} queueID:{1}", eventContext.MessageID, queueMessage.QueueId);

            var message             = eventContext.Message;
            var messageHandlerTypes = HandlerProvider.GetHandlerTypes(message.GetType());

            if (messageHandlerTypes.Count == 0)
            {
                return;
            }

            messageHandlerTypes.ForEach(messageHandlerType =>
            {
                PerMessageContextLifetimeManager.CurrentMessageContext = eventContext;
                eventContext.ToBeSentMessageContexts.Clear();
                var messageStore     = IoCFactory.Resolve <IMessageStore>();
                var subscriptionName = string.Format("{0}.{1}", SubscribeTopic, messageHandlerType.FullName);
                if (!messageStore.HasEventHandled(eventContext.MessageID, subscriptionName))
                {
                    try
                    {
                        var messageHandler = IoCFactory.Resolve(messageHandlerType);
                        ((dynamic)messageHandler).Handle((dynamic)message);
                        var commandContexts = eventContext.ToBeSentMessageContexts;
                        var eventBus        = IoCFactory.Resolve <IEventBus>();
                        var messageContexts = new List <MessageContext>();
                        eventBus.GetMessages().ForEach(msg => messageContexts.Add(new MessageContext(msg)));

                        messageStore.SaveEvent(eventContext, subscriptionName, commandContexts, messageContexts);
                        if (commandContexts.Count > 0)
                        {
                            IoCFactory.Resolve <ICommandBus>().Send(commandContexts.AsEnumerable());
                        }
                        if (messageContexts.Count > 0)
                        {
                            IoCFactory.Resolve <IEventPublisher>().Publish(messageContexts.ToArray());
                        }
                    }
                    catch (Exception e)
                    {
                        if (e is DomainException)
                        {
                            _Logger.Warn(message.ToJson(), e);
                        }
                        else
                        {
                            //IO error or sytem Crash
                            _Logger.Error(message.ToJson(), e);
                        }
                        messageStore.SaveFailHandledEvent(eventContext, subscriptionName, e);
                    }
                    finally
                    {
                        PerMessageContextLifetimeManager.CurrentMessageContext = null;
                        MessageCount++;
                    }
                }
            });
        }
コード例 #17
0
 public ConsumingMessage(QueueMessage message, ProcessQueue processQueue)
 {
     Message = message;
     ProcessQueue = processQueue;
 }
コード例 #18
0
ファイル: EventConsumer.cs プロジェクト: key-value/enode
 public EventProcessContext(QueueMessage queueMessage, EventMessage eventMessage, Action<EventStream, EventProcessContext> eventProcessedAction)
 {
     QueueMessage = queueMessage;
     EventMessage = eventMessage;
     EventProcessedAction = eventProcessedAction;
 }
コード例 #19
0
        protected override void ConsumeMessage(IFramework.MessageQueue.EQueue.MessageFormat.MessageContext messageContext, EQueueProtocols.QueueMessage queueMessage)
        {
            if (messageContext == null || messageContext.Message == null)
            {
                return;
            }
            var message = messageContext.Message as ICommand;

            if (message == null)
            {
                return;
            }
            MessageReply  messageReply      = null;
            var           needRetry         = message.NeedRetry;
            bool          commandHasHandled = false;
            IMessageStore messageStore      = null;

            try
            {
                PerMessageContextLifetimeManager.CurrentMessageContext = messageContext;
                messageStore      = IoCFactory.Resolve <IMessageStore>();
                commandHasHandled = messageStore.HasCommandHandled(messageContext.MessageID);
                if (!commandHasHandled)
                {
                    var messageHandler = HandlerProvider.GetHandler(message.GetType());
                    _Logger.InfoFormat("Handle command, commandID:{0}", messageContext.MessageID);

                    if (messageHandler == null)
                    {
                        messageReply = new MessageReply(messageContext.ReplyToEndPoint, messageContext.MessageID, new NoHandlerExists());
                    }
                    else
                    {
                        do
                        {
                            try
                            {
                                ((dynamic)messageHandler).Handle((dynamic)message);
                                messageReply = new MessageReply(messageContext.ReplyToEndPoint, messageContext.MessageID, messageContext.Reply);
                                needRetry    = false;
                            }
                            catch (Exception ex)
                            {
                                if (!(ex is OptimisticConcurrencyException) || !needRetry)
                                {
                                    throw;
                                }
                            }
                        } while (needRetry);
                    }
                }
                else
                {
                    messageReply = new MessageReply(messageContext.ReplyToEndPoint, messageContext.MessageID, new MessageDuplicatelyHandled());
                }
            }
            catch (Exception e)
            {
                messageReply = new MessageReply(messageContext.ReplyToEndPoint, messageContext.MessageID, e.GetBaseException());
                if (e is DomainException)
                {
                    _Logger.Warn(message.ToJson(), e);
                }
                else
                {
                    _Logger.Error(message.ToJson(), e);
                }
                if (messageStore != null)
                {
                    messageStore.SaveFailedCommand(messageContext);
                }
            }
            finally
            {
                PerMessageContextLifetimeManager.CurrentMessageContext = null;
            }
            if (!commandHasHandled)
            {
                OnMessageHandled(messageContext, messageReply);
                HandledMessageCount++;
            }
        }
コード例 #20
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;
 }
コード例 #21
0
ファイル: DomainEventConsumer.cs プロジェクト: ulswww/enode
 public DomainEventStreamProcessContext(DomainEventConsumer eventConsumer, DomainEventStreamMessage domainEventStreamMessage, QueueMessage queueMessage, IMessageContext messageContext)
     : base(queueMessage, messageContext)
 {
     _eventConsumer = eventConsumer;
     _domainEventStreamMessage = domainEventStreamMessage;
 }
コード例 #22
0
 private void PersistMessage(QueueMessage message)
 {
     using (var connection = new SqlConnection(_setting.ConnectionString))
     {
         connection.Insert(new
         {
             MessageId = message.MessageId,
             MessageOffset = message.MessageOffset,
             MessageKey = message.Key,
             Topic = message.Topic,
             QueueId = message.QueueId,
             QueueOffset = message.QueueOffset,
             Code = message.Code,
             Body = message.Body,
             RoutingKey = message.RoutingKey,
             CreatedTime = message.CreatedTime,
             ArrivedTime = message.ArrivedTime,
             StoredTime = DateTime.Now
         }, _setting.MessageTable);
     }
 }
コード例 #23
0
ファイル: ConsumingMessage.cs プロジェクト: Aaron-Liu/equeue
 public ConsumingMessage(QueueMessage message, PullRequest pullRequest)
 {
     Message = message;
     PullRequest = pullRequest;
     Message.BrokerName = pullRequest.MessageQueue.BrokerName;
 }
コード例 #24
0
 protected abstract void ConsumeMessage(TMessage messageContext, EQueueProtocols.QueueMessage message);
コード例 #25
0
 public virtual void Handle(EQueueProtocols.QueueMessage message, EQueueClientsConsumers.IMessageContext context)
 {
     ConsumeMessage(message.Body.GetMessage <TMessage>(), message);
 }
コード例 #26
0
ファイル: Consumer.cs プロジェクト: uliian/equeue
 private bool IsQueueMessageMatchTag(QueueMessage message, HashSet<string> tags)
 {
     if (tags == null || tags.Count == 0)
     {
         return true;
     }
     foreach (var tag in tags)
     {
         if (tag == "*" || tag == message.Tag)
         {
             return true;
         }
     }
     return false;
 }
コード例 #27
0
 public EQueueProcessContext(QueueMessage queueMessage, IMessageContext messageContext)
 {
     _queueMessage = queueMessage;
     _messageContext = messageContext;
 }
コード例 #28
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.Key,
         message.Body,
         messageOffset,
         queueId,
         queueOffset,
         message.CreatedTime,
         DateTime.Now,
         DateTime.Now,
         routingKey);
     if (_messageDict.TryAdd(messageOffset, queueMessage))
     {
         WriteMessageLog(queueMessage);
     }
     return queueMessage;
 }
コード例 #29
0
 private void RecoverMessageToMemory(QueueMessage queueMessage)
 {
     if (_messageDict.Count < _setting.MessageMaxCacheSize)
     {
         _messageDict[queueMessage.MessageOffset] = queueMessage;
     }
     var key = string.Format("{0}-{1}", queueMessage.Topic, queueMessage.QueueId);
     long queueOffset;
     if (!_queueMaxPersistedOffsetDict.TryGetValue(key, out queueOffset) || queueOffset < queueMessage.QueueOffset)
     {
         _queueMaxPersistedOffsetDict[key] = queueMessage.QueueOffset;
     }
     if (_currentMessageOffset < queueMessage.MessageOffset)
     {
         _currentMessageOffset = queueMessage.MessageOffset;
         _persistedMessageOffset = queueMessage.MessageOffset;
     }
 }
コード例 #30
0
ファイル: WrappedMessage.cs プロジェクト: kouweizhong/equeue
 public WrappedMessage(MessageQueue messageQueue, QueueMessage queueMessage, ProcessQueue processQueue)
 {
     MessageQueue = messageQueue;
     QueueMessage = queueMessage;
     ProcessQueue = processQueue;
 }
コード例 #31
0
ファイル: Program.cs プロジェクト: tangxuehua/EQueueSample
 public void Handle(QueueMessage message, IMessageContext context)
 {
     Interlocked.Increment(ref _handledCount);
     Thread.Sleep(20);
     context.OnMessageHandled(message);
 }
コード例 #32
0
 private void WriteMessageLog(QueueMessage message)
 {
     var messageData = new MessageData
     {
         MessageId = message.MessageId,
         MessageOffset = message.MessageOffset,
         MessageKey = message.Key,
         Topic = message.Topic,
         QueueId = message.QueueId,
         QueueOffset = message.QueueOffset,
         RoutingKey = message.RoutingKey,
         Code = message.Code,
         Body = ObjectId.ToHexString(message.Body),
         Created = message.CreatedTime,
         Arrived = message.ArrivedTime
     };
     _messageLogger.Info(_jsonSerializer.Serialize(messageData));
 }
コード例 #33
0
ファイル: Consumer.cs プロジェクト: uliian/equeue
        private IEnumerable<QueueMessage> DecodeMessages(PullRequest pullRequest, byte[] buffer)
        {
            var messages = new List<QueueMessage>();
            if (buffer == null || buffer.Length <= 4)
            {
                return messages;
            }

            try
            {
                var nextOffset = 0;
                var messageLength = MessageUtils.DecodeInt(buffer, nextOffset, out nextOffset);
                while (messageLength > 0)
                {
                    var message = new QueueMessage();
                    var messageBytes = new byte[messageLength];
                    Buffer.BlockCopy(buffer, nextOffset, messageBytes, 0, messageLength);
                    nextOffset += messageLength;
                    message.ReadFrom(messageBytes);
                    if (!message.IsValid())
                    {
                        _logger.ErrorFormat("Invalid message, pullRequest: {0}", pullRequest);
                        continue;
                    }
                    messages.Add(message);
                    if (nextOffset >= buffer.Length)
                    {
                        break;
                    }
                    messageLength = MessageUtils.DecodeInt(buffer, nextOffset, out nextOffset);
                }
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Decode pull return message has exception, pullRequest: {0}", pullRequest), ex);
            }

            return messages;
        }
コード例 #34
0
ファイル: Program.cs プロジェクト: Aaron-Liu/equeue
 public void Handle(QueueMessage message, IMessageContext context)
 {
     Interlocked.Increment(ref _handledCount);
     _rtStatisticService.AddRT((DateTime.Now - message.CreatedTime).TotalMilliseconds);
     context.OnMessageHandled(message);
 }
コード例 #35
0
ファイル: ProcessQueue.cs プロジェクト: kakashi006/equeue
 public void RemoveMessage(QueueMessage message)
 {
     QueueMessage removedMessage;
     _messageDict.TryRemove(message.QueueOffset, out removedMessage);
 }
コード例 #36
0
 public MessageStoreResult StoreMessage(Message message, int queueId, long queueOffset)
 {
     var offset = GetNextOffset();
     _queueCurrentOffsetDict[offset] = new QueueMessage(message.Topic, message.Body, offset, queueId, queueOffset, DateTime.Now);
     return new MessageStoreResult(offset, queueId, queueOffset);
 }