forked from php-enqueue/stomp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StompConsumer.php
232 lines (185 loc) · 6.24 KB
/
StompConsumer.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
declare(strict_types=1);
namespace Enqueue\Stomp;
use Interop\Queue\Consumer;
use Interop\Queue\Exception\Exception;
use Interop\Queue\Exception\InvalidMessageException;
use Interop\Queue\Message;
use Interop\Queue\Queue;
use Stomp\Client;
use Stomp\Exception\ErrorFrameException;
use Stomp\Transport\Frame;
class StompConsumer implements Consumer
{
const ACK_AUTO = 'auto';
const ACK_CLIENT = 'client';
const ACK_CLIENT_INDIVIDUAL = 'client-individual';
/**
* @var StompDestination
*/
private $queue;
/**
* @var Client
*/
private $stomp;
/**
* @var bool
*/
private $isSubscribed;
/**
* @var string
*/
private $ackMode;
/**
* @var int
*/
private $prefetchCount;
/**
* @var string
*/
private $subscriptionId;
public function __construct(BufferedStompClient $stomp, StompDestination $queue)
{
$this->stomp = $stomp;
$this->queue = $queue;
$this->isSubscribed = false;
$this->ackMode = self::ACK_CLIENT_INDIVIDUAL;
$this->prefetchCount = 1;
$this->subscriptionId = StompDestination::TYPE_TEMP_QUEUE == $queue->getType() ?
$queue->getQueueName() :
uniqid('', true)
;
}
public function setAckMode(string $mode): void
{
if (false === in_array($mode, [self::ACK_AUTO, self::ACK_CLIENT, self::ACK_CLIENT_INDIVIDUAL], true)) {
throw new \LogicException(sprintf('Ack mode is not valid: "%s"', $mode));
}
$this->ackMode = $mode;
}
public function getAckMode(): string
{
return $this->ackMode;
}
public function getPrefetchCount(): int
{
return $this->prefetchCount;
}
public function setPrefetchCount(int $prefetchCount): void
{
$this->prefetchCount = $prefetchCount;
}
/**
* @return StompDestination
*/
public function getQueue(): Queue
{
return $this->queue;
}
public function receive(int $timeout = 0): ?Message
{
$this->subscribe();
try {
if (0 === $timeout) {
while (true) {
if ($message = $this->stomp->readMessageFrame($this->subscriptionId, 100)) {
return $this->convertMessage($message);
}
}
} else {
if ($message = $this->stomp->readMessageFrame($this->subscriptionId, $timeout)) {
return $this->convertMessage($message);
}
}
} catch (ErrorFrameException $e) {
throw new Exception($e->getMessage()."\n".$e->getFrame()->getBody(), 0, $e);
}
return null;
}
public function receiveNoWait(): ?Message
{
$this->subscribe();
if ($message = $this->stomp->readMessageFrame($this->subscriptionId, 0)) {
return $this->convertMessage($message);
}
return null;
}
/**
* @param StompMessage $message
*/
public function acknowledge(Message $message): void
{
InvalidMessageException::assertMessageInstanceOf($message, StompMessage::class);
$this->stomp->sendFrame(
$this->stomp->getProtocol()->getAckFrame($message->getFrame())
);
}
/**
* @param StompMessage $message
*/
public function reject(Message $message, bool $requeue = false): void
{
InvalidMessageException::assertMessageInstanceOf($message, StompMessage::class);
$nackFrame = $this->stomp->getProtocol()->getNackFrame($message->getFrame());
if (ExtensionType::RABBITMQ === $this->queue->getExtensionType()) {
$nackFrame->addHeaders([
'requeue' => $requeue ? 'true' : 'false',
]);
}
$this->stomp->sendFrame($nackFrame);
}
private function subscribe(): void
{
if (StompDestination::TYPE_TEMP_QUEUE == $this->queue->getType()) {
$this->isSubscribed = true;
return;
}
if (false == $this->isSubscribed) {
$this->isSubscribed = true;
$frame = $this->stomp->getProtocol()->getSubscribeFrame(
$this->queue->getQueueName(),
$this->subscriptionId,
$this->ackMode
);
$headers = $this->queue->getHeaders();
if (ExtensionType::RABBITMQ === $this->queue->getExtensionType()) {
$headers['prefetch-count'] = $this->prefetchCount;
$headers = StompHeadersEncoder::encode($headers);
foreach ($headers as $key => $value) {
$frame[$key] = $value;
}
} elseif (ExtensionType::ARTEMIS === $this->queue->getExtensionType()) {
$subscriptionName = $this->subscriptionId.'-'.$this->queue->getStompName();
$artemisHeaders = [];
$artemisHeaders['client-id'] = true ? $this->subscriptionId : null;
$artemisHeaders['durable-subscription-name'] = true ? $subscriptionName : null;
$artemisHeaders = StompHeadersEncoder::encode(array_filter($artemisHeaders));
foreach ($artemisHeaders as $key => $value) {
$frame[$key] = $value;
}
}
$this->stomp->sendFrame($frame);
}
}
private function convertMessage(Frame $frame): StompMessage
{
if ('MESSAGE' !== $frame->getCommand()) {
throw new \LogicException(sprintf('Frame is not MESSAGE frame but: "%s"', $frame->getCommand()));
}
list($headers, $properties) = StompHeadersEncoder::decode($frame->getHeaders());
$redelivered = isset($headers['redelivered']) && 'true' === $headers['redelivered'];
unset(
$headers['redelivered'],
$headers['destination'],
$headers['message-id'],
$headers['ack'],
$headers['receipt'],
$headers['subscription'],
$headers['content-length']
);
$message = new StompMessage((string) $frame->getBody(), $properties, $headers);
$message->setRedelivered($redelivered);
$message->setFrame($frame);
return $message;
}
}