You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/consumption/message_processor.md
+44-4
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,13 @@
1
1
# Message processor
2
2
3
+
*[Basics](#basics)
4
+
*[Reply result](#reply-result)
5
+
*[On exceptions](#on-exceptions)
6
+
*[Examples](#examples)
7
+
8
+
9
+
## Basics
10
+
3
11
The message processor is an object that actually process the message and must return a result status.
4
12
Here's example:
5
13
@@ -20,9 +28,15 @@ class SendMailProcessor implements PsrProcessor
20
28
}
21
29
```
22
30
23
-
Usually there is no need to catch exceptions.
24
-
The message broker can detect consumer has failed and redeliver the message.
25
-
Sometimes you have to reject messages explicitly.
31
+
By returning `self::ACK` a processor tells a broker that the message has been processed correctly.
32
+
33
+
There are other statuses:
34
+
35
+
*`self::ACK` - Use this constant when the message is processed successfully and the message could be removed from the queue.
36
+
*`self::REJECT` - Use this constant when the message is not valid or could not be processed. The message is removed from the queue.
37
+
*`self::REQUEUE` - Use this constant when the message is not valid or could not be processed right now but we can try again later
38
+
39
+
Look at the next example that shows the message validation before sending a mail. If the message is not valid a processor rejects it.
26
40
27
41
```php
28
42
<?php
@@ -95,7 +109,11 @@ class SendMailProcessor implements PsrProcessor
95
109
}
96
110
```
97
111
98
-
The consumption component provide some useful extensions, for example there is an extension that makes RPC processing simplier.
112
+
## Reply result
113
+
114
+
The consumption component provide some useful extensions, for example there is an extension that makes RPC processing simpler.
115
+
The producer might wait for a reply from a consumer and in order to send it a processor has to return a reply result.
116
+
Dont forget to add `ReplyExtension`.
99
117
100
118
```php
101
119
<?php
@@ -130,4 +148,26 @@ $queueConsumer->bind('foo', new SendMailProcessor());
130
148
$queueConsumer->consume();
131
149
```
132
150
151
+
152
+
## On exceptions
153
+
154
+
It is advised to not catch exceptions and [fail fast](https://en.wikipedia.org/wiki/Fail-fast).
155
+
Also consider using [supervisord](supervisord.org) or similar process manager to restart exited consumers.
156
+
157
+
Despite advising to fail there are some cases where you might want to catch exceptions.
158
+
159
+
* A message validator throws an exception on invalid message. It is better to catch it and return `REJECT`.
160
+
* Some transports ([Doctrine DBAL](../transport/dbal.md), [Filesystem](../transport/filesystem.md), [Redis](../transport/redis.md)) does notice an error,
161
+
and therefor won't be able to redeliver the message. The message is completely lost. You might want to catch an exception to properly redelivery\requeue the message.
0 commit comments