【发布时间】:2020-12-08 09:53:25
【问题描述】:
我在 Symfony 4.4 应用程序中使用 Symfony Messenger 组件。我正在通过 RabbitMQ 异步处理消息,并通过 Doctrine 传输将失败的消息存储在数据库中。
这是信使配置:
framework:
messenger:
failure_transport: failed
buses:
command_bus:
middleware:
- doctrine_ping_connection
transports:
failed: 'doctrine://default?queue_name=failed'
async_priority_high:
dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
retry_strategy:
delay: 2000
max_retries: 5
multiplier: 2
options:
exchange:
name: high
queues:
messages_high: ~
async_priority_low:
dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
retry_strategy:
delay: 3000
max_retries: 3
multiplier: 2
options:
exchange:
name: low
queues:
messages_low: ~
routing:
'App\SampleMessageButHighPriority': async_priority_high
'App\SampleMessageInterface': async_priority_low
'App\OtherMessage': async_priority_low
这是一个示例处理程序,它处理实现SampleMessageInterface 接口的消息。
final class SampleMessageHandler implements MessageHandlerInterface
{
private ProjectRepository $projectRepository;
public function __construct(ProjectRepository $projectRepository)
{
$this->projectRepository = $projectRepository;
}
public function __invoke(SampleMessageInterface $message): void
{
$project = $this->projectRepository->find($message->getProjectId()->toString());
if ($project === null) {
return;
}
$this->someProcessor->__invoke($project);
}
}
在遇到任何消息失败之前一切正常。尝试重试或显示失败消息时,问题在失败后开始显示。让我们试试php bin/console messenger:failed:show 命令:
结果:
In PhpSerializer.php line 64:
Cannot instantiate interface App\SampleMessageInterface
我猜Symfony需要反序列化失败的消息,之前序列化并存储在数据库中,但是因为它是一个接口,所以不能这样做。
我该如何解决这个问题?有没有办法使用类实现而不是接口来序列化失败的消息?
【问题讨论】:
-
请不要将代码放入图片中。使用```code``来发布你的格式化代码
-
对此我很抱歉,我刚刚更新了我的问题。谢谢你告诉我。
-
github.com/opengento/magento2-gdpr/issues/43 这里说这是一个缓存问题。也许您已经尝试清理缓存?你为什么用
final这个关键词? -
Doc : symfony.com/doc/current/… 没有使用final关键字
-
可能的帮助下一个配置:``` messenger serializer: default_serializer: "messenger.transport.symfony_serializer" ``` 为我工作。在 4.2 中有它,但是在 4.4 之后(可能更早,没有检查),它默认更改为 phpSerializer。所以只需设置它,应该可以工作
标签: symfony symfony4 symfony-messenger