Пример #1
0
        public async Task Update(CancellationToken cancellationToken)
        {
            var start = await _tracker.GetNext(_messageJournal, Id);

            var journalResult = await _messageJournal.Read(start, BatchSize, Filter, cancellationToken);

            if (journalResult.Entries.Any())
            {
                foreach (var entry in journalResult.Entries.Where(x => x != null))
                {
                    var t = _messageNamingService.GetTypeForName(GetEventName(entry));

                    if (t == null)
                    {
                        continue;
                    }

                    var serializer   = _serializationService.GetSerializer(entry.Data.Headers.ContentType);
                    var journalEvent = serializer.Deserialize(entry.Data.Content, t);


                    if (_eventProcessor.Handlers.ContainsKey(t))
                    {
                        await _eventProcessor.Handlers[t](journalEvent);
                    }
                }

                start = journalResult.Next;
                await _tracker.Update(Id, start);
            }
        }
Пример #2
0
        /// <summary>
        /// Deserializes the <see cref="Message.Content"/> of the supplied <paramref name="message"/>
        /// into an instance of the appropriate runtime <see cref="System.Type"/> based on the
        /// <see cref="IMessageHeaders.MessageName"/> and <see cref="IMessageHeaders.ContentType"/>
        /// specified in the <see cref="Message.Headers"/>
        /// </summary>
        /// <param name="message">The message whose content is to be deserialized</param>
        /// <returns>The deserialized content of the specified <paramref name="message"/></returns>
        public object Unmarshal(Message message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var messageName = message.Headers.MessageName;
            var content     = message.Content;
            var contentType = string.IsNullOrWhiteSpace(message.Headers.ContentType)
                ? _defaultContentType
                : message.Headers.ContentType;

            var messageType = _messageNamingService.GetTypeForName(messageName);
            var serializer  = _serializationService.GetSerializer(contentType);

            return(serializer.Deserialize(content, messageType));
        }
Пример #3
0
        public static async Task HandleMessage(IMessageNamingService messageNamingService,
            ISerializationService serializationService, IEnumerable<IMessageHandler> messageHandlers, Message message,
            IMessageContext messageContext, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (message.Headers.Expires < DateTime.UtcNow)
            {
                Log.WarnFormat("Discarding expired \"{0}\" message (ID {1}, expired {2})", message.Headers.MessageName,
                    message.Headers.MessageId, message.Headers.Expires);

                messageContext.Acknowledge();
                return;
            }

            var messageType = messageNamingService.GetTypeForName(message.Headers.MessageName);
            var serializer = serializationService.GetSerializer(message.Headers.ContentType);
            var messageContent = serializer.Deserialize(message.Content, messageType);
            var handlingTasks =
                messageHandlers.Select(
                    handler => handler.HandleMessage(messageContent, messageContext, cancellationToken));

            await Task.WhenAll(handlingTasks);
        }