コード例 #1
0
        public static RebusConfigurer RequireTimeToBeReceivedUsing <TAttribute>(this RebusConfigurer configurer)
            where TAttribute : System.Attribute
        {
            Guard.IsNotNull(() => configurer, configurer);

            return(configurer.Events(e =>
            {
                e.BeforeMessage += (bus, message) =>
                {
                    if (message == null)
                    {
                        return;
                    }

                    var type = message.GetType();
                    var context = MessageContext.GetCurrent();

                    if (type.GetCustomAttribute <TAttribute>() != null &&
                        !context.Headers.ContainsKey(Headers.TimeToBeReceived))
                    {
                        throw new InvalidOperationException("Message is missing 'TimeToBeReceived' header!");
                    }
                };
            }));
        }
コード例 #2
0
        public static RebusConfigurer RequireDateOnMessages(this RebusConfigurer configurer, IEnumerable <Type> inclusions = null, IEnumerable <Type> exclusions = null)
        {
            configurer.Events(e =>
            {
                e.BeforeMessage += (bus, message) =>
                {
                    if (message == null)
                    {
                        return;
                    }
                    if (exclusions != null && exclusions.Contains(message.GetType()))
                    {
                        return;
                    }
                    if (inclusions != null && !inclusions.Contains(message.GetType()))
                    {
                        return;
                    }

                    if (!MessageContext.GetCurrent().Headers.ContainsKey(MessageDataHeader))
                    {
                        throw new ArgumentException("Message missing 'message-date' header!");
                    }
                };
            });
            return(configurer);
        }
コード例 #3
0
 public static RebusConfigurer AutomaticallyTransferUserContext(this RebusConfigurer configurer)
 {
     return(configurer.Events(e =>
     {
         e.MessageSent += EncodeUserContextHeadersIfPossible;
         e.MessageContextEstablished += DecodeUserContextHeadersIfPossible;
     }));
 }
コード例 #4
0
 public static RebusConfigurer SetDateOnSentMessages(this RebusConfigurer configurer)
 {
     configurer.Events(e =>
     {
         e.MessageSent += SetDateOnMessageSent;
     });
     return(configurer);
 }
コード例 #5
0
        public static RebusConfigurer EnablePerMessageRunningTimeReporting(this RebusConfigurer configurer, TimeSpan threshold)
        {
            configurer.Events(e =>
            {
                e.MessageContextEstablished += (_, ctx) =>
                {
                    var sw = new System.Diagnostics.Stopwatch();

                    ctx.Items[StopwatchKey] = sw;
                    ctx.Disposed           += sw.Stop;

                    sw.Start();
                };

                e.AfterMessage += (_, ex, msg) =>
                {
                    if (!MessageContext.HasCurrent)
                    {
                        _Log.Warn("Missing message context at AfterMessage event?!");
                        return;
                    }

                    var ctx   = MessageContext.GetCurrent();
                    var tname = ctx.CurrentMessage?.GetType().Name;
                    var sw    = ctx.Items.GetValueOrDefault(StopwatchKey, null) as System.Diagnostics.Stopwatch;

                    if (sw == null)
                    {
                        _Log.Warn("Missing stopwatch at AfterMessage event?!");
                        return;
                    }

                    if (sw.Elapsed > threshold)
                    {
                        _Log.WarnFormat("Message processing took too long ({0}), for message of type '{1}' (threshold: {2})", sw.Elapsed, tname, threshold);
                    }
                    else
                    {
                        _Log.InfoFormat("Message processing took ({0}), for message of type '{1}'", sw.Elapsed, tname);
                    }
                };
            });
            return(configurer);
        }
コード例 #6
0
        public static RebusConfigurer SetTimeToBeReceivedFrom <TAttribute>(this RebusConfigurer configurer, Func <TAttribute, TimeSpan> getter)
            where TAttribute : System.Attribute
        {
            Guard.IsNotNull(() => configurer, configurer);
            Guard.IsNotNull(() => getter, getter);

            return(configurer.Events(e =>
            {
                e.MessageSent += (advbus, destination, message) =>
                {
                    var attribute = message.GetType()
                                    .GetCustomAttributes(typeof(TAttribute), false)
                                    .Cast <TAttribute>()
                                    .SingleOrDefault();

                    if (attribute != null)
                    {
                        advbus.AttachHeader(message, Headers.TimeToBeReceived, getter(attribute).ToString());
                    }
                };
            }));
        }
コード例 #7
0
        public static RebusConfigurer ValidateIncomingMessages(this RebusConfigurer configurer, IEnumerable <Type> inclusions = null, IEnumerable <Type> exclusions = null)
        {
            configurer.Events(evs =>
                              evs.BeforeHandling += (ibus, message, handler) =>
            {
                if (message == null)
                {
                    return;
                }
                if (exclusions != null && exclusions.Contains(message.GetType()))
                {
                    return;
                }
                if (inclusions != null && !inclusions.Contains(message.GetType()))
                {
                    return;
                }

                ExtendedValidator.EnsureIsValid(message);
            });

            return(configurer);
        }
コード例 #8
0
        /// <summary>
        /// Installs a hook that automatically transfers the correlation ID of incoming messages to the Log4Net
        /// <see cref="ThreadContext"/>, allowing it to be included in the logging output. <see cref="propertyKey"/>
        /// specifies the key under which the correlation ID will be set.
        /// </summary>
        public static RebusConfigurer TransferCorrelationIdToLog4NetThreadContext(this RebusConfigurer configurer, string propertyKey)
        {
            configurer.Events(e => SetLoggerPropertiesWhenAvailable(e, propertyKey));

            return(configurer);
        }