1 回答

TA貢獻(xiàn)1846條經(jīng)驗 獲得超7個贊
要將元數(shù)據(jù)添加到消息中,您可以使用 stamps。
您以后可以在自己的自定義中間件中使用它。
例如,對于這個自定義StampInterface
實現(xiàn)類:
class LoopCount implements StampInterface {
private int $count;
public function __construct($count) {
$this->count = $count;
}
public function getCount(): int {
return $this->count;
}
}
然后創(chuàng)建您自己的中間件來檢查此標(biāo)記并在處理后重新發(fā)送:
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'
我匆忙寫了這篇文章,現(xiàn)在無法測試,但 base 應(yīng)該可以幫助你朝著正確的方向前進(jìn)。測試和調(diào)試,你會得到它的工作。我稍后會回到這個問題上,看看是否有遺漏的東西
- 1 回答
- 0 關(guān)注
- 123 瀏覽
添加回答
舉報