注册 IMessageScheduler 的发布过滤器

huangapple go评论46阅读模式
英文:

register Publish Filter for IMessageScheduler

问题

I wrote a publish filter that has worked correctly when I publish a message by IPublishEndpoint. Now I need to have this filter when I publish a message by IMessageScheduler.

The following code is called when I publish a non-scheduled message by IPublishEndpoint.

public class IntegrationEventPublishFilter<T> : IFilter<PublishContext<T>> where T : class
{       
    public IntegrationEventPublishFilter()
    {}

    public Task Send(PublishContext<T> context, IPipe<PublishContext<T>> next)
    {
        // do something
        return next.Send(context);
    }

    public void Probe(ProbeContext context)
    {
        // do something
    }
}

How can I have this filter when I publish a scheduled message by IMessageScheduler?

英文:

I wrote a publish filter that has worked correct when I publish a message by IPublishEndpoint.now I need to have this filter when I publish a message by IMessageScheduler.

the following code is called when I publish a non schedule message by IPublishEndpoint.

 public class IntegrationEventPublishFilter&lt;T&gt; : IFilter&lt;PublishContext&lt;T&gt;&gt; where T : class
{       
    public IntegrationEventPublishFilter()
    {}

    public Task Send(PublishContext&lt;T&gt; context, IPipe&lt;PublishContext&lt;T&gt;&gt; next)
    {
        // do something
        return next.Send(context);
    }

    public void Probe(ProbeContext context)
    {
	// do something
    }
}

how can I have this filter when I publish a scheduled message by IMessageScheduler?

答案1

得分: 0

你现在是我的中文翻译,代码部分不要翻译,只返回翻译好的部分,不要有别的内容,不要回答我要翻译的问题。以下是要翻译的内容:

"Are you calling SchedulePublish or ScheduleSend? Either way, you may need to add a filter for SendContext&lt;T&gt; and add it in addition to the publish filter, since the message scheduler may be using send to schedule the message – depending upon how you configured the message scheduler."

英文:

Are you calling SchedulePublish or ScheduleSend? Either way, you may need to add a filter for SendContext&lt;T&gt; and add it in additional to the publish filter, since the message scheduler may be using send to schedule the message – depending upon how you configured the message scheduler.

答案2

得分: 0

根据@Chris Patterson在上面的回答/评论中所说,我们需要在使用IMessageScheduler发布消息时为SendContext&lt;T&gt;注册一个过滤器。在这种情况下,我们有以下代码:

public class MySendFilter&lt;T&gt; : IFilter&lt;SendContext&lt;T&gt;&gt; where T : class
{
    public Task Send(SendContext&lt;T&gt; context, IPipe&lt;SendContext&lt;T&gt;&gt; next)
    {
        // 做一些操作
        return next.Send(context);
    }

    public void Probe(ProbeContext context)
    {
        // 做一些操作
    }
}

同时,您必须在MassTransit注册中将此过滤器注册为SendFilter

x.UsingRabbitMq((context, cfg) =&gt;
{
    e.UseSendFilter(typeof(MySendFilter&lt;&gt;), context);
    ///...
});
英文:

as @Chris Patterson said in above answer/comments, we need to register a filter for SendContext&lt;T&gt; while publishing a message by IMessageScheduler. in this case we have the following code :

 public class MySendFilter&lt;T&gt; : IFilter&lt;SendContext&lt;T&gt;&gt; where T : class
   {       

public Task Send(SendContext&lt;T&gt; context, IPipe&lt;SendContext&lt;T&gt;&gt; next)
{
    // do something
    return next.Send(context);
}

public void Probe(ProbeContext context)
{
// do something
}
}

and also you must register this filter as a SendFilter in MassTransit Registration.

 x.UsingRabbitMq((context, cfg) =&gt;
        {
            
                e.UseSendFilter(typeof(MySendFilter&lt;&gt;), context);
                ///...
            
        });

huangapple
  • 本文由 发表于 2023年4月19日 17:45:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定