Skip to content

Commit 5c4382d

Browse files
author
Valentin Nazarov
committed
[Messenger] Add per-message priority
1 parent bd6f7b4 commit 5c4382d

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

components/messenger.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ Here are some important envelope stamps that are shipped with the Symfony Messen
146146

147147
#. :class:`Symfony\\Component\\Messenger\\Stamp\\DelayStamp`,
148148
to delay handling of an asynchronous message.
149+
#. :class:`Symfony\\Component\\Messenger\\Stamp\\PriorityStamp`,
150+
to prioritize messages of the same type within a queue. Read more
151+
at :doc:`/messenger/message_priority`.
149152
#. :class:`Symfony\\Component\\Messenger\\Stamp\\DispatchAfterCurrentBusStamp`,
150153
to make the message be handled after the current bus has executed. Read more
151154
at :doc:`/messenger/dispatch_after_current_bus`.

messenger/message_priority.rst

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
Message Priority
2+
================
3+
4+
By default messenger uses the first in, first out principle: messages will be
5+
received in the same order they were sent (except the delayed ones).
6+
7+
Basic priority can be achieved by using :doc:`prioritized transports </messenger>`,
8+
but only for different message types.
9+
10+
With AMQP and Beanstalkd transports, you can have a priority queue for messages
11+
of the same type::
12+
13+
use Symfony\Component\Messenger\Envelope;
14+
use Symfony\Component\Messenger\Stamp\PriorityStamp;
15+
16+
$bus->dispatch(
17+
(new Envelope($message))->with(new PriorityStamp(255))
18+
);
19+
20+
.. tip::
21+
22+
Priorities between ``0`` (lowest) and ``255`` (highest) are supported.
23+
24+
Priority can be used together with delay::
25+
26+
use Symfony\Component\Messenger\Envelope;
27+
use Symfony\Component\Messenger\Stamp\DelayStamp;
28+
use Symfony\Component\Messenger\Stamp\PriorityStamp;
29+
30+
$bus->dispatch(
31+
(new Envelope($message))->with(
32+
new PriorityStamp(255),
33+
new DelayStamp(5000)
34+
)
35+
);
36+
37+
When the time comes, if other messages in queue have lower priority, this one
38+
will be delivered first.
39+
40+
Using with Beanstalkd transport
41+
-------------------------------
42+
43+
Beanstalkd does not require any additional configuration. You can start using priority
44+
stamp right away.
45+
46+
.. note::
47+
48+
Internally Beanstalkd uses a different priority system, where priority
49+
can take values between ``2^32 - 1`` and ``0``, with zero being the highest priority.
50+
Messenger handles the transformation internally.
51+
52+
Using with AMQP transport
53+
-------------------------
54+
55+
With AMQP transport, you need to enable priority for the queue in configuration:
56+
57+
.. configuration-block::
58+
59+
.. code-block:: yaml
60+
61+
# config/packages/messenger.yaml
62+
framework:
63+
messenger:
64+
transports:
65+
async:
66+
dsn: "%env(MESSENGER_TRANSPORT_DSN)%"
67+
options:
68+
queues:
69+
messenger:
70+
arguments:
71+
x-max-priority: 255
72+
73+
.. code-block:: xml
74+
75+
<?xml version="1.0" encoding="UTF-8" ?>
76+
<container xmlns="http://symfony.com/schema/dic/services"
77+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
78+
xmlns:framework="http://symfony.com/schema/dic/symfony"
79+
xsi:schemaLocation="http://symfony.com/schema/dic/services
80+
https://symfony.com/schema/dic/services/services-1.0.xsd
81+
http://symfony.com/schema/dic/symfony
82+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
83+
84+
<framework:config>
85+
<framework:messenger>
86+
<framework:transport name="async"
87+
dsn="%env(MESSENGER_TRANSPORT_DSN)%"
88+
>
89+
<framework:options>
90+
<framework:queues>
91+
<framework:messenger>
92+
<framework:arguments>
93+
<framework:x-max-priority>255</framework:x-max-priority>
94+
</framework:arguments>
95+
</framework:messenger>
96+
</framework:queues>
97+
</framework:options>
98+
</framework:transport>
99+
</framework:messenger>
100+
</framework:config>
101+
</container>
102+
103+
.. code-block:: php
104+
105+
// config/packages/messenger.php
106+
use Symfony\Config\FrameworkConfig;
107+
108+
return static function (FrameworkConfig $framework) {
109+
$messenger = $framework->messenger();
110+
111+
$messenger->transport('async')
112+
->dsn('%env(MESSENGER_TRANSPORT_DSN)%')
113+
->options([
114+
'queues' => [
115+
'messenger' => [
116+
'arguments' => [
117+
'x-max-priority' => 255,
118+
],
119+
]
120+
]
121+
]);
122+
};
123+
124+
.. caution::
125+
126+
It's not safe to set ``x-max-priority`` for an existing queue.
127+
RabbitMQ can not change existing queue's configuration. Messenger will
128+
fail to auto-setup and priorities won't work.
129+
130+
.. note::
131+
132+
`RabbitMQ manual recommends`_ using up to 10 different priority levels.
133+
For example, you may use 255 for high, 127 for medium and 0 for low priority.
134+
Having more levels may have an impact on performance.
135+
136+
.. _`RabbitMQ manual recommends`: https://www.rabbitmq.com/priority.html

0 commit comments

Comments
 (0)