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; }
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); }
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); }
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); }
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)); } }
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); }
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); }
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); }
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; } } } }
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); }
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--; } } }
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); } } }
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; }
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); }
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; }
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++; } } }); }
public ConsumingMessage(QueueMessage message, ProcessQueue processQueue) { Message = message; ProcessQueue = processQueue; }
public EventProcessContext(QueueMessage queueMessage, EventMessage eventMessage, Action<EventStream, EventProcessContext> eventProcessedAction) { QueueMessage = queueMessage; EventMessage = eventMessage; EventProcessedAction = eventProcessedAction; }
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++; } }
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; }
public DomainEventStreamProcessContext(DomainEventConsumer eventConsumer, DomainEventStreamMessage domainEventStreamMessage, QueueMessage queueMessage, IMessageContext messageContext) : base(queueMessage, messageContext) { _eventConsumer = eventConsumer; _domainEventStreamMessage = domainEventStreamMessage; }
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); } }
public ConsumingMessage(QueueMessage message, PullRequest pullRequest) { Message = message; PullRequest = pullRequest; Message.BrokerName = pullRequest.MessageQueue.BrokerName; }
protected abstract void ConsumeMessage(TMessage messageContext, EQueueProtocols.QueueMessage message);
public virtual void Handle(EQueueProtocols.QueueMessage message, EQueueClientsConsumers.IMessageContext context) { ConsumeMessage(message.Body.GetMessage <TMessage>(), message); }
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; }
public EQueueProcessContext(QueueMessage queueMessage, IMessageContext messageContext) { _queueMessage = queueMessage; _messageContext = messageContext; }
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; }
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; } }
public WrappedMessage(MessageQueue messageQueue, QueueMessage queueMessage, ProcessQueue processQueue) { MessageQueue = messageQueue; QueueMessage = queueMessage; ProcessQueue = processQueue; }
public void Handle(QueueMessage message, IMessageContext context) { Interlocked.Increment(ref _handledCount); Thread.Sleep(20); context.OnMessageHandled(message); }
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)); }
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; }
public void Handle(QueueMessage message, IMessageContext context) { Interlocked.Increment(ref _handledCount); _rtStatisticService.AddRT((DateTime.Now - message.CreatedTime).TotalMilliseconds); context.OnMessageHandled(message); }
public void RemoveMessage(QueueMessage message) { QueueMessage removedMessage; _messageDict.TryRemove(message.QueueOffset, out removedMessage); }
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); }