forked from sroze/messenger-enqueue-transport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageBusProcessor.php
64 lines (56 loc) · 1.93 KB
/
MessageBusProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Enqueue\MessengerAdapter;
use Enqueue\MessengerAdapter\Exception\RejectMessageException;
use Enqueue\MessengerAdapter\Exception\RequeueMessageException;
use Interop\Queue\Context;
use Interop\Queue\Message;
use Interop\Queue\Processor;
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
/**
* The processor could be used with any queue interop compatible consumer, for example Enqueue's QueueConsumer.
*
* @author Max Kotliar <kotlyar.maksim@gmail.com>
* @author Samuel Roze <samuel.roze@gmail.com>
*/
class MessageBusProcessor implements Processor
{
private $bus;
private $messageDecoder;
public function __construct(MessageBusInterface $bus, SerializerInterface $messageDecoder)
{
$this->bus = $bus;
$this->messageDecoder = $messageDecoder;
}
public function process(Message $message, Context $context)
{
try {
$busMessage = $this->messageDecoder->decode(array(
'body' => $message->getBody(),
'headers' => $message->getHeaders(),
'properties' => $message->getProperties(),
));
} catch (MessageDecodingFailedException $e) {
return Processor::REJECT;
}
try {
$this->bus->dispatch($busMessage);
return Processor::ACK;
} catch (RejectMessageException $e) {
return Processor::REJECT;
} catch (RequeueMessageException $e) {
return Processor::REQUEUE;
} catch (\Throwable $e) {
return Processor::REJECT;
}
}
}