예제 #1
0
        /// <summary>
        /// Publishes a message to all subscribed consumers for the message type
        /// </summary>
        /// <typeparam name="T">The type of the message</typeparam>
        /// <param name="message">The messages to be published</param>
        /// <param name="contextCallback">The callback to perform operations on the context</param>
        public void Publish <T>(T message, Action <IPublishContext <T> > contextCallback)
            where T : class
        {
            PublishContext <T> context = ContextStorage.CreatePublishContext(message);

            context.SetSourceAddress(Endpoint.Address.Uri);

            contextCallback(context);

            IList <Exception> exceptions = new List <Exception>();

            int publishedCount = 0;

            foreach (var consumer in OutboundPipeline.Enumerate(context))
            {
                try
                {
                    consumer(context);
                    publishedCount++;
                }
                catch (Exception ex)
                {
                    _log.Error(string.Format("'{0}' threw an exception publishing message '{1}'",
                                             consumer.GetType().FullName, message.GetType().FullName), ex);

                    exceptions.Add(ex);
                }
            }

            context.Complete();

            if (publishedCount == 0)
            {
                context.NotifyNoSubscribers();
            }

            _eventChannel.Send(new MessagePublished
            {
                MessageType   = typeof(T),
                ConsumerCount = publishedCount,
                Duration      = context.Duration,
            });

            if (exceptions.Count > 0)
            {
                throw new PublishException(typeof(T), exceptions);
            }
        }
예제 #2
0
        /// <summary>
        /// Publishes a message to all subscribed consumers for the message type
        /// </summary>
        /// <typeparam name="T">The type of the message</typeparam>
        /// <param name="message">The messages to be published</param>
        public void Publish <T>(T message)
            where T : class
        {
            Stopwatch publishDuration = Stopwatch.StartNew();

            IOutboundMessageContext context = OutboundMessage.Context;

            context.SetSourceAddress(Endpoint.Uri);
            context.SetMessageType(typeof(T));

            int publishedCount = 0;

            foreach (var consumer in OutboundPipeline.Enumerate(message))
            {
                try
                {
                    consumer(message);
                    publishedCount++;
                }
                catch (Exception ex)
                {
                    _log.Error(string.Format("'{0}' threw an exception publishing message '{1}'",
                                             consumer.GetType().FullName, message.GetType().FullName), ex);
                }
            }

            publishDuration.Stop();

            if (publishedCount == 0)
            {
                context.NotifyNoSubscribers(message);
            }

            _eventAggregator.Send(new MessagePublished
            {
                MessageType   = typeof(T),
                ConsumerCount = publishedCount,
                Duration      = publishDuration.Elapsed,
            });

            context.Reset();
        }