-
Notifications
You must be signed in to change notification settings - Fork 19
/
AmqpConsumer.php
138 lines (112 loc) · 2.99 KB
/
AmqpConsumer.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
declare(strict_types=1);
namespace Enqueue\AmqpLib;
use Interop\Amqp\AmqpConsumer as InteropAmqpConsumer;
use Interop\Amqp\AmqpMessage as InteropAmqpMessage;
use Interop\Amqp\AmqpQueue as InteropAmqpQueue;
use Interop\Queue\Exception\InvalidMessageException;
use Interop\Queue\Message;
use Interop\Queue\Queue;
use PhpAmqpLib\Channel\AMQPChannel;
class AmqpConsumer implements InteropAmqpConsumer
{
/**
* @var AmqpContext
*/
private $context;
/**
* @var AMQPChannel
*/
private $channel;
/**
* @var InteropAmqpQueue
*/
private $queue;
/**
* @var int
*/
private $flags;
/**
* @var string
*/
private $consumerTag;
public function __construct(AmqpContext $context, InteropAmqpQueue $queue)
{
$this->context = $context;
$this->channel = $context->getLibChannel();
$this->queue = $queue;
$this->flags = self::FLAG_NOPARAM;
}
public function setConsumerTag(string $consumerTag = null): void
{
$this->consumerTag = $consumerTag;
}
public function getConsumerTag(): ?string
{
return $this->consumerTag;
}
public function clearFlags(): void
{
$this->flags = self::FLAG_NOPARAM;
}
public function addFlag(int $flag): void
{
$this->flags |= $flag;
}
public function getFlags(): int
{
return $this->flags;
}
public function setFlags(int $flags): void
{
$this->flags = $flags;
}
/**
* @return InteropAmqpQueue
*/
public function getQueue(): Queue
{
return $this->queue;
}
/**
* @return InteropAmqpMessage
*/
public function receive(int $timeout = 0): ?Message
{
$end = microtime(true) + ($timeout / 1000);
while (0 === $timeout || microtime(true) < $end) {
if ($message = $this->receiveNoWait()) {
return $message;
}
usleep(100000); //100ms
}
return null;
}
/**
* @return InteropAmqpMessage
*/
public function receiveNoWait(): ?Message
{
if ($message = $this->channel->basic_get($this->queue->getQueueName(), (bool) ($this->getFlags() & InteropAmqpConsumer::FLAG_NOACK))) {
return $this->context->convertMessage($message);
}
return null;
}
/**
* @param InteropAmqpMessage $message
*/
public function acknowledge(Message $message): void
{
InvalidMessageException::assertMessageInstanceOf($message, InteropAmqpMessage::class);
$this->channel->basic_ack($message->getDeliveryTag());
}
/**
* @param InteropAmqpMessage $message
* @param bool $requeue
*/
public function reject(Message $message, bool $requeue = false): void
{
InvalidMessageException::assertMessageInstanceOf($message, InteropAmqpMessage::class);
$this->channel->basic_reject($message->getDeliveryTag(), $requeue);
}
}