我希望能够轻松地同步和异步处理 api 调用。
所以我使用相应的处理程序和基类创建了一个空接口
interface ApiPayloadInterface
{
}
class ApiPayload implements ApiPayloadInterface
{
/** @var array */
public $payload;
/** @var string */
public $username;
}
class ApiPayloadMessageHandler implements MessageHandlerInterface
{
public function __invoke(ApiPayloadInterface $row)
{
...
}
}
好的,那么我有空的子类来区分异步和同步
class ApiPayloadSync extends ApiPayload implements ApiPayloadInterface
{}
class ApiPayloadASync extends ApiPayload implements ApiPayloadInterface
{}
选择发生在
messenger.yaml
transports:
async: 'doctrine://default?auto_setup=false'
sync:
dsn: 'sync://'
retry_strategy:
max_retries: 0
routing:
'App\Entity\Messages\ApiPayloadSync': sync
'App\Entity\Messages\ApiPayloadASync': async
好的,这确实有效,但我在调度同步有效负载时遇到错误:
$this->dispatch($bSync ? new ApiPayloadSync() : new ApiPayloadASync());
在我的日志文件中,我看到几十条这样的消息:
[2019-10-27 22:11:14] messenger.CRITICAL:处理消息 App\Entity\Messages\ApiPayloadSync 时抛出错误。重试 3 次后从运输中移除。错误:“没有消息“App\Entity\Messages\ApiPayloadSync”的处理程序。” {“消息”:“[对象](App\Entity\Messages\ApiPayloadSync:{“payload”:{“action”:“mdcadd”,...})”,“class”:“App\Entity\Messages\ ApiPayloadSync","retryCount":3,"error":"没有消息“App\Entity\Messages\ApiPayloadSync”的处理程序。","异常":"[对象] (Symfony\Component\Messenger\Exception\NoHandlerForMessageException(代码:0):没有消息“App\Entity\Messages\ApiPayloadSync”的处理程序。位于 /var/www/xxx/vendor/symfony/messenger/Middleware/HandleMessageMiddleware.php: 75)"} []
因此,框架确实会开始重试,尽管我将其配置为不执行任何重试(对于同步传输)
在 Symfony 7.1 中需要 handler 上面的属性
#[作为消息处理程序]
https://symfony.com/doc/current/messenger.html#creating-a-message-handler