But this middleware doesn't get executed, it may not have been configured, like:
#messenger.yaml
framework:
messenger:
buses:
messenger.bus.default:
middleware:
- 'App\Middleware\RetryCountMiddleware'
Also, a new stamp HandlerArgumentsStamp has appeared in Symfony 6.2, which allows you to add your own data a bit easier.
For example:
namespace App\Middleware;
use App\Contracts\RetryCountSupportInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Stamp\HandlerArgumentsStamp;
use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
class RetryCountMiddleware implements MiddlewareInterface
{
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
# Check based on interface, class, stamp or something else
if ($envelope->getMessage() instanceof RetryCountSupportInterface) {
$envelope = $envelope->with(new HandlerArgumentsStamp([
'retryCount' => RedeliveryStamp::getRetryCountFromEnvelope($envelope)
]));
}
return $stack->next()->handle($envelope, $stack);
}
}
And in handle
#[AsMessageHandler]
class YourMessageHandler
{
public function __invoke(YourMessage $message, int $retryCount): void
{
#...
}
}