1 回答

TA貢獻1846條經驗 獲得超7個贊
要將元數據添加到消息中,您可以使用 stamps。
您以后可以在自己的自定義中間件中使用它。
例如,對于這個自定義StampInterface
實現類:
class LoopCount implements StampInterface {
private int $count;
public function __construct($count) {
$this->count = $count;
}
public function getCount(): int {
return $this->count;
}
}
然后創建您自己的中間件來檢查此標記并在處理后重新發送:
class ResendingMiddleware implements MiddlewareInterface
{
private $bus;
public function __construct(MessageBusInterface $bus) {
$this->bus = $bus;
}
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$envelope = $stack->next()->handle($envelope, $stack);
if (null !== $stamp = $envelope->last(LoopCount::class)) {
$count = $stamp->getCount();
} else {
return $envelope;
}
// Stop dispatching
if ($count > 9) {
return $envelope;
}
$this->bus->dispatch(new RetryTest("Dit is een test"), [
new DelayStamp(5000),
new LoopCount($count + 1)
]);
return $envelope;
}
如果它被處理超過 9 次,則不做任何事情來使用消息。
您還需要將中間件添加到配置中:
framework:
messenger:
buses:
messenger.bus.default:
middleware:
# service ids that implement Symfony\Component\Messenger\Middleware\MiddlewareInterface
- 'App\Middleware\ResendingMiddleware'
我匆忙寫了這篇文章,現在無法測試,但 base 應該可以幫助你朝著正確的方向前進。測試和調試,你會得到它的工作。我稍后會回到這個問題上,看看是否有遺漏的東西
- 1 回答
- 0 關注
- 129 瀏覽
添加回答
舉報