示例#1
0
        public void DispatchMessage(ImmutableEnvelope envelope)
        {
            if (envelope.Items.Length != 1)
            {
                throw new InvalidOperationException(
                          "Batch message arrived to the shared scope. Are you batching events or dispatching commands to shared scope?");
            }

            // we get failure if one of the subscribers fails
            // hence, if any of the handlers fail - we give up
            var item = envelope.Items[0];

            Type[] consumerTypes;

            if (!_dispatcher.TryGetValue(item.MappedType, out consumerTypes))
            {
                // else -> we don't have consumers. It's OK for the event
                _observer.Notify(new EventHadNoConsumers(envelope.EnvelopeId, item.MappedType));
                return;
            }

            using (var scope = _strategy.BeginEnvelopeScope())
            {
                foreach (var consumerType in consumerTypes)
                {
                    scope.Dispatch(consumerType, envelope, item);
                }
                scope.Complete();
            }
        }
示例#2
0
        void ISingleThreadMessageDispatcher.DispatchMessage(ImmutableEnvelope message)
        {
            // empty message, hm...
            if (message.Items.Length == 0)
            {
                return;
            }

            // verify that all consumers are available
            foreach (var item in message.Items)
            {
                if (!_messageConsumers.ContainsKey(item.MappedType))
                {
                    throw new InvalidOperationException("Couldn't find consumer for " + item.MappedType);
                }
            }

            using (var scope = _strategy.BeginEnvelopeScope())
            {
                foreach (var item in message.Items)
                {
                    var consumerType = _messageConsumers[item.MappedType];
                    scope.Dispatch(consumerType, message, item);
                }
                scope.Complete();
            }
        }