I want to be able to handle api calls synchronoulsy and asynchronously easily.
So I created an empty interface with according handler and base class
interface ApiPayloadInterface
{
}
class ApiPayload implements ApiPayloadInterface
{
/** @var array */
public $payload;
/** @var string */
public $username;
}
class ApiPayloadMessageHandler implements MessageHandlerInterface
{
public function __invoke(ApiPayloadInterface $row)
{
...
}
}
Ok, then I have empty child classes to distinguish async from sync
class ApiPayloadSync extends ApiPayload implements ApiPayloadInterface
{}
class ApiPayloadASync extends ApiPayload implements ApiPayloadInterface
{}
The selection happens in 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
Ok, this does work, but I get errors when dispatching sync payloads:
$this->dispatch($bSync ? new ApiPayloadSync() : new ApiPayloadASync());
in my log file I see dozens of such messages:
[2019-10-27 22:11:14] messenger.CRITICAL: Error thrown while handling message App\Entity\Messages\ApiPayloadSync. Removing from transport after 3 retries. Error: "No handler for message "App\Entity\Messages\ApiPayloadSync"." {"message":"[object] (App\Entity\Messages\ApiPayloadSync: {"payload":{"action":"mdcadd",...})","class":"App\Entity\Messages\ApiPayloadSync","retryCount":3,"error":"No handler for message "App\Entity\Messages\ApiPayloadSync".","exception":"[object] (Symfony\Component\Messenger\Exception\NoHandlerForMessageException(code: 0): No handler for message "App\Entity\Messages\ApiPayloadSync". at /var/www/xxx/vendor/symfony/messenger/Middleware/HandleMessageMiddleware.php:75)"} []
So, the framework does start retries although I configured it to not perform any (for the sync transport)