Symfony MessageHandler 计算一条消息被分派的次数 [英] Symfony MessageHandler count how many times a message has been dispatched

查看:19
本文介绍了Symfony MessageHandler 计算一条消息被分派的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Symfony Messenger,我想继续在处理程序中分派消息,直到它被分派多次.

I'm using Symfony Messenger and I want to keep dispatching a message in the handler until it has been dispatched a number of times.

我怎样才能跟踪它?

这是迄今为止我的处理程序类的代码:

This is the code of my handler class so far:

class RetryTestHandler implements MessageHandlerInterface
{
    /**
    * @var EntityManagerInterface
    */
    private $entityManager;
    /**
     * @var MessageBusInterface
     */
    private $bus;

    public function __construct(MessageBusInterface $bus, EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
        $this->bus = $bus;
    }

    public function __invoke(RetryTest $message)
    {
        // TODO: Keep dispatching message until it has been dispatched 10 times?
        $this->bus->dispatch(new RetryTest("This is a test!"), [
            new DelayStamp(5000)
        ]);
    }
}

推荐答案

要将元数据添加到您的消息,您可以使用邮票.

To add metadata to your messages, you can use stamps.

您以后可以在自己的自定义中间件中使用.

Which you can later use in your own custom middleware.

例如对于这个自定义 StampInterface 实现类:

E.g. for this custom StampInterface implementing class:

class LoopCount implements StampInterface {


    private int $count; 

    public function __construct($count) {
        $this->count = $count;
    }

    public function getCount(): int {
        return $this->count;
    } 
}

然后创建您自己的中间件来检查此戳记并重新处理后发货:

Then create your own middleware that checks for this stamp and re-dispatches after handling:

class ResendingMiddleware implements MiddlewareInterface
{
     private $bus;

     public function __construct(MessageBusInterface $bus) {
           $this->bus = $bus;
    }

    public function handle(Envelope $envelope, StackInterface $stack): Envelope
    {

        $envelope = $stack->next()->handle($envelope, $stack);

        if (null !== $stamp = $envelope->last(LoopCount::class)) {
            $count = $stamp->getCount();
        } else {
            return $envelope;
        }

        // Stop dispatching
        if ($count > 9) {
            return $envelope;
        }

        $this->bus->dispatch(new RetryTest("Dit is een test"), [
            new DelayStamp(5000),
            new LoopCount($count + 1)
        ]);

        return $envelope;
    }

如果处理次数超过 9 次,则不执行任何操作即可消费该消息.

If it was processed more than 9 times, consume the message without doing anything.

您还需要将中间件添加到配置中:

You need also to add the middleware to the configuration:

framework:
    messenger:
        buses:
            messenger.bus.default:
                middleware:
                    # service ids that implement Symfony\Component\Messenger\Middleware\MiddlewareInterface
                    - 'App\Middleware\ResendingMiddleware'

我匆忙写了这个,现在无法测试,但基础应该可以帮助你朝着正确的方向前进.测试和调试,你会得到它的工作.稍后我会回到这个问题,看看是否有任何遗漏

I wrote this in a hurry and can't test it right now, but the base should help you go in the right direction. Test and debug, and you'll get it working. I'll get back to this later to try to see if there is anything missing

这篇关于Symfony MessageHandler 计算一条消息被分派的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆