【问题标题】:How to measure invocation time of "Handle" method in NServiceBus?如何测量 NServiceBus 中“Handle”方法的调用时间?
【发布时间】:2015-05-29 11:52:57
【问题描述】:

我需要在 IHandleMessages 接口的每个实例中测量 Handle 方法的调用时间。 我尝试使用温莎城堡拦截器,

public class NsbHandlerMeasurementInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        if (invocation.Method.Name == ExpressionExtender.GetMethodName<IHandleMessages<DummyType>>(b => b.Handle(null)))
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            invocation.Proceed();

            stopwatch.Stop();

            // save stopwatch.ElapsedMilliseconds value
        }
        else
        {
            invocation.Proceed();
        }
    }
}

附安装代码:

container.Register(Component.For<NsbHandlerMeasurementInterceptor>());
container.Kernel.ComponentModelBuilder.AddContributor(new NsbWindsorModelConstructionContributor());

public class NsbWindsorModelConstructionContributor : IContributeComponentModelConstruction
{
    public void ProcessModel(global::Castle.MicroKernel.IKernel kernel, global::Castle.Core.ComponentModel model)
    {
        if (model.Services.Any(s => s.ImplementsGenericInterface(typeof(IHandleMessages<>))))
        {
            model.Interceptors.AddIfNotInCollection(new InterceptorReference(typeof(NsbHandlerMeasurementInterceptor)));    
        }
    }
}

但从那一刻起,Handle 方法没有触发。

我知道 NSB 中的性能计数器,但我需要更具体的、面向类型的测量。有可能实现吗?

【问题讨论】:

    标签: c# castle-windsor nservicebus stopwatch nservicebus5


    【解决方案1】:

    确实有性能计数器来衡量所有内容,但如果这还不够,那么您可以在 NServiceBus 管道中创建自己的步骤。

    http://docs.particular.net/nservicebus/pipeline/customizing

    通过继承IBehavior&lt;IncomingContext&gt;创建自定义行为并实现接口。您现在可以访问包含有关类型信息的 IncomingContext 参数。

    看看InvokeHandlersBehavior 行为的实现。此行为调用实际的处理程序,并且可能想要包装它。

    https://github.com/Particular/NServiceBus/blob/5.2.0/src/NServiceBus.Core/Unicast/Behaviors/InvokeHandlersBehavior.cs

    class InvokeHandlersBehavior : IBehavior<IncomingContext>
    {
        public void Invoke(IncomingContext context, Action next)
        {
            ActiveSagaInstance saga;
    
            if (context.TryGet(out saga) && saga.NotFound && saga.SagaType == context.MessageHandler.Instance.GetType())
            {
                next();
                return;
            }
    
            var messageHandler = context.MessageHandler;
    
            messageHandler.Invocation(messageHandler.Instance, context.IncomingLogicalMessage.Instance);
            next();
        }
    }
    

    然后您需要注册它,以便它被调用包含在管道中。

    class NewStepInPipeline : RegisterStep
    {
        public NewStepInPipeline()
            : base("NewStepInPipeline", typeof(SampleBehavior), "Logs a warning when processing takes too long")
        {
            // Optional: Specify where it needs to be invoked in the pipeline, for example InsertBefore or InsertAfter
            InsertBefore(WellKnownStep.InvokeHandlers);
        }
    }
    
    class NewStepInPipelineRegistration : INeedInitialization
    {
        public void Customize(BusConfiguration busConfiguration)
        {
            // Register the new step in the pipeline
            busConfiguration.Pipeline.Register<NewStepInPipeline>();
        }
    }
    

    请注意,此代码需要 v5。查看特定文档网站以获取有关其他版本的帮助。

    【讨论】:

      猜你喜欢
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      相关资源
      最近更新 更多