Skip to content

Commit cbc6ad5

Browse files
committed
Merge branch '5.4' into 6.2
* 5.4: [Messenger] Add `BatchHandlerInterface` and `BatchHandlerTrait` mentions
2 parents 8e006c6 + ab0b2da commit cbc6ad5

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

messenger.rst

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,71 @@ That's it! You can now consume each transport:
21292129
If a handler does *not* have ``from_transport`` config, it will be executed
21302130
on *every* transport that the message is received from.
21312131

2132+
Process Messages by Batches
2133+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
2134+
2135+
You can declare "special" handlers which will process messages by batch.
2136+
By doing so, the handler will wait for a certain amount of messages to be
2137+
pending before processing them. The declaration of a batch handler is done
2138+
by implementing
2139+
:class:`Symfony\\Component\\Messenger\\Handler\\BatchHandlerInterface`. The
2140+
:class:`Symfony\\Component\\Messenger\\Handler\\BatchHandlerTrait` is also
2141+
provided in order to ease the declaration of these special handlers::
2142+
2143+
use Symfony\Component\Messenger\Handler\Acknowledger;
2144+
use Symfony\Component\Messenger\Handler\BatchHandlerInterface;
2145+
use Symfony\Component\Messenger\Handler\BatchHandlerTrait;
2146+
2147+
class MyBatchHandler implements BatchHandlerInterface
2148+
{
2149+
use BatchHandlerTrait;
2150+
2151+
public function __invoke(MyMessage $message, Acknowledger $ack = null)
2152+
{
2153+
return $this->handle($message, $ack);
2154+
}
2155+
2156+
private function process(array $jobs): void
2157+
{
2158+
foreach ($jobs as [$message, $ack]) {
2159+
try {
2160+
// Compute $result from $message...
2161+
2162+
// Acknowledge the processing of the message
2163+
$ack->ack($result);
2164+
} catch (\Throwable $e) {
2165+
$ack->nack($e);
2166+
}
2167+
}
2168+
}
2169+
2170+
// Optionally, you can redefine the `shouldFlush()` method
2171+
// of the trait to define your own batch size
2172+
private function shouldFlush(): bool
2173+
{
2174+
return 100 <= \count($this->jobs);
2175+
}
2176+
}
2177+
2178+
.. note::
2179+
2180+
When the ``$ack`` argument of ``__invoke()`` is ``null``, the message is
2181+
expected to be handled synchronously. Otherwise, ``__invoke()`` is
2182+
expected to return the number of pending messages. The
2183+
:class:`Symfony\\Component\\Messenger\\Handler\\BatchHandlerTrait` handles
2184+
this for you.
2185+
2186+
.. note::
2187+
2188+
By default, pending batches are flushed when the worker is idle as well
2189+
as when it is stopped.
2190+
2191+
.. versionadded:: 5.4
2192+
2193+
:class:`Symfony\\Component\\Messenger\\Handler\\BatchHandlerInterface` and
2194+
:class:`Symfony\\Component\\Messenger\\Handler\\BatchHandlerTrait` were
2195+
introduced in Symfony 5.4.
2196+
21322197
Extending Messenger
21332198
-------------------
21342199

0 commit comments

Comments
 (0)