8000 [Messenger] Add `BatchHandlerInterface` and `BatchHandlerTrait` mentions · symfony/symfony-docs@b1c77c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit b1c77c9

Browse files
[Messenger] Add BatchHandlerInterface and BatchHandlerTrait mentions
1 parent 6cf5c14 commit b1c77c9

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
@@ -2201,6 +2201,71 @@ That's it! You can now consume each transport:
22012201
If a handler does *not* have ``from_transport`` config, it will be executed
22022202
on *every* transport that the message is received from.
22032203

2204+
Process Messages by Batches
2205+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
2206+
2207+
You can declare "special" handlers which will process messages by batch.
2208+
By doing so, the handler will wait for a certain amount of messages to be
2209+
pending before processing them. The declaration of a batch handler is done
2210+
by implementing
2211+
:class:`Symfony\\Component\\Messenger\\Handler\\BatchHandlerInterface`. The
2212+
:class:`Symfony\\Component\\Messenger\\Handler\\BatchHandlerTrait` is also
2213+
provided in order to ease the declaration of these special handlers::
2214+
2215+
use Symfony\Component\Messenger\Handler\Acknowledger;
2216+
use Symfony\Component\Messenger\Handler\BatchHandlerInterface;
2217+
use Symfony\Component\Messenger\Handler\BatchHandlerTrait;
2218+
2219+
class MyBatchHandler implements BatchHandlerInterface
2220+
{
2221+
use BatchHandlerTrait;
2222+
2223+
public function __invoke(MyMessage $message, Acknowledger $ack = null)
2224+
{
2225+
return $this->handle($message, $ack);
2226+
}
2227+
2228+
private function process(array $jobs): void
2229+
{
2230+
foreach ($jobs as [$message, $ack]) {
2231+
try {
2232+
// Compute $result from $message...
2233+
2234+
// Acknowledge the processing of the message
2235+
$ack->ack($result);
2236+
} catch (\Throwable $e) {
2237+
$ack->nack($e);
2238+
}
2239+
}
2240+
}
2241+
2242+
// Optionally, you can redefine the `shouldFlush()` method
2243+
// of the trait to define your own batch size
2244+
private function shouldFlush(): bool
2245+
{
2246+
return 100 <= \count($this->jobs);
2247+
}
2248+
}
2249+
2250+
.. note::
2251+
2252+
When the ``$ack`` argument of ``__invoke()`` is ``null``, the message is
2253+
expected to be handled synchronously. Otherwise, ``__invoke()`` is
2254+
expected to return the number of pending messages. The
2255+
:class:`Symfony\\Component\\Messenger\\Handler\\BatchHandlerTrait` handles
2256+
this for you.
2257+
2258+
.. note::
2259+
2260+
By default, pending batches are flushed when the worker is idle as well
2261+
as when it is stopped.
2262+
2263+
.. versionadded:: 5.4
2264+
2265+
:class:`Symfony\\Component\\Messenger\\Handler\\BatchHandlerInterface` and
2266+
:class:`Symfony\\Component\\Messenger\\Handler\\BatchHandlerTrait` were
2267+
introduced in Symfony 5.4.
2268+
22042269
Extending Messenger
22052270
-------------------
22062271

0 commit comments

Comments
 (0)
0