0

我在 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 需要反序列化失败的消息,之前序列化并存储在数据库中,但因为它是一个接口,所以不能这样做。

我该如何解决这个问题?有没有办法使用类实现而不是接口来序列化失败的消息?

4

1 回答 1

1

失败的消息被序列化地存储在数据库中。当您重试或显示这些消息时,它们将被反序列化。

只需将 interface 替换SampleMessageInterface为 class SampleMessage

于 2020-08-19T12:11:18.730 回答