2 回答
TA贡献1875条经验 获得超5个赞
最终,我应用了以下解决方案:
<?php
/**
* Author: writ3it
*/
namespace App\Maintenance\Messenger;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\MessageBusInterface;
class FailedMessageListener implements EventSubscriberInterface
{
/**
* @var MessageBusInterface
*/
private MessageBusInterface $bus;
public function __construct(MessageBusInterface $bus)
{
$this->bus = $bus;
}
public function onMessageFailed(WorkerMessageFailedEvent $event)
{
if ($event->willRetry()) {
return;
}
$envelope = $event->getEnvelope();
$message = $envelope->getMessage();
$subject = sprintf("Message added to failed transport. Message: %s", \get_class($message));
$content = serialize($message);
$notification = new FailedMessageNotificationEvent();
$notification->setSubject($subject)
->setContent($content);
$this->bus->dispatch($notification);
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
WorkerMessageFailedEvent::class => ['onMessageFailed', -256],
];
}
}
FailedMessageNotificationEvent是一条将被我的邮件处理程序捕获的消息。该类FailedMessageListener应标记为kernel.event_subscriber或自动配置。
我没有使用中间件,因为 Messenger 直接将消息发送到失败的传输,没有中间件。
- 2 回答
- 0 关注
- 138 浏览
添加回答
举报